Ejemplo n.º 1
0
    def test_update_state_multiple_answers_in_schema(self):

        question = RepeatingAnswerStateQuestion('question_id', MagicMock())
        question.schema_item.type = 'RepeatingAnswer'

        answer1 = Answer('answer_one')
        answer2 = Answer('answer_two')

        answer1.widget = TextWidget('answer_one')
        answer2.widget = TextWidget('answer_two')

        question.schema_item.answers = [answer1, answer2]

        question.update_state({
            'answer_one': 'answer_one_value',
            'answer_one_1': 'answer_one_value_1',
            'answer_two': 'answer_two_value',
        })

        self.assertEqual(len(question.children), 3)

        question.update_state({
            'answer_one': 'answer_one_value',
            'answer_two': 'answer_two_value',
            'answer_two_3': 'answer_two_value_3',
            'answer_two_2': 'answer_two_value_2',
        })

        self.assertEqual(len(question.children), 5)
Ejemplo n.º 2
0
    def test_update_state_multiple_repeating_answers(self):

        question_state = RepeatingAnswerStateQuestion('question_id',
                                                      MagicMock())
        question_state.schema_item.type = 'RepeatingAnswer'

        answers = []
        for x in range(3):
            answer = Answer('answer' + str(x))
            answer.widget = TextWidget('answer' + str(x))
            answers.append(answer)
        question_state.schema_item.answers = answers

        answer1 = StateAnswer('answer0', MagicMock())
        answer2 = StateAnswer('answer1', MagicMock())
        answer3 = StateAnswer('answer2', MagicMock())

        question_state.answers = [answer1, answer2, answer3]

        question_state.update_state({
            'answer0': 'answer_one_value',
            'answer0_1': 'answer_one_value_1',
            'answer1': 'answer_two_value',
            'answer1_1': 'answer_two_value_1',
            'answer1_2': 'answer_two_value_2',
            'answer2': 'answer_three_value',
        })

        self.assertEqual(len(question_state.answers), 6)
Ejemplo n.º 3
0
    def test_answers_grouped_by_instance_id_one_answer_many_instances(self):
        # Given
        question_state = RepeatingAnswerStateQuestion('question_id',
                                                      MagicMock())

        schema_answer = Answer('answer')
        schema_answer.widget = TextWidget('answer')
        question_state.schema_item.answers = [schema_answer]

        state_answer = MagicMock()
        state_answer.id = 'answer'
        state_answer.answer_instance = 0

        state_answer1 = MagicMock()
        state_answer1.id = 'answer'
        state_answer1.answer_instance = 1

        question_state.answers = [state_answer, state_answer1]

        # When
        result = question_state.answers_grouped_by_instance()

        # Then
        self.assertEqual(len(result), 2)
        self.assertEqual(len(result[0]), 1)
        self.assertEqual(result[0][0], state_answer)
        self.assertEqual(len(result[1]), 1)
        self.assertEqual(result[1][0], state_answer1)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def test_answers_grouped_by_instance_id_many_answers_one_instance(self):
        # 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

        question_state.answers = [first_name, middle_name, last_name]

        # When
        result = question_state.answers_grouped_by_instance()

        # Then
        self.assertEqual(len(result), 1)
        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)
Ejemplo n.º 6
0
    def test_answers_grouped_by_instance_id_one_answer_no_instances(self):
        # Given
        question_state = RepeatingAnswerStateQuestion('question_id',
                                                      MagicMock())

        schema_answer = Answer('answer')
        schema_answer.widget = TextWidget('answer')
        question_state.schema_item.answers = [schema_answer]

        question_state.answers = []

        # When
        result = question_state.answers_grouped_by_instance()

        # Then
        self.assertEqual(len(result), 0)
Ejemplo n.º 7
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"
             }
         }
     ]
Ejemplo n.º 8
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)
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
 def __init__(self, answer_id=None):
     super().__init__(answer_id)
     self.widget = TextWidget(self.id)
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
Ejemplo n.º 12
0
 def __init__(self, answer_id=None):
     super().__init__(answer_id)
     self.type_checkers.append(IntegerTypeCheck())
     self.widget = TextWidget(self.id)