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_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_get_known_validators_by_type(self):
        # answer types
        item = Answer()
        item.type = 'integer'
        validators = TypeValidatorFactory.get_validators_by_type(item)

        self.assertEquals(len(validators), 1)
        self.assertTrue(isinstance(validators[0], IntegerTypeCheck))

        item.type = 'date'
        validators = TypeValidatorFactory.get_validators_by_type(item)

        self.assertEquals(len(validators), 1)
        self.assertTrue(isinstance(validators[0], DateTypeCheck))

        item.type = 'positiveinteger'
        validators = TypeValidatorFactory.get_validators_by_type(item)

        self.assertEquals(len(validators), 1)
        self.assertTrue(isinstance(validators[0], PositiveIntegerTypeCheck))

        item.type = 'textarea'
        validators = TypeValidatorFactory.get_validators_by_type(item)

        self.assertEquals(len(validators), 1)
        self.assertTrue(isinstance(validators[0], TextAreaTypeCheck))

        # question types
        item = Question()

        item.type = 'daterange'
        validators = TypeValidatorFactory.get_validators_by_type(item)

        self.assertEquals(len(validators), 1)
        self.assertTrue(isinstance(validators[0], DateRangeCheck))
Esempio n. 4
0
    def test_answer_with_zero(self):
        with self.application.test_request_context():
            user_answer = [create_answer('GHI', 0, 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("0", answer_object["data"]["003"])
Esempio n. 5
0
 def setUp(self):
     self.answer_schema = Answer('multiple-choice-with-other')
     self.answer_schema.type = 'Radio'
     self.answer_schema.widget = TextWidget(self.answer_schema.id)
     self.answer_schema.options = [
         {
             "label": "Yes",
             "value": "Yes"
         },
         {
             "label": "No",
             "value": "No",
             "other": {
                 "label": "Please enter the country of usual residence"
             }
         }
     ]
    def test_basics(self):
        answer = Answer()

        answer.id = 'some-id'
        answer.label = 'my answer object'
        answer.guidance = 'fill this in'
        answer.type = 'some-type'
        answer.code = 'code'
        answer.container = None

        self.assertEquals(answer.id, 'some-id')
        self.assertEquals(answer.label, 'my answer object')
        self.assertEquals(answer.guidance, 'fill this in')
        self.assertEquals(answer.type, 'some-type')
        self.assertEquals(answer.code, 'code')
        self.assertIsNone(answer.container)
Esempio n. 7
0
    def test_update_state_initialise_repeating(self):

        question_state = RepeatingAnswerStateQuestion('question_id',
                                                      MagicMock())
        question_state.schema_item.type = 'RepeatingAnswer'
        question_state.schema_item.answers = [Answer('answer_id')]
        question_state.schema_item.answers[0].widget = TextWidget('answer_id')

        answer_state = MagicMock()
        answer_state.id = 'answer_id'
        answer_state.answer_instance = 0

        question_state.answers.append(answer_state)
        question_state.update_state({
            'answer_id': 'answer_value',
            'answer_id_1': 'answer_value_1'
        })
        self.assertEqual(len(question_state.answers), 2)
Esempio n. 8
0
    def test_answers_grouped_by_instance_id_odd_number_of_instances(self):
        # Given
        question_state = RepeatingAnswerStateQuestion('question_id',
                                                      MagicMock())

        first_name_schema = Answer('first-name')
        first_name_schema.widget = TextWidget('first-name')

        last_name_schema = Answer('last-name')
        last_name_schema.widget = TextWidget('last-name')

        question_state.schema_item.answers = [
            first_name_schema, last_name_schema
        ]

        first_name = MagicMock()
        first_name.id = 'first-name'
        first_name.answer_instance = 0

        last_name = MagicMock()
        last_name.id = 'last-name'
        last_name.answer_instance = 0

        first_name1 = MagicMock()
        first_name1.id = 'first-name'
        first_name1.answer_instance = 1

        question_state.answers = [first_name, last_name, first_name1]

        # When
        result = question_state.answers_grouped_by_instance()

        # Then
        self.assertEqual(len(result), 2)

        # Instance 0
        self.assertEqual(len(result[0]), 2)
        self.assertEqual(result[0][0], first_name)
        self.assertEqual(result[0][1], last_name)

        # Instance 1
        self.assertEqual(len(result[1]), 1)
        self.assertEqual(result[1][0], first_name1)
Esempio n. 9
0
 def check_user_input(user_input):
     return user_input if (Answer.check_user_input(user_input)
                           and user_input != ['']
                           and user_input != [None]) else None
 def check_user_input(user_input):
     return user_input if (Answer.check_user_input(user_input) and
                           user_input != [''] and
                           user_input != [None]) else None
Esempio n. 11
0
    def test_answers_grouped_by_instance_id_many_answers_many_instance(self):  # pylint: disable=too-many-statements
        # Given
        question_state = RepeatingAnswerStateQuestion('question_id',
                                                      MagicMock())

        first_name_schema = Answer('first-name')
        first_name_schema.widget = TextWidget('first-name')

        middle_name_schema = Answer('middle-names')
        middle_name_schema.widget = TextWidget('middle-names')

        last_name_schema = Answer('last-name')
        last_name_schema.widget = TextWidget('last-name')

        question_state.schema_item.answers = [
            first_name_schema, middle_name_schema, last_name_schema
        ]

        first_name = MagicMock()
        first_name.id = 'first-name'
        first_name.answer_instance = 0

        middle_name = MagicMock()
        middle_name.id = 'middle-names'
        middle_name.answer_instance = 0

        last_name = MagicMock()
        last_name.id = 'last-name'
        last_name.answer_instance = 0

        first_name1 = MagicMock()
        first_name1.id = 'first-name'
        first_name1.answer_instance = 1

        middle_name1 = MagicMock()
        middle_name1.id = 'middle-names'
        middle_name1.answer_instance = 1

        last_name1 = MagicMock()
        last_name1.id = 'last-name'
        last_name1.answer_instance = 1

        first_name2 = MagicMock()
        first_name2.id = 'first-name'
        first_name2.answer_instance = 2

        middle_name2 = MagicMock()
        middle_name2.id = 'middle-names'
        middle_name2.answer_instance = 2

        last_name2 = MagicMock()
        last_name2.id = 'last-name'
        last_name2.answer_instance = 2

        question_state.answers = [
            first_name,
            middle_name,
            last_name,
            first_name1,
            middle_name1,
            last_name1,
            first_name2,
            middle_name2,
            last_name2,
        ]

        # When
        result = question_state.answers_grouped_by_instance()

        # Then
        self.assertEqual(len(result), 3)

        # Instance 0
        self.assertEqual(len(result[0]), 3)
        self.assertEqual(result[0][0], first_name)
        self.assertEqual(result[0][1], middle_name)
        self.assertEqual(result[0][2], last_name)

        # Instance 1
        self.assertEqual(len(result[1]), 3)
        self.assertEqual(result[1][0], first_name1)
        self.assertEqual(result[1][1], middle_name1)
        self.assertEqual(result[1][2], last_name1)

        # Instance 2
        self.assertEqual(len(result[2]), 3)
        self.assertEqual(result[2][0], first_name2)
        self.assertEqual(result[2][1], middle_name2)
        self.assertEqual(result[2][2], last_name2)
def create_answer(group_instance=0):
    answer = Answer('whos-related')
    answer.widget = TextWidget('whos-related')
    answer_state = answer.construct_state()
    answer_state.group_instance = group_instance
    return answer_state