Commit 9f198f74 by Jonathan Piacenti

Tests added to Survey.

parent 1042076a
......@@ -381,7 +381,7 @@ class SurveyBlock(PollBase):
'feedback': markdown(self.feedback) or False,
# The SDK doesn't set url_name.
'url_name': getattr(self, 'url_name', ''),
})
})
return self.create_fragment(
context, "public/html/survey.html", "public/css/poll.css",
......@@ -540,4 +540,13 @@ class SurveyBlock(PollBase):
<survey />
</vertical_demo>
"""),
("Survey Functions",
"""
<vertical_demo>
<survey tally='{"q1": {"sa": 5, "a": 5, "n": 3, "d": 2, "sd": 5}, "q2": {"sa": 3, "a": 2, "n": 3, "d": 10, "sd": 2}, "q3": {"sa": 2, "a": 7, "n": 1, "d": 4, "sd": 6}, "q4": {"sa": 1, "a": 2, "n": 8, "d": 4, "sd": 5}}'
questions='[["q1", "I feel like this test will pass."], ["q2", "I like testing software"], ["q3", "Testing is not necessary"], ["q4", "I would fake a test result to get software deployed."]]'
answers='[["sa", {"label": "Strongly Agree"}], ["a", {"label": "Agree"}], ["n", {"label": "Neutral"}], ["d", {"label": "Disagree"}], ["sd", {"label": "Strongly Disagree"}]]'
feedback="### Thank you&#10;&#10;for running the tests."/>
</vertical_demo>
""")
]
......@@ -12,9 +12,17 @@
<tr>
<td>{{{text}}}</td>
{{#each answers}}
<td>{{percent}}%</td>
<td class="survey-percentage">{{percent}}%</td>
{{/each}}
</tr>
{{/each}}
</table>
<input class="input-main" type="button" name="poll-submit" value="Submit" disabled>
<div class="poll-footnote">Results gathered from {{total}} respondent{{#if plural}}s{{/if}}.</div>
{{#if feedback}}
<hr />
<div class="poll-feedback">
{{{feedback}}}
</div>
{{/if}}
</script>
......@@ -8,17 +8,17 @@
<tr>
<th></th>
{% for answer, details in answers %}
<th>{{details.label}}</th>
<th class="survey-answer">{{details.label}}</th>
{% endfor %}
</tr>
</thead>
{% for key, question in questions %}
<tr>
<td>
<td class="survey-question">
{{question}}
</td>
{% for answer, answer_details in answers %}
<td>
<td class="survey-option">
<input type="radio" name="{{key}}" value="{{answer}}" />
</td>
{% endfor %}
......
......@@ -38,3 +38,21 @@ class TestDefault(PollBaseTest):
does not load since it is not enabled by default.
"""
self.go_to_page('Survey Defaults')
names = ['enjoy', 'recommend', 'learn']
# Select the first answer for each.
for name in names:
self.browser.find_element_by_css_selector('input[name="%s"]' % name).click()
submit = self.get_submit()
submit.click()
self.wait_until_exists('.survey-percentage')
# Should now be on the results page.
for element in self.browser.find_elements_by_css_selector('table > tr'):
# First element is question, second is first answer result.
self.assertEqual(element.find_elements_by_css_selector('td')[1].text, '100%')
# No feedback section.
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback')
......@@ -50,9 +50,7 @@ class TestPollFunctions(PollBaseTest):
self.get_submit().click()
# Not a good way to wait here, since all the elements we care about
# tracking don't exist yet.
time.sleep(1)
self.wait_until_exists('.poll-feedback')
self.assertTrue(self.browser.find_element_by_css_selector('.poll-feedback').text,
"Thank you\nfor being a valued student.")
......@@ -75,7 +73,133 @@ class TestPollFunctions(PollBaseTest):
self.get_submit().click()
# Button will be reaplaced with a new disabled copy, not just disabled.
# Button will be replaced with a new disabled copy, not just disabled.
self.wait_until_exists('input[name=poll-submit]:disabled')
self.go_to_page('Poll Functions')
self.assertFalse(self.get_submit().is_enabled())
class TestSurveyFunctions(PollBaseTest):
@staticmethod
def chunk_list(chunkable, max_size):
"""
Subdivides a list into several smaller lists.
"""
result = []
in_list = False
for index, item in enumerate(chunkable, start=1):
if not in_list:
result.append([])
in_list = True
result[-1].append(item)
if not index % max_size:
in_list = False
return result
def test_first_load(self):
"""
Checks the first load of the survey.
Verifies that the poll loads with the expected questions,
that the answers are shown in the expected order, that feedback is
not showing, and that the submit button is disabled.
"""
self.go_to_page('Survey Functions')
self.assertEqual(
[element.text for element in self.browser.find_elements_by_css_selector('.survey-question')],
[
"I feel like this test will pass.", "I like testing software", "Testing is not necessary",
"I would fake a test result to get software deployed."
]
)
self.assertEqual(
[element.text for element in self.browser.find_elements_by_css_selector('.survey-answer')],
[
"Strongly Agree", "Agree", "Neutral", "Disagree", "Strongly Disagree"
]
)
self.assertRaises(NoSuchElementException, self.browser.find_element_by_css_selector, '.poll-feedback')
submit_button = self.get_submit()
self.assertFalse(submit_button.is_enabled())
def fill_survey(self, assert_submit=False):
"""
Fills out the survey. Optionally checks if the submit button is
in the right state along the way.
"""
elements = self.browser.find_elements_by_css_selector('.survey-option input[type=radio]')
# Answers should be in sets of five.
questions = self.chunk_list(elements, 5)
# Disabled to start...
submit_button = self.get_submit()
if assert_submit:
self.assertFalse(submit_button.is_enabled())
# Strongly Agree: I feel like this test will pass.
questions[0][0].click()
if assert_submit:
self.assertFalse(submit_button.is_enabled())
# Disagree: Testing is not necessary
questions[2][3].click()
if assert_submit:
self.assertFalse(submit_button.is_enabled())
# Agree: I like testing software
questions[1][1].click()
if assert_submit:
self.assertFalse(submit_button.is_enabled())
# Strongly Disagree: I would fake a test result to get software deployed.
questions[3][4].click()
if assert_submit:
# Submit button should now be enabled!
self.assertTrue(submit_button.is_enabled())
def test_submit_enabled(self):
"""
Verify that the submit button is enabled only when every question
has an answer.
"""
self.go_to_page('Survey Functions')
self.fill_survey(assert_submit=True)
def test_survey_submission(self):
"""
Verify that the user can submit his or her vote and that the vote counts.
Also check that feedback is displayed afterward.
"""
self.go_to_page('Survey Functions')
self.fill_survey()
self.get_submit().click()
self.wait_until_exists('.poll-feedback')
self.assertEqual(self.browser.find_element_by_css_selector('.poll-footnote').text,
'Results gathered from 21 respondents.')
self.assertTrue(self.browser.find_element_by_css_selector('.poll-feedback').text,
"Thank you\nfor running the tests.")
def test_submit_not_enabled_on_revisit(self):
"""
Verify that revisiting the page post-vote does not re-enable the submit button.
"""
self.go_to_page('Survey Functions')
self.fill_survey()
self.get_submit().click()
# Button will be replaced with a new disabled copy, not just disabled.
self.wait_until_exists('input[name=poll-submit]:disabled')
self.go_to_page('Poll Functions')
......
<vertical_demo>
<survey tally='{"q1": {"sa": 5, "a": 5, "n": 3, "d": 2, "sd": 5}, "q2": {"sa": 3, "a": 2, "n": 3, "d": 10, "sd": 2}, "q3": {"sa": 2, "a": 7, "n": 1, "d": 4, "sd": 6}, "q4": {"sa": 1, "a": 2, "n": 8, "d": 4, "sd": 5}}'
questions='[["q1", "I feel like this test will pass."], ["q2", "I like testing software"], ["q3", "Testing is not necessary"], ["q4", "I would fake a test result to get software deployed."]]'
answers='[["sa", {"label": "Strongly Agree"}], ["a", {"label": "Agree"}], ["n", {"label": "Neutral"}], ["d", {"label": "Disagree"}], ["sd", {"label": "Strongly Disagree"}]]'
feedback="### Thank you&#10;&#10;for running the tests."/>
</vertical_demo>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment