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 _parse_section(self, schema, questionnaire): """Parse a section element :param schema: The section schema :returns: A Section object :raises: SchemaParserException """ section = Section() section.id = ParserUtils.get_required_string(schema, "id") section.title = ParserUtils.get_optional_string(schema, "title") section.number = ParserUtils.get_optional_string(schema, "number") section.description = ParserUtils.get_optional_string( schema, "description") questionnaire.register(section) if 'questions' in schema.keys(): for question_schema in schema['questions']: section.add_question( self._parse_question(question_schema, questionnaire)) else: raise SchemaParserException( 'Section must have at least one question') return section
def test_set_skipped_skips_when_not_equals_satisfied(self): answers = {'12345': 'yes'} skip = { "when": [{ "id": "12345", "condition": "not equals", "value": "no" }] } section = Section() section.skip_condition = skip state_item = StateItem(item_id='', schema_item=section) state_item.set_skipped(answers, {}) self.assertEqual(state_item.skipped, True)
def test_set_skipped_skips_when_meta_equals_satisfied(self): metadata = {'region_code': 'GB-WLS'} skip = { "when": [{ "meta": "region_code", "condition": "equals", "value": "GB-WLS" }] } section = Section() section.skip_condition = skip state_item = StateItem(item_id='', schema_item=section) state_item.set_skipped({}, metadata) self.assertEqual(state_item.skipped, True)
def _parse_section(self, schema, questionnaire): """Parse a section element :param schema: The section schema :returns: A Section object :raises: SchemaParserException """ section = Section() try: section.id = ParserUtils.get_required_string(schema, "id") section.title = ParserUtils.get_optional_string(schema, "title") section.description = ParserUtils.get_optional_string(schema, "description") # regisger the section questionnaire.register(section) except Exception as e: logging.error('Error parsing schema') logging.info(e) raise e if 'questions' in schema.keys(): for question_schema in schema['questions']: section.add_question(self._parse_question(question_schema, questionnaire)) else: raise SchemaParserException('Section must have at least one question') return section
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): block = Block() block.id = 'some-id' block.title = 'my block object' section1 = Section() section1.id = 'section-1' section2 = Section() section2.id = 'section-2' block.add_section(section1) block.add_section(section2) self.assertEquals(block.id, 'some-id') self.assertEquals(block.title, 'my block object') self.assertIsNone(block.container) self.assertEquals(len(block.sections), 2) self.assertEquals(block.sections[0], section1) self.assertEquals(block.sections[1], section2) self.assertEquals(section1.container, block) self.assertEquals(section2.container, block)
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_basics(self): block = Block() block.id = 'some-id' block.title = 'my block object' section1 = Section() section1.id = 'section-1' section2 = Section() section2.id = 'section-2' block.add_section(section1) block.add_section(section2) self.assertEqual(block.id, 'some-id') self.assertEqual(block.title, 'my block object') self.assertIsNone(block.container) self.assertEqual(len(block.sections), 2) self.assertEqual(block.sections[0], section1) self.assertEqual(block.sections[1], section2) self.assertEqual(section1.container, block) self.assertEqual(section2.container, block)
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')