def test_parse_get_params(self): # We have to set up Django settings in order to use QueryDict from django.conf import settings settings.configure() # Valid GET param dict valid_get_dict = self._querydict_from_dict({ 'input_1': 'test', 'input_1_2': 'test', 'input_1_2_3': 'test', 'input_[]_3': 'test', 'input_4': None, 'input_5': [], 'input_6': 5 }) result = CapaModule.make_dict_of_responses(valid_get_dict) # Expect that we get a dict with "input" stripped from key names # and that we get the same values back for key in result.keys(): original_key = "input_" + key self.assertTrue(original_key in valid_get_dict, "Output dict should have key %s" % original_key) self.assertEqual(valid_get_dict[original_key], result[key]) # Valid GET param dict with list keys valid_get_dict = self._querydict_from_dict( {'input_2[]': ['test1', 'test2']}) result = CapaModule.make_dict_of_responses(valid_get_dict) self.assertTrue('2' in result) self.assertEqual(['test1', 'test2'], result['2']) # If we use [] at the end of a key name, we should always # get a list, even if there's just one value valid_get_dict = self._querydict_from_dict({'input_1[]': 'test'}) result = CapaModule.make_dict_of_responses(valid_get_dict) self.assertEqual(result['1'], ['test']) # If we have no underscores in the name, then the key is invalid invalid_get_dict = self._querydict_from_dict({'input': 'test'}) with self.assertRaises(ValueError): result = CapaModule.make_dict_of_responses(invalid_get_dict) # Two equivalent names (one list, one non-list) # One of the values would overwrite the other, so detect this # and raise an exception invalid_get_dict = self._querydict_from_dict({ 'input_1[]': 'test 1', 'input_1': 'test 2' }) with self.assertRaises(ValueError): result = CapaModule.make_dict_of_responses(invalid_get_dict)
def test_parse_get_params(self): # We have to set up Django settings in order to use QueryDict from django.conf import settings if not settings.configured: settings.configure() # Valid GET param dict valid_get_dict = self._querydict_from_dict( { "input_1": "test", "input_1_2": "test", "input_1_2_3": "test", "input_[]_3": "test", "input_4": None, "input_5": [], "input_6": 5, } ) result = CapaModule.make_dict_of_responses(valid_get_dict) # Expect that we get a dict with "input" stripped from key names # and that we get the same values back for key in result.keys(): original_key = "input_" + key self.assertTrue(original_key in valid_get_dict, "Output dict should have key %s" % original_key) self.assertEqual(valid_get_dict[original_key], result[key]) # Valid GET param dict with list keys valid_get_dict = self._querydict_from_dict({"input_2[]": ["test1", "test2"]}) result = CapaModule.make_dict_of_responses(valid_get_dict) self.assertTrue("2" in result) self.assertEqual(["test1", "test2"], result["2"]) # If we use [] at the end of a key name, we should always # get a list, even if there's just one value valid_get_dict = self._querydict_from_dict({"input_1[]": "test"}) result = CapaModule.make_dict_of_responses(valid_get_dict) self.assertEqual(result["1"], ["test"]) # If we have no underscores in the name, then the key is invalid invalid_get_dict = self._querydict_from_dict({"input": "test"}) with self.assertRaises(ValueError): result = CapaModule.make_dict_of_responses(invalid_get_dict) # Two equivalent names (one list, one non-list) # One of the values would overwrite the other, so detect this # and raise an exception invalid_get_dict = self._querydict_from_dict({"input_1[]": "test 1", "input_1": "test 2"}) with self.assertRaises(ValueError): result = CapaModule.make_dict_of_responses(invalid_get_dict)
def create(cls, max_attempts=None, attempts=None, correct=False, last_submission_time=None, submission_wait_seconds=None): """ Optional parameters here are cut down to what we actually use vs. the regular CapaFactory. """ location = BlockUsageLocator(CourseLocator('edX', 'capa_test', 'run', deprecated=True), 'problem', 'SampleProblem{0}'.format(cls.next_num()), deprecated=True) field_data = {'data': cls.sample_problem_xml} if max_attempts is not None: field_data['max_attempts'] = max_attempts if last_submission_time is not None: field_data['last_submission_time'] = last_submission_time if submission_wait_seconds is not None: field_data['submission_wait_seconds'] = submission_wait_seconds descriptor = Mock(weight="1") if attempts is not None: # converting to int here because I keep putting "0" and "1" in the tests # since everything else is a string. field_data['attempts'] = int(attempts) system = get_test_system() system.render_template = Mock( return_value="<div>Test Template HTML</div>") module = CapaModule( descriptor, system, DictFieldData(field_data), ScopeIds(None, None, location, location), ) if correct: # Could set the internal state formally, but here we just jam in the score. module.score = Score(raw_earned=1, raw_possible=1) else: module.score = Score(raw_earned=0, raw_possible=1) return module
def create(cls, max_attempts=None, attempts=None, correct=False, last_submission_time=None, submission_wait_seconds=None): """ Optional parameters here are cut down to what we actually use vs. the regular CapaFactory. """ location = Location([ "i4x", "edX", "capa_test", "problem", "SampleProblem{0}".format(cls.next_num()) ]) field_data = {'data': cls.sample_problem_xml} if max_attempts is not None: field_data['max_attempts'] = max_attempts if last_submission_time is not None: field_data['last_submission_time'] = last_submission_time if submission_wait_seconds is not None: field_data['submission_wait_seconds'] = submission_wait_seconds descriptor = Mock(weight="1") if attempts is not None: # converting to int here because I keep putting "0" and "1" in the tests # since everything else is a string. field_data['attempts'] = int(attempts) system = get_test_system() system.render_template = Mock( return_value="<div>Test Template HTML</div>") module = CapaModule( descriptor, system, DictFieldData(field_data), ScopeIds(None, None, location, location), ) if correct: # Could set the internal state formally, but here we just jam in the score. module.get_score = lambda: {'score': 1, 'total': 1} else: module.get_score = lambda: {'score': 0, 'total': 1} return module
def create( cls, max_attempts=None, attempts=None, correct=False, last_submission_time=None, submission_wait_seconds=None ): """ Optional parameters here are cut down to what we actually use vs. the regular CapaFactory. """ location = BlockUsageLocator(CourseLocator('edX', 'capa_test', 'run', deprecated=True), 'problem', 'SampleProblem{0}'.format(cls.next_num()), deprecated=True) field_data = {'data': cls.sample_problem_xml} if max_attempts is not None: field_data['max_attempts'] = max_attempts if last_submission_time is not None: field_data['last_submission_time'] = last_submission_time if submission_wait_seconds is not None: field_data['submission_wait_seconds'] = submission_wait_seconds descriptor = Mock(weight="1") if attempts is not None: # converting to int here because I keep putting "0" and "1" in the tests # since everything else is a string. field_data['attempts'] = int(attempts) system = get_test_system() system.render_template = Mock(return_value="<div>Test Template HTML</div>") module = CapaModule( descriptor, system, DictFieldData(field_data), ScopeIds(None, None, location, location), ) if correct: # Could set the internal state formally, but here we just jam in the score. module.score = Score(raw_earned=1, raw_possible=1) else: module.score = Score(raw_earned=0, raw_possible=1) return module
def create( cls, max_attempts=None, attempts=None, correct=False, last_submission_time=None, submission_wait_seconds=None ): """ Optional parameters here are cut down to what we actually use vs. the regular CapaFactory. """ location = Location(["i4x", "edX", "capa_test", "problem", "SampleProblem{0}".format(cls.next_num())]) field_data = {'data': cls.sample_problem_xml} if max_attempts is not None: field_data['max_attempts'] = max_attempts if last_submission_time is not None: field_data['last_submission_time'] = last_submission_time if submission_wait_seconds is not None: field_data['submission_wait_seconds'] = submission_wait_seconds descriptor = Mock(weight="1") if attempts is not None: # converting to int here because I keep putting "0" and "1" in the tests # since everything else is a string. field_data['attempts'] = int(attempts) system = get_test_system() system.render_template = Mock(return_value="<div>Test Template HTML</div>") module = CapaModule( descriptor, system, DictFieldData(field_data), ScopeIds(None, None, location, location), ) if correct: # Could set the internal state formally, but here we just jam in the score. module.get_score = lambda: {'score': 1, 'total': 1} else: module.get_score = lambda: {'score': 0, 'total': 1} return module
def create(graceperiod=None, due=None, max_attempts=None, showanswer=None, rerandomize=None, force_save_button=None, attempts=None, problem_state=None, correct=False, done=None ): """ All parameters are optional, and are added to the created problem if specified. Arguments: graceperiod: due: max_attempts: showanswer: force_save_button: rerandomize: all strings, as specified in the policy for the problem problem_state: a dict to to be serialized into the instance_state of the module. attempts: also added to instance state. Will be converted to an int. """ location = Location(["i4x", "edX", "capa_test", "problem", "SampleProblem{0}".format(CapaFactory.next_num())]) model_data = {'data': CapaFactory.sample_problem_xml} if graceperiod is not None: model_data['graceperiod'] = graceperiod if due is not None: model_data['due'] = due if max_attempts is not None: model_data['max_attempts'] = max_attempts if showanswer is not None: model_data['showanswer'] = showanswer if force_save_button is not None: model_data['force_save_button'] = force_save_button if rerandomize is not None: model_data['rerandomize'] = rerandomize if done is not None: model_data['done'] = done descriptor = Mock(weight="1") if problem_state is not None: model_data.update(problem_state) if attempts is not None: # converting to int here because I keep putting "0" and "1" in the tests # since everything else is a string. model_data['attempts'] = int(attempts) system = test_system() system.render_template = Mock(return_value="<div>Test Template HTML</div>") module = CapaModule(system, location, descriptor, model_data) if correct: # TODO: probably better to actually set the internal state properly, but... module.get_score = lambda: {'score': 1, 'total': 1} else: module.get_score = lambda: {'score': 0, 'total': 1} return module
def create( graceperiod=None, due=None, max_attempts=None, showanswer=None, rerandomize=None, force_save_button=None, attempts=None, problem_state=None, correct=False, done=None, text_customization=None, ): """ All parameters are optional, and are added to the created problem if specified. Arguments: graceperiod: due: max_attempts: showanswer: force_save_button: rerandomize: all strings, as specified in the policy for the problem problem_state: a dict to to be serialized into the instance_state of the module. attempts: also added to instance state. Will be converted to an int. """ location = Location(["i4x", "edX", "capa_test", "problem", "SampleProblem{0}".format(CapaFactory.next_num())]) field_data = {"data": CapaFactory.sample_problem_xml} if graceperiod is not None: field_data["graceperiod"] = graceperiod if due is not None: field_data["due"] = due if max_attempts is not None: field_data["max_attempts"] = max_attempts if showanswer is not None: field_data["showanswer"] = showanswer if force_save_button is not None: field_data["force_save_button"] = force_save_button if rerandomize is not None: field_data["rerandomize"] = rerandomize if done is not None: field_data["done"] = done if text_customization is not None: field_data["text_customization"] = text_customization descriptor = Mock(weight="1") if problem_state is not None: field_data.update(problem_state) if attempts is not None: # converting to int here because I keep putting "0" and "1" in the tests # since everything else is a string. field_data["attempts"] = int(attempts) system = get_test_system() system.render_template = Mock(return_value="<div>Test Template HTML</div>") module = CapaModule(descriptor, system, DictFieldData(field_data), ScopeIds(None, None, location, location)) if correct: # TODO: probably better to actually set the internal state properly, but... module.get_score = lambda: {"score": 1, "total": 1} else: module.get_score = lambda: {"score": 0, "total": 1} return module