def test_should_set_all_relationship_answers(self): # Given answer_state = create_answer() question_schema = Question() question_schema.answers = [answer_state.schema_item] relationship_state_question = RelationshipStateQuestion( 'relationship', question_schema) relationship_state_question.answers = [answer_state] with patch( 'app.questionnaire_state.relationship_state_question.get_answer_store' ) as get_answer_store: get_answer_store().filter = Mock( return_value=self.household_answers) # When relationship_state_question.update_state({ 'whos-related': 'Brother', 'whos-related_1': 'Sister' }) # Then self.assertEqual(len(relationship_state_question.children), 2) self.assertEqual( relationship_state_question.children[0].schema_item.widget.id, 'whos-related') self.assertEqual(relationship_state_question.children[0].value, 'Brother') self.assertEqual( relationship_state_question.children[1].schema_item.widget.id, 'whos-related_1') self.assertEqual(relationship_state_question.children[1].value, 'Sister')
def test_render_nested_templatable_property(self): question = Question() question.guidance = [{ 'title': 'Include', 'description': '{{someone_else}}', 'list': ['{{yourself}}', 'People here on holiday', ['{{someone}}']] }] context = { 'yourself': 'Joe Bloggs', 'someone': 'Jane Bloggs', 'someone_else': 'John Doe' } schema = TemplateRenderer().render_schema_items(question, context) expected = [{ 'title': 'Include', 'description': 'John Doe', 'list': ['Joe Bloggs', 'People here on holiday', ['Jane Bloggs']] }] self.assertEqual(schema.guidance, expected)
def test_render_schema_item(self): question = Question() question.title = 'Hello {{name}}' question.templatable_properties = ['title'] context = {'name': 'Joe Bloggs'} schema = TemplateRenderer().render_schema_items(question, context) self.assertEqual(schema.title, 'Hello Joe Bloggs')
def test_convert_answers(self): with self.application.test_request_context(): user_answer = [create_answer('ABC', '2016-01-01', group_id='group-1', block_id='block-1'), create_answer('DEF', '2016-03-30', group_id='group-1', block_id='block-1')] answer_1 = Answer() answer_1.id = "ABC" answer_1.code = "001" answer_2 = Answer() answer_2.id = "DEF" answer_2.code = "002" question = Question() question.id = 'question-1' question.add_answer(answer_1) question.add_answer(answer_2) section = Section() section.add_question(question) block = Block() block.id = 'block-1' block.add_section(section) group = Group() group.id = 'group-1' group.add_block(block) questionnaire = Questionnaire() questionnaire.survey_id = "021" questionnaire.data_version = "0.0.1" questionnaire.add_group(group) questionnaire.register(group) questionnaire.register(block) questionnaire.register(section) questionnaire.register(question) questionnaire.register(answer_1) questionnaire.register(answer_2) routing_path = [Location(group_id='group-1', group_instance=0, block_id='block-1')] answer_object = convert_answers(metadata, questionnaire, AnswerStore(user_answer), routing_path) self.assertEqual(answer_object['type'], 'uk.gov.ons.edc.eq:surveyresponse') self.assertEqual(answer_object['version'], '0.0.1') self.assertEqual(answer_object['origin'], 'uk.gov.ons.edc.eq') self.assertEqual(answer_object['survey_id'], '021') self.assertEqual(answer_object['collection']['exercise_sid'], metadata['collection_exercise_sid']) self.assertEqual(answer_object['collection']['instrument_id'], metadata['form_type']) self.assertEqual(answer_object['collection']['period'], metadata['period_id']) self.assertEqual(answer_object['metadata']['user_id'], metadata['user_id']) self.assertEqual(answer_object['metadata']['ru_ref'], metadata['ru_ref']) self.assertEqual(answer_object['data']['001'], '2016-01-01') self.assertEqual(answer_object['data']['002'], '2016-03-30')
def test_should_display_surname_if_no_first_name_provided(self): # Given answer_state = create_answer(group_instance=0) question_schema = Question() question_schema.answers = [answer_state.schema_item] relationship_state_question = RelationshipStateQuestion( 'relationship', question_schema) relationship_state_question.answers = [answer_state] first_names = [{ "answer_id": "first-name", "answer_instance": 0, "value": "John" }, { "answer_id": "first-name", "answer_instance": 1, "value": None }, { "answer_id": "first-name", "answer_instance": 2, "value": 'Joe' }] last_names = [{ "answer_id": "last-name", "answer_instance": 0, "value": None }, { "answer_id": "last-name", "answer_instance": 1, "value": 'Doe' }, { "answer_id": "last-name", "answer_instance": 2, "value": 'Bloggs' }] with patch( 'app.questionnaire_state.relationship_state_question.get_answer_store' ) as get_answer_store: get_answer_store().filter = Mock() get_answer_store().filter.side_effect = [first_names, last_names] # When relationship_state_question.update_state({}) # Then self.assertEqual( relationship_state_question.children[0].schema_item.widget. other_person, 'Doe')
def test_get_known_validators_by_type(self): # answer types item = Answer() item.type = 'integer' validators = TypeValidatorFactory.get_validators_by_type(item) self.assertEqual(len(validators), 1) self.assertTrue(isinstance(validators[0], IntegerTypeCheck)) item.type = 'date' validators = TypeValidatorFactory.get_validators_by_type(item) self.assertEqual(len(validators), 1) self.assertTrue(isinstance(validators[0], DateTypeCheck)) item.type = 'positiveinteger' validators = TypeValidatorFactory.get_validators_by_type(item) self.assertEqual(len(validators), 1) self.assertTrue(isinstance(validators[0], PositiveIntegerTypeCheck)) item.type = 'textarea' validators = TypeValidatorFactory.get_validators_by_type(item) self.assertEqual(len(validators), 1) self.assertTrue(isinstance(validators[0], TextAreaTypeCheck)) # question types item = Question() item.type = 'daterange' validators = TypeValidatorFactory.get_validators_by_type(item) self.assertEqual(len(validators), 1) self.assertTrue(isinstance(validators[0], DateRangeCheck))
def test_basics(self): question = Question() question.id = 'some-id' question.title = 'my question object' question.description = 'fill this in' answer1 = Answer() answer1.id = 'answer-1' answer2 = Answer() answer2.id = 'answer-2' question.add_answer(answer1) question.add_answer(answer2) self.assertEqual(question.id, 'some-id') self.assertEqual(question.title, 'my question object') self.assertEqual(question.description, 'fill this in') self.assertIsNone(question.container) self.assertEqual(len(question.answers), 2) self.assertEqual(question.answers[0], answer1) self.assertEqual(question.answers[1], answer2) self.assertEqual(answer1.container, question) self.assertEqual(answer2.container, question)
def test_should_create_relationship_answers_other_household_members(self): # Given answer_state = create_answer() question_schema = Question() question_schema.answers = [answer_state.schema_item] relationship_state_question = RelationshipStateQuestion( 'relationship', question_schema) relationship_state_question.answers = [answer_state] with patch( 'app.questionnaire_state.relationship_state_question.get_answer_store' ) as get_answer_store: get_answer_store().filter = Mock( return_value=self.household_answers) # When relationship_state_question.update_state({}) # Then self.assertEqual(len(relationship_state_question.answers), 2)
def test_should_not_have_answer_for_last_person(self): # Given answer_state = create_answer(group_instance=2) question_schema = Question() question_schema.answers = [answer_state.schema_item] relationship_state_question = RelationshipStateQuestion( 'relationship', question_schema) relationship_state_question.answers = [answer_state] with patch( 'app.questionnaire_state.relationship_state_question.get_answer_store' ) as get_answer_store: get_answer_store().filter = Mock( return_value=self.household_answers) # When relationship_state_question.update_state({}) # Then self.assertEqual(len(relationship_state_question.children), 0)
def test_unknown_validators(self): # answer types item = Answer() item.type = 'unknown_type' self.assertRaises(TypeValidatorFactoryException, TypeValidatorFactory.get_validators_by_type, item) # question types item = Question() item.type = 'unknown_type' self.assertRaises(TypeValidatorFactoryException, TypeValidatorFactory.get_validators_by_type, item)
def test_basics(self): question = Question() question.id = 'some-id' question.title = 'my question object' question.description = 'fill this in' answer1 = Answer() answer1.id = 'answer-1' answer2 = Answer() answer2.id = 'answer-2' question.add_answer(answer1) question.add_answer(answer2) self.assertEquals(question.id, 'some-id') self.assertEquals(question.title, 'my question object') self.assertEquals(question.description, 'fill this in') self.assertIsNone(question.container) self.assertEquals(len(question.answers), 2) self.assertEquals(question.answers[0], answer1) self.assertEquals(question.answers[1], answer2) self.assertEquals(answer1.container, question) self.assertEquals(answer2.container, question)
def test_answer_with_multiple_instances(self): with self.application.test_request_context(): user_answer = [create_answer('GHI', 0, group_id='group-1', block_id='block-1'), create_answer('GHI', value=1, answer_instance=1, group_id='group-1', block_id='block-1'), create_answer('GHI', value=2, answer_instance=2, group_id='group-1', block_id='block-1')] answer = Answer() answer.id = "GHI" answer.code = "003" question = Question() question.id = 'question-2' question.add_answer(answer) section = Section() section.add_question(question) block = Block() block.id = 'block-1' block.add_section(section) group = Group() group.id = 'group-1' group.add_block(block) questionnaire = Questionnaire() questionnaire.survey_id = "021" questionnaire.data_version = "0.0.1" questionnaire.add_group(group) questionnaire.register(question) questionnaire.register(answer) routing_path = [Location(group_id='group-1', group_instance=0, block_id='block-1')] answer_object = convert_answers(metadata, questionnaire, AnswerStore(user_answer), routing_path) # Check the converter correctly self.assertEqual(answer_object["data"]["003"], ['0', '1', '2'])
def test_basics(self): section = Section() section.id = 'some-id' section.title = 'my section object' question1 = Question() question1.id = 'question-1' question2 = Question() question2.id = 'question-2' section.add_question(question1) section.add_question(question2) self.assertEquals(section.id, 'some-id') self.assertEquals(section.title, 'my section object') self.assertIsNone(section.container) self.assertEquals(len(section.questions), 2) self.assertEquals(section.questions[0], question1) self.assertEquals(section.questions[1], question2) self.assertEquals(question1.container, section) self.assertEquals(question2.container, section)
def test_should_set_other_person_on_widget(self): # Given answer_state = create_answer(group_instance=1) question_schema = Question() question_schema.answers = [answer_state.schema_item] relationship_state_question = RelationshipStateQuestion( 'relationship', question_schema) relationship_state_question.answers = [answer_state] with patch( 'app.questionnaire_state.relationship_state_question.get_answer_store' ) as get_answer_store: get_answer_store().filter = Mock() get_answer_store().filter.side_effect = [ self.household_answers, self.last_names ] # When relationship_state_question.update_state({}) # Then self.assertEqual( relationship_state_question.children[0].schema_item.widget. other_person, 'Joe Bloggs')
def setUp(self): question = Question() question.skipped = False question.type = "General" question_state = StateQuestion('2', question) self.skip_mandatory_validation = False self.check_box_answer = CheckboxAnswer('1234') self.check_box_answer.type = "Checkbox" self.check_box_answer.mandatory = True self.check_box_answer.questionnaire = MagicMock() self.answer_state = StateAnswer('1', self.check_box_answer) self.answer_state.is_valid = True self.answer_state.parent = question_state self.answer_state.schema_item.options = [{ "label": "Option1", "value": "Option1" }, { "label": "Option2", "value": "Option2" }]
def test_basics(self): section = Section() section.id = 'some-id' section.title = 'my section object' question1 = Question() question1.id = 'question-1' question2 = Question() question2.id = 'question-2' section.add_question(question1) section.add_question(question2) self.assertEqual(section.id, 'some-id') self.assertEqual(section.title, 'my section object') self.assertIsNone(section.container) self.assertEqual(len(section.questions), 2) self.assertEqual(section.questions[0], question1) self.assertEqual(section.questions[1], question2) self.assertEqual(question1.container, section) self.assertEqual(question2.container, section)
def test_converter_checkboxes_with_q_codes_and_other_value(self): with self.application.test_request_context(): routing_path = [Location(group_id='favourite food', group_instance=0, block_id='crisps')] answers = [create_answer('crisps-answer', ['Ready salted', 'other', 'Bacon'], group_id='favourite food', block_id='crisps')] answer = CheckboxAnswer() answer.id = "crisps-answer" answer.code = "" answer.options = [ { "label": "Ready salted", "value": "Ready salted", "q_code": "1" }, { "label": "Sweet chilli", "value": "Sweet chilli", "q_code": "2" }, { "label": "Cheese and onion", "value": "Cheese and onion", "q_code": "3" }, { "label": "Other", "value": "other", "q_code": "4", "description": "Choose any other flavour", "other": { "label": "Please specify other" } } ] question = Question() question.id = 'crisps-question' question.add_answer(answer) section = Section() section.add_question(question) block = Block() block.id = 'crisps' block.add_section(section) group = Group() group.id = 'favourite food' group.add_block(block) questionnaire = Questionnaire() questionnaire.survey_id = "999" questionnaire.data_version = "0.0.1" questionnaire.add_group(group) questionnaire.register(question) questionnaire.register(answer) # When answer_object = convert_answers(metadata, questionnaire, AnswerStore(answers), routing_path) # Then self.assertEqual(len(answer_object['data']), 2) self.assertEqual(answer_object['data']['1'], 'Ready salted') self.assertEqual(answer_object['data']['4'], 'Bacon')