Commit a9a8dcf8 by Calen Pennington

Allow acceptance test problem functions to work without hardcoded course name

parent 77942cec
...@@ -72,7 +72,7 @@ def input_problem_answer(_, problem_type, correctness): ...@@ -72,7 +72,7 @@ def input_problem_answer(_, problem_type, correctness):
""" """
assert(correctness in ['correct', 'incorrect']) assert(correctness in ['correct', 'incorrect'])
assert(problem_type in PROBLEM_DICT) assert(problem_type in PROBLEM_DICT)
answer_problem(problem_type, correctness) answer_problem(world.scenario_dict['COURSE'].number, problem_type, correctness)
@step(u'I check a problem') @step(u'I check a problem')
...@@ -98,7 +98,7 @@ def assert_problem_has_answer(step, problem_type, answer_class): ...@@ -98,7 +98,7 @@ def assert_problem_has_answer(step, problem_type, answer_class):
''' '''
assert answer_class in ['correct', 'incorrect', 'blank'] assert answer_class in ['correct', 'incorrect', 'blank']
assert problem_type in PROBLEM_DICT assert problem_type in PROBLEM_DICT
problem_has_answer(problem_type, answer_class) problem_has_answer(world.scenario_dict['COURSE'].number, problem_type, answer_class)
@step(u'I reset the problem') @step(u'I reset the problem')
......
...@@ -162,46 +162,48 @@ PROBLEM_DICT = { ...@@ -162,46 +162,48 @@ PROBLEM_DICT = {
} }
def answer_problem(problem_type, correctness): def answer_problem(course, problem_type, correctness):
# Make sure that the problem has been completely rendered before # Make sure that the problem has been completely rendered before
# starting to input an answer. # starting to input an answer.
world.wait_for_ajax_complete() world.wait_for_ajax_complete()
section_loc = section_location(course)
if problem_type == "drop down": if problem_type == "drop down":
select_name = "input_i4x-edx-model_course-problem-drop_down_2_1" select_name = "input_i4x-{0.org}-{0.course}-problem-drop_down_2_1".format(section_loc)
option_text = 'Option 2' if correctness == 'correct' else 'Option 3' option_text = 'Option 2' if correctness == 'correct' else 'Option 3'
world.select_option(select_name, option_text) world.select_option(select_name, option_text)
elif problem_type == "multiple choice": elif problem_type == "multiple choice":
if correctness == 'correct': if correctness == 'correct':
world.css_check(inputfield('multiple choice', choice='choice_2')) world.css_check(inputfield(course, 'multiple choice', choice='choice_2'))
else: else:
world.css_check(inputfield('multiple choice', choice='choice_1')) world.css_check(inputfield(course, 'multiple choice', choice='choice_1'))
elif problem_type == "checkbox": elif problem_type == "checkbox":
if correctness == 'correct': if correctness == 'correct':
world.css_check(inputfield('checkbox', choice='choice_0')) world.css_check(inputfield(course, 'checkbox', choice='choice_0'))
world.css_check(inputfield('checkbox', choice='choice_2')) world.css_check(inputfield(course, 'checkbox', choice='choice_2'))
else: else:
world.css_check(inputfield('checkbox', choice='choice_3')) world.css_check(inputfield(course, 'checkbox', choice='choice_3'))
elif problem_type == 'radio': elif problem_type == 'radio':
if correctness == 'correct': if correctness == 'correct':
world.css_check(inputfield('radio', choice='choice_2')) world.css_check(inputfield(course, 'radio', choice='choice_2'))
else: else:
world.css_check(inputfield('radio', choice='choice_1')) world.css_check(inputfield(course, 'radio', choice='choice_1'))
elif problem_type == 'string': elif problem_type == 'string':
textvalue = 'correct string' if correctness == 'correct' else 'incorrect' textvalue = 'correct string' if correctness == 'correct' else 'incorrect'
world.css_fill(inputfield('string'), textvalue) world.css_fill(inputfield(course, 'string'), textvalue)
elif problem_type == 'numerical': elif problem_type == 'numerical':
textvalue = "pi + 1" if correctness == 'correct' else str(random.randint(-2, 2)) textvalue = "pi + 1" if correctness == 'correct' else str(random.randint(-2, 2))
world.css_fill(inputfield('numerical'), textvalue) world.css_fill(inputfield(course, 'numerical'), textvalue)
elif problem_type == 'formula': elif problem_type == 'formula':
textvalue = "x^2+2*x+y" if correctness == 'correct' else 'x^2' textvalue = "x^2+2*x+y" if correctness == 'correct' else 'x^2'
world.css_fill(inputfield('formula'), textvalue) world.css_fill(inputfield(course, 'formula'), textvalue)
elif problem_type == 'script': elif problem_type == 'script':
# Correct answer is any two integers that sum to 10 # Correct answer is any two integers that sum to 10
...@@ -213,8 +215,8 @@ def answer_problem(problem_type, correctness): ...@@ -213,8 +215,8 @@ def answer_problem(problem_type, correctness):
if correctness == 'incorrect': if correctness == 'incorrect':
second_addend += random.randint(1, 10) second_addend += random.randint(1, 10)
world.css_fill(inputfield('script', input_num=1), str(first_addend)) world.css_fill(inputfield(course, 'script', input_num=1), str(first_addend))
world.css_fill(inputfield('script', input_num=2), str(second_addend)) world.css_fill(inputfield(course, 'script', input_num=2), str(second_addend))
elif problem_type == 'code': elif problem_type == 'code':
# The fake xqueue server is configured to respond # The fake xqueue server is configured to respond
...@@ -233,15 +235,16 @@ def answer_problem(problem_type, correctness): ...@@ -233,15 +235,16 @@ def answer_problem(problem_type, correctness):
choice = "choiceinput_0bc" if correctness == 'correct' else "choiceinput_1bc" choice = "choiceinput_0bc" if correctness == 'correct' else "choiceinput_1bc"
world.css_fill( world.css_fill(
inputfield( inputfield(
course,
problem_type, problem_type,
choice="choiceinput_0_numtolerance_input_0" choice="choiceinput_0_numtolerance_input_0"
), ),
input_value input_value
) )
world.css_check(inputfield(problem_type, choice=choice)) world.css_check(inputfield(course, problem_type, choice=choice))
def problem_has_answer(problem_type, answer_class): def problem_has_answer(course, problem_type, answer_class):
if problem_type == "drop down": if problem_type == "drop down":
if answer_class == 'blank': if answer_class == 'blank':
assert world.is_css_not_present('option[selected="true"]') assert world.is_css_not_present('option[selected="true"]')
...@@ -252,52 +255,52 @@ def problem_has_answer(problem_type, answer_class): ...@@ -252,52 +255,52 @@ def problem_has_answer(problem_type, answer_class):
elif problem_type == "multiple choice": elif problem_type == "multiple choice":
if answer_class == 'correct': if answer_class == 'correct':
assert_checked('multiple choice', ['choice_2']) assert_checked(course, 'multiple choice', ['choice_2'])
elif answer_class == 'incorrect': elif answer_class == 'incorrect':
assert_checked('multiple choice', ['choice_1']) assert_checked(course, 'multiple choice', ['choice_1'])
else: else:
assert_checked('multiple choice', []) assert_checked(course, 'multiple choice', [])
elif problem_type == "checkbox": elif problem_type == "checkbox":
if answer_class == 'correct': if answer_class == 'correct':
assert_checked('checkbox', ['choice_0', 'choice_2']) assert_checked(course, 'checkbox', ['choice_0', 'choice_2'])
elif answer_class == 'incorrect': elif answer_class == 'incorrect':
assert_checked('checkbox', ['choice_3']) assert_checked(course, 'checkbox', ['choice_3'])
else: else:
assert_checked('checkbox', []) assert_checked(course, 'checkbox', [])
elif problem_type == "radio": elif problem_type == "radio":
if answer_class == 'correct': if answer_class == 'correct':
assert_checked('radio', ['choice_2']) assert_checked(course, 'radio', ['choice_2'])
elif answer_class == 'incorrect': elif answer_class == 'incorrect':
assert_checked('radio', ['choice_1']) assert_checked(course, 'radio', ['choice_1'])
else: else:
assert_checked('radio', []) assert_checked(course, 'radio', [])
elif problem_type == 'string': elif problem_type == 'string':
if answer_class == 'blank': if answer_class == 'blank':
expected = '' expected = ''
else: else:
expected = 'correct string' if answer_class == 'correct' else 'incorrect' expected = 'correct string' if answer_class == 'correct' else 'incorrect'
assert_textfield('string', expected) assert_textfield(course, 'string', expected)
elif problem_type == 'formula': elif problem_type == 'formula':
if answer_class == 'blank': if answer_class == 'blank':
expected = '' expected = ''
else: else:
expected = "x^2+2*x+y" if answer_class == 'correct' else 'x^2' expected = "x^2+2*x+y" if answer_class == 'correct' else 'x^2'
assert_textfield('formula', expected) assert_textfield(course, 'formula', expected)
elif problem_type in ("radio_text", "checkbox_text"): elif problem_type in ("radio_text", "checkbox_text"):
if answer_class == 'blank': if answer_class == 'blank':
expected = ('', '') expected = ('', '')
assert_choicetext_values(problem_type, (), expected) assert_choicetext_values(course, problem_type, (), expected)
elif answer_class == 'incorrect': elif answer_class == 'incorrect':
expected = ('5', '') expected = ('5', '')
assert_choicetext_values(problem_type, ["choiceinput_1bc"], expected) assert_choicetext_values(course, problem_type, ["choiceinput_1bc"], expected)
else: else:
expected = ('8', '') expected = ('8', '')
assert_choicetext_values(problem_type, ["choiceinput_0bc"], expected) assert_choicetext_values(course, problem_type, ["choiceinput_0bc"], expected)
else: else:
# The other response types use random data, # The other response types use random data,
...@@ -325,14 +328,16 @@ def add_problem_to_course(course, problem_type, extra_meta=None): ...@@ -325,14 +328,16 @@ def add_problem_to_course(course, problem_type, extra_meta=None):
# We set rerandomize=always in the metadata so that the "Reset" button # We set rerandomize=always in the metadata so that the "Reset" button
# will appear. # will appear.
category_name = "problem" category_name = "problem"
return world.ItemFactory.create(parent_location=section_location(course), return world.ItemFactory.create(
category=category_name, parent_location=section_location(course),
display_name=str(problem_type), category=category_name,
data=problem_xml, display_name=str(problem_type),
metadata=metadata) data=problem_xml,
metadata=metadata
)
def inputfield(problem_type, choice=None, input_num=1): def inputfield(course, problem_type, choice=None, input_num=1):
""" Return the css selector for `problem_type`. """ Return the css selector for `problem_type`.
For example, if problem_type is 'string', return For example, if problem_type is 'string', return
the text field for the string problem in the test course. the text field for the string problem in the test course.
...@@ -340,14 +345,20 @@ def inputfield(problem_type, choice=None, input_num=1): ...@@ -340,14 +345,20 @@ def inputfield(problem_type, choice=None, input_num=1):
`choice` is the name of the checkbox input in a group `choice` is the name of the checkbox input in a group
of checkboxes. """ of checkboxes. """
sel = ("input#input_i4x-edx-model_course-problem-%s_2_%s" % section_loc = section_location(course)
(problem_type.replace(" ", "_"), str(input_num)))
# this is necessary due to naming requirement for this problem type # this is necessary due to naming requirement for this problem type
if problem_type in ("radio_text", "checkbox_text"): if problem_type in ("radio_text", "checkbox_text"):
sel = "input#i4x-edx-model_course-problem-{0}_2_{1}".format( selector_template = "input#i4x-{org}-{course}-problem-{ptype}_2_{input}"
problem_type.replace(" ", "_"), str(input_num) else:
) selector_template = "input#input_i4x-{org}-{course}-problem-{ptype}_2_{input}"
sel = selector_template.format(
org=section_loc.org,
course=section_loc.course,
ptype=problem_type.replace(" ", "_"),
input=input_num,
)
if choice is not None: if choice is not None:
base = "_choice_" if problem_type == "multiple choice" else "_" base = "_choice_" if problem_type == "multiple choice" else "_"
...@@ -360,7 +371,7 @@ def inputfield(problem_type, choice=None, input_num=1): ...@@ -360,7 +371,7 @@ def inputfield(problem_type, choice=None, input_num=1):
return sel return sel
def assert_checked(problem_type, choices): def assert_checked(course, problem_type, choices):
''' '''
Assert that choice names given in *choices* are the only Assert that choice names given in *choices* are the only
ones checked. ones checked.
...@@ -371,7 +382,7 @@ def assert_checked(problem_type, choices): ...@@ -371,7 +382,7 @@ def assert_checked(problem_type, choices):
all_choices = ['choice_0', 'choice_1', 'choice_2', 'choice_3'] all_choices = ['choice_0', 'choice_1', 'choice_2', 'choice_3']
for this_choice in all_choices: for this_choice in all_choices:
def check_problem(): def check_problem():
element = world.css_find(inputfield(problem_type, choice=this_choice)) element = world.css_find(inputfield(course, problem_type, choice=this_choice))
if this_choice in choices: if this_choice in choices:
assert element.checked assert element.checked
else: else:
...@@ -379,12 +390,12 @@ def assert_checked(problem_type, choices): ...@@ -379,12 +390,12 @@ def assert_checked(problem_type, choices):
world.retry_on_exception(check_problem) world.retry_on_exception(check_problem)
def assert_textfield(problem_type, expected_text, input_num=1): def assert_textfield(course, problem_type, expected_text, input_num=1):
element_value = world.css_value(inputfield(problem_type, input_num=input_num)) element_value = world.css_value(inputfield(course, problem_type, input_num=input_num))
assert element_value == expected_text assert element_value == expected_text
def assert_choicetext_values(problem_type, choices, expected_values): def assert_choicetext_values(course, problem_type, choices, expected_values):
""" """
Asserts that only the given choices are checked, and given Asserts that only the given choices are checked, and given
text fields have a desired value text fields have a desired value
...@@ -397,7 +408,7 @@ def assert_choicetext_values(problem_type, choices, expected_values): ...@@ -397,7 +408,7 @@ def assert_choicetext_values(problem_type, choices, expected_values):
"choiceinput_1_numtolerance_input_0" "choiceinput_1_numtolerance_input_0"
] ]
for this_choice in all_choices: for this_choice in all_choices:
element = world.css_find(inputfield(problem_type, choice=this_choice)) element = world.css_find(inputfield(course, problem_type, choice=this_choice))
if this_choice in choices: if this_choice in choices:
assert element.checked assert element.checked
...@@ -405,6 +416,6 @@ def assert_choicetext_values(problem_type, choices, expected_values): ...@@ -405,6 +416,6 @@ def assert_choicetext_values(problem_type, choices, expected_values):
assert not element.checked assert not element.checked
for (name, expected) in zip(all_inputs, expected_values): for (name, expected) in zip(all_inputs, expected_values):
element = world.css_find(inputfield(problem_type, name)) element = world.css_find(inputfield(course, problem_type, name))
# Remove any trailing spaces that may have been added # Remove any trailing spaces that may have been added
assert element.value.strip() == expected assert element.value.strip() == expected
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