def test_update_enriched_survey_response_with_new_survey_response_values(self):
        field = TextField('name', 'q1', 'A Question')
        type(self.form_model).fields = PropertyMock(return_value=[field])

        type(self.form_model).entity_type = PropertyMock(return_value=['reporter'])

        self.form_model.id= 'form_model_id'
        survey_response = SurveyResponse(Mock())
        survey_response._doc = SurveyResponseDocument(status=False, values={'q1': 'answer1'},
                                                      form_model_id='form_model_id')
        builder = EnrichedSurveyResponseBuilder(self.dbm, survey_response, self.form_model, {})

        def patch_data_sender():
            return {}

        builder._data_sender = patch_data_sender
        doc = builder.feed_document()

        self.assertEquals(doc.values, {'q1': 'answer1'})

        edited_survey_response_doc = SurveyResponseDocument(status=True, values={'q1': 'answer2'},
                                                            form_model_id='form_model_id')
        edited_survey_response = SurveyResponse(Mock())
        edited_survey_response._doc = edited_survey_response_doc

        new_builder = EnrichedSurveyResponseBuilder(self.dbm, edited_survey_response, self.form_model, {})
        new_builder._data_sender = patch_data_sender
        feeds_dbm = Mock(spec=DatabaseManager)
        with patch('mangrove.feeds.enriched_survey_response.get_feed_document_by_id') as get_document:
            get_document.return_value = doc
            edited_doc = new_builder.update_event_document(feeds_dbm)
            self.assertEquals(edited_doc.values, {'q1': {'answer': 'answer2', 'label': 'A Question', 'type': 'text'}})
    def test_should_replace_answer_option_values_with_options_text_when_answer_type_is_changed_from_single_select_choice_field(
            self):
        survey_response_doc = SurveyResponseDocument(values={
            'q1': 'a',
        })
        survey_response = SurveyResponse(Mock())
        survey_response._doc = survey_response_doc
        choice_field = SelectField(name='question one',
                                   code='q1',
                                   label='question one',
                                   options=[("one", "a"), ("two", "b"),
                                            ("three", "c"), ("four", "d")],
                                   single_select_flag=True)
        text_field = TextField(name='question one',
                               code='q1',
                               label='question one')

        questionnaire_form_model = Mock(spec=FormModel)
        questionnaire_form_model.form_code = 'test_form_code'
        questionnaire_form_model.fields = [text_field]
        questionnaire_form_model.get_field_by_code_and_rev.return_value = choice_field
        request_dict = construct_request_dict(survey_response,
                                              questionnaire_form_model, 'id')

        expected_dict = {
            'q1': 'one',
            'form_code': 'test_form_code',
            'dsid': 'id'
        }
        self.assertEqual(request_dict, expected_dict)
    def test_should_create_request_dict_with_older_survey_response(self):
        survey_response_doc = SurveyResponseDocument(values={
            'q1': 23,
            'q2': 'sometext',
            'q3': 'ab'
        })
        survey_response = SurveyResponse(Mock())
        survey_response._doc = survey_response_doc
        int_field = IntegerField(name='question one',
                                 code='q1',
                                 label='question one')
        text_field = TextField(name='question two',
                               code='q2',
                               label='question two')
        choice_field = SelectField(name='question three',
                                   code='q4',
                                   label='question three',
                                   options=[("one", "a"), ("two", "b"),
                                            ("three", "c"), ("four", "d")],
                                   single_select_flag=False)
        questionnaire_form_model = Mock(spec=FormModel)
        questionnaire_form_model.form_code = 'test_form_code'
        questionnaire_form_model.fields = [int_field, text_field, choice_field]

        request_dict = construct_request_dict(survey_response,
                                              questionnaire_form_model, 'id')
        expected_dict = {
            'q1': 23,
            'q2': 'sometext',
            'q4': None,
            'form_code': 'test_form_code',
            'dsid': 'id'
        }
        self.assertEqual(request_dict, expected_dict)
    def test_should_get_static_info_from_submission(self):
        with patch("datawinners.project.views.submission_views.get_data_sender"
                   ) as get_data_sender:
            survey_response_document = SurveyResponseDocument(
                channel='web',
                status=False,
                error_message="Some Error in submission")
            get_data_sender.return_value = ('Psub', 'rep2', '*****@*****.**')
            submission_date = datetime(2012, 02, 20, 12, 15, 44)
            survey_response_document.submitted_on = submission_date
            survey_response_document.created = datetime(
                2012, 02, 20, 12, 15, 50)

            survey_response = SurveyResponse(Mock())

            survey_response._doc = survey_response_document
            static_info = build_static_info_context(Mock(), survey_response)

            expected_values = OrderedDict({
                'static_content': {
                    'Data Sender': ('Psub', 'rep2', '*****@*****.**'),
                    'Source':
                    u'Web',
                    'Submission Date':
                    submission_date.strftime(
                        SUBMISSION_DATE_FORMAT_FOR_SUBMISSION)
                }
            })
            expected_values.update({'is_edit': True})
            expected_values.update({'status': u'Error'})
            self.assertEqual(expected_values, static_info)
예제 #5
0
    def test_should_get_static_info_from_submission(self):
        with patch("datawinners.project.views.submission_views.get_data_sender") as get_data_sender:
            survey_response_document = SurveyResponseDocument(channel='web', status=False,
                                                              error_message="Some Error in submission")
            get_data_sender.return_value = ('Psub', 'rep2')
            submission_date = datetime(2012, 02, 20, 12, 15, 44)
            survey_response_document.submitted_on = submission_date
            survey_response_document.created = datetime(2012, 02, 20, 12, 15, 50)

            survey_response = SurveyResponse(Mock())

            survey_response._doc = survey_response_document
            project = Mock()
            project.data_senders = ["rep2"]
            organization_mock = Mock()
            organization_mock.org_id = "TEST1234"
            # with patch("datawinners.project.views.submission_views.get_organization_from_manager") as get_ngo_from_manager_mock:
            #     get_ngo_from_manager_mock.return_value = organization_mock
            static_info = build_static_info_context(Mock(), survey_response, questionnaire_form_model=project)

            expected_values = OrderedDict({'static_content': {
                'Data Sender': ('Psub', 'rep2'),
                'Source': u'Web',
                'Submission Date': submission_date.strftime(SUBMISSION_DATE_FORMAT_FOR_SUBMISSION)}})
            expected_values.update({'is_edit': True})
            expected_values.update({'status': u'Error'})
            self.assertEqual(expected_values, static_info)
    def test_convert_survey_response_values_to_dict_format(self):
        survey_response_doc = SurveyResponseDocument(
            values={
                'q1': '23',
                'q2': 'sometext',
                'q3': 'a',
                'geo': '2.34,5.64',
                'date': '12.12.2012'
            })
        survey_response = SurveyResponse(Mock())
        survey_response._doc = survey_response_doc
        int_field = IntegerField(name='question one',
                                 code='q1',
                                 label='question one',
                                 ddtype=Mock(spec=DataDictType))
        text_field = TextField(name='question two',
                               code='q2',
                               label='question two',
                               ddtype=Mock(spec=DataDictType))
        single_choice_field = SelectField(name='question three',
                                          code='q3',
                                          label='question three',
                                          options=[("one", "a"), ("two", "b"),
                                                   ("three", "c"),
                                                   ("four", "d")],
                                          single_select_flag=True,
                                          ddtype=Mock(spec=DataDictType))
        geo_field = GeoCodeField(name='geo',
                                 code='GEO',
                                 label='geo',
                                 ddtype=Mock())
        date_field = DateField(name='date',
                               code='DATE',
                               label='date',
                               date_format='dd.mm.yyyy',
                               ddtype=Mock())
        questionnaire_form_model = Mock(spec=FormModel)
        questionnaire_form_model.form_code = 'test_form_code'
        questionnaire_form_model.fields = [
            int_field, text_field, single_choice_field, geo_field, date_field
        ]

        request_dict = construct_request_dict(survey_response,
                                              questionnaire_form_model)
        expected_dict = OrderedDict({
            'q1': '23',
            'q2': 'sometext',
            'q3': 'a',
            'GEO': '2.34,5.64',
            'DATE': '12.12.2012',
            'form_code': 'test_form_code'
        })
        self.assertEqual(expected_dict, request_dict)
 def test_should_return_none_if_survey_response_questionnaire_is_different_from_form_model(
         self):
     survey_response_doc = SurveyResponseDocument(values={
         'q5': '23',
         'q6': 'sometext',
         'q7': 'ab'
     })
     survey_response = SurveyResponse(Mock())
     survey_response._doc = survey_response_doc
     int_field = IntegerField(name='question one',
                              code='q1',
                              label='question one',
                              ddtype=Mock(spec=DataDictType))
     text_field = TextField(name='question two',
                            code='q2',
                            label='question two',
                            ddtype=Mock(spec=DataDictType))
     choice_field = SelectField(name='question three',
                                code='Q3',
                                label='question three',
                                options=[("one", "a"), ("two", "b"),
                                         ("three", "c"), ("four", "d")],
                                single_select_flag=False,
                                ddtype=Mock(spec=DataDictType))
     questionnaire_form_model = Mock(spec=FormModel)
     questionnaire_form_model.form_code = 'test_form_code'
     questionnaire_form_model.fields = [int_field, text_field, choice_field]
     result_dict = construct_request_dict(survey_response,
                                          questionnaire_form_model)
     expected_dict = {
         'q1': None,
         'q2': None,
         'Q3': None,
         'form_code': 'test_form_code'
     }
     self.assertEqual(expected_dict, result_dict)