def test_answer_non_repeating_dependency_repeating_validate_all_of_block_and_group_removed(
            self):
        """ load a schema with a non repeating independent answer and a repeating one that depends on it
        validate that when the independent variable is set a call is made to remove all instances of
        the dependant variables
        """
        # Given
        schema = load_schema_from_params(
            'test', 'titles_repeating_non_repeating_dependency')
        colour_answer_location = Location('colour-group', 0,
                                          'favourite-colour')
        colour_answer = {'fav-colour-answer': 'blue'}

        # When
        with self._application.test_request_context():
            with patch(
                    'app.data_model.questionnaire_store.QuestionnaireStore.remove_completed_blocks'
            ) as patch_remove:
                update_questionnaire_store_with_form_data(
                    self.question_store, colour_answer_location, colour_answer,
                    schema)

        # Then
        patch_remove.assert_called_with(group_id='repeating-group',
                                        block_id='repeating-block-3')
    def test_update_questionnaire_store_with_form_data(self):

        schema = load_schema_from_params('test', '0112')

        location = Location('rsi', 0, 'total-retail-turnover')

        form_data = {
            'total-retail-turnover-answer': '1000',
        }

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      location, form_data,
                                                      schema)

        self.assertEqual(self.question_store.completed_blocks, [location])

        self.assertIn(
            {
                'group_instance_id': None,
                'group_instance': 0,
                'answer_id': 'total-retail-turnover-answer',
                'answer_instance': 0,
                'value': '1000'
            }, self.question_store.answer_store.answers)
    def test_updating_questionnaire_store_specific_group(self):
        schema = load_schema_from_params('test', 'repeating_household_routing')
        answers = [
            Answer(group_instance=0,
                   answer_id='first-name',
                   answer_instance=0,
                   value='Joe'),
            Answer(group_instance=0,
                   answer_id='last-name',
                   answer_instance=0,
                   value='Bloggs'),
            Answer(group_instance=0,
                   answer_id='date-of-birth-answer',
                   answer_instance=0,
                   value='2016-03-12'),
            Answer(group_instance=1,
                   answer_id='date-of-birth-answer',
                   answer_instance=0,
                   value='2018-01-01')
        ]

        for answer in answers:
            self.question_store.answer_store.add_or_update(answer)

        answer_form_data = {'date-of-birth-answer': None}
        location = Location('household-member-group', 1, 'date-of-birth')
        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      location,
                                                      answer_form_data, schema)

        self.assertIsNone(self.question_store.answer_store.find(answers[3]))
        for answer in answers[:2]:
            self.assertIsNotNone(self.question_store.answer_store.find(answer))
    def test_update_questionnaire_store_with_date_form_data(self):

        g.schema_json = load_schema_file("test_dates.json")

        location = Location("a23d36db-6b07-4ce0-94b2-a843369511e3", 0,
                            "date-block")

        form_data = {
            'single-date-answer': {
                'day': '12',
                'month': '03',
                'year': '2016'
            },
            'month-year-answer': {
                'month': '11',
                'year': '2014'
            },
        }

        update_questionnaire_store_with_form_data(self.question_store,
                                                  location, form_data)

        self.assertEquals(self.question_store.completed_blocks, [location])

        self.assertIn(
            {
                'group_id': 'a23d36db-6b07-4ce0-94b2-a843369511e3',
                'group_instance': 0,
                'block_id': 'date-block',
                'answer_id': 'single-date-answer',
                'answer_instance': 0,
                'value': '12/03/2016',
            }, self.question_store.answer_store.answers)

        self.assertIn(
            {
                'group_id': 'a23d36db-6b07-4ce0-94b2-a843369511e3',
                'group_instance': 0,
                'block_id': 'date-block',
                'answer_id': 'month-year-answer',
                'answer_instance': 0,
                'value': '11/2014',
            }, self.question_store.answer_store.answers)
    def test_update_questionnaire_store_with_default_value(self):

        schema = load_schema_from_params('test', 'default')

        location = Location('group', 0, 'number-question')

        # No answer given so will use schema defined default
        form_data = {'answer': None}

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      location, form_data,
                                                      schema)

        self.assertEqual(self.question_store.completed_blocks, [location])

        self.assertIn(
            {
                'group_instance': 0,
                'answer_id': 'answer',
                'answer_instance': 0,
                'value': 0
            }, self.question_store.answer_store.answers)
    def test_update_questionnaire_store_with_form_data(self):

        g.schema_json = load_schema_file("1_0112.json")

        location = Location("rsi", 0, "total-retail-turnover")

        form_data = {
            'total-retail-turnover-answer': "1000",
        }

        update_questionnaire_store_with_form_data(self.question_store,
                                                  location, form_data)

        self.assertEquals(self.question_store.completed_blocks, [location])

        self.assertIn(
            {
                'group_id': 'rsi',
                'group_instance': 0,
                'block_id': 'total-retail-turnover',
                'answer_id': 'total-retail-turnover-answer',
                'answer_instance': 0,
                'value': '1000',
            }, self.question_store.answer_store.answers)
    def test_updating_questionnaire_store_removes_completed_block_for_calculation_dependencies(
            self):

        schema = load_schema_from_params('test', 'dependencies_calculation')

        calculation_answer_location = Location('group', 0, 'total-block')
        dependent_location = Location('group', 0, 'breakdown-block')

        calculation_answer_data = {
            'total-answer': '100',
        }

        dependent_data = {
            'breakdown-1': '10',
            'breakdown-2': '20',
            'breakdown-3': '30',
            'breakdown-4': '40'
        }

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(
                self.question_store, calculation_answer_location,
                calculation_answer_data, schema)

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      dependent_location,
                                                      dependent_data, schema)

        self.assertIn(calculation_answer_location,
                      self.question_store.completed_blocks)
        self.assertIn(dependent_location, self.question_store.completed_blocks)

        calculation_answer_data = {
            'total-answer': '99',
        }

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(
                self.question_store, calculation_answer_location,
                calculation_answer_data, schema)

        self.assertNotIn(dependent_location,
                         self.question_store.completed_blocks)
    def test_updating_questionnaire_store_removes_completed_block_for_max_dependencies(
            self):

        schema = load_schema_from_params('test', 'dependencies_max_value')

        max_answer_location = Location('group', 0, 'max-block')
        dependent_location = Location('group', 0, 'dependent-block')

        max_answer_data = {
            'max-answer': '10',
        }

        dependent_data = {
            'dependent-1': '10',
        }

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      max_answer_location,
                                                      max_answer_data, schema)

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      dependent_location,
                                                      dependent_data, schema)

        self.assertIn(max_answer_location,
                      self.question_store.completed_blocks)
        self.assertIn(dependent_location, self.question_store.completed_blocks)

        max_answer_data = {
            'max-answer': '11',
        }

        with self._application.test_request_context():
            update_questionnaire_store_with_form_data(self.question_store,
                                                      max_answer_location,
                                                      max_answer_data, schema)

        self.assertNotIn(dependent_location,
                         self.question_store.completed_blocks)