def test_question_code_case_mismatch_gives_right_value(self):
        value_mock = PropertyMock(return_value={'Q4': '23'})
        type(self.survey_response).values = value_mock
        number_field = IntegerField('name', 'q4', 'age')

        builder = EnrichedSurveyResponseBuilder(None, self.survey_response, self.form_model, {})
        dictionary = builder._create_answer_dictionary(number_field)
        self.assertEquals('23', dictionary.get('answer'))
        self.assertEquals('age', dictionary.get('label'))
        self.assertEquals('integer', dictionary.get('type'))
    def test_format_is_present_for_date_fields(self):
        value_mock = PropertyMock(return_value={'q3': '21.03.2011'})
        type(self.survey_response).values = value_mock

        builder = EnrichedSurveyResponseBuilder(None, self.survey_response, self.form_model, {})
        dictionary = builder._create_answer_dictionary(DateField('name', 'q3', 'date lab', 'dd.mm.yyyy'))
        self.assertEquals('dd.mm.yyyy', dictionary.get('format'))
        self.assertEquals('21.03.2011', dictionary.get('answer'))
        self.assertEquals('date lab', dictionary.get('label'))
        self.assertEquals('date', dictionary.get('type'))
    def test_single_select_field_value_representation(self):
        value_mock = PropertyMock(return_value={'q4': 'b'})
        type(self.survey_response).values = value_mock
        select_field = SelectField('name', 'q4', 'select',
                                   [{'text': 'orange', 'val': 'a'}, {'text': 'mango', 'val': 'b'},
                                    {'text': 'apple', 'val': 'c'}])

        builder = EnrichedSurveyResponseBuilder(None, self.survey_response, self.form_model, {})
        dictionary = builder._create_answer_dictionary(select_field)
        self.assertEquals({'b': 'mango'}, dictionary.get('answer'))
        self.assertEquals('select', dictionary.get('label'))
        self.assertEquals('select1', dictionary.get('type'))
    def test_subject_answer_id_as_value_rather_than_name_when_subject_is_not_existing(self):
        survey_response = Mock(spec=SurveyResponse)
        type(survey_response).values = PropertyMock(return_value={'q1': 'cli001'})
        subject_field = UniqueIdField('clinic','name', 'q1', 'Reporting for Subject')
        builder = EnrichedSurveyResponseBuilder(self.dbm, survey_response, self.form_model, {})

        with patch('mangrove.feeds.enriched_survey_response.by_short_code') as by_short_code:
            by_short_code.side_effect = DataObjectNotFound("Entity", "id", "cli001")

            dictionary = builder._create_answer_dictionary(subject_field)

            self.assertEquals({'id': 'cli001', 'name': '', 'deleted': True}, dictionary.get('answer'))
            self.assertEquals('Reporting for Subject', dictionary.get('label'))
            self.assertEquals('unique_id', dictionary.get('type'))
            self.assertEquals('true', dictionary.get('is_entity_question'))
    def test_identify_multi_select_answer_with_more_than_26_options(self):
        value_mock = PropertyMock(return_value={'q4': '1a1c'})
        type(self.survey_response).values = value_mock
        select_field = SelectField('name', 'q4', 'multi select',
                                   [{'text': 'orange', 'val': '1a'},
                                    {'text': 'watermelon', 'val': '1b'},
                                    {'text': 'strawberry', 'val': '1c'},
                                    {'text': 'apple', 'val': 'c'}],
                                   single_select_flag=False)

        builder = EnrichedSurveyResponseBuilder(None, self.survey_response, self.form_model, {})
        dictionary = builder._create_answer_dictionary(select_field)
        self.assertEquals({'1a': 'orange', '1c': 'strawberry'}, dictionary.get('answer'))
        self.assertEquals('multi select', dictionary.get('label'))
        self.assertEquals('select', dictionary.get('type'))
    def test_subject_answer_has_name_of_subject(self):
        value_mock = PropertyMock(return_value={'Q1': 'cli001'})
        type(self.survey_response).values = value_mock
        subject_field = UniqueIdField('clinic','name', 'q1', 'Reporting for Subject')
        builder = EnrichedSurveyResponseBuilder(self.dbm, self.survey_response, self.form_model, {})

        with patch('mangrove.feeds.enriched_survey_response.by_short_code') as by_short_code:
            entity = Mock(spec=Entity)
            by_short_code.return_value = entity
            type(entity).data = PropertyMock(return_value={'name': {'value': 'Kormanagala Clinic'}})

            dictionary = builder._create_answer_dictionary(subject_field)

            self.assertEquals({'id': 'cli001', 'name': 'Kormanagala Clinic', 'deleted': False},
                              dictionary.get('answer'))
            self.assertEquals('Reporting for Subject', dictionary.get('label'))
            self.assertEquals('unique_id', dictionary.get('type'))
            self.assertEquals('true', dictionary.get('is_entity_question'))