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_difference_between_two_survey_response_with_with_deleted_question_in_new_questionnaire(self): old_survey_response = SurveyResponse(Mock(), values={'q1': 'ans5', 'q2': 'ans6', 'q3': 'ans3'}, transport_info=TransportInfo('web', 'web', 'web')) old_survey_response._doc.submitted_on = datetime.date(2012, 02, 27) new_survey_response = SurveyResponse(Mock(), values={'q1': 'ans1', 'q2': 'ans2'}, transport_info=TransportInfo('web', 'web', 'web')) new_survey_response._doc.status = True result_diff = new_survey_response.differs_from(old_survey_response) expected_result_diff = SurveyResponseDifference(old_survey_response.submitted_on, True) expected_result_diff.changed_answers = {'q1': {'old': 'ans5', 'new': 'ans1'}, 'q2': {'old': 'ans6', 'new': 'ans2'}} self.assertEqual(expected_result_diff, result_diff)
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)
def test_set_answers_uses_case_insensitive_key_value(self): values = {'eid': 'cli001'} survey_response = SurveyResponse(Mock(), transport_info=TransportInfo('web', '*****@*****.**', 'destination'), values=values) survey_response.entity_question_code = 'EID' survey_response.set_answers(values) self.assertEquals(1, len(values))
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_should_return_data_sender_TESTER_when_send_from_TEST_REPORTER_MOBILE_NUMBER( self): survey_response = SurveyResponse( TestDataSenderHelper.manager, TransportInfo("sms", TEST_REPORTER_MOBILE_NUMBER, "destination"), owner_uid=self.test_ds_id) data_sender = get_data_sender(TestDataSenderHelper.manager, survey_response) self.assertEqual(('TEST', 'test', data_sender[2]), data_sender)
def test_deep_copy(self): original = SurveyResponse(Mock(), values={'q1': 'ans5', 'q2': 'ans6', 'q3': 'ans3'}, transport_info=TransportInfo('web', 'web', 'web')) duplicate = original.copy() original.values['q1'] = 2 original.values['q2'] = 2 original.values['q3'] = 2 self.assertNotEqual(original.values['q1'], duplicate.values['q1']) self.assertNotEqual(original.values['q2'], duplicate.values['q2']) self.assertNotEqual(original.values['q3'], duplicate.values['q3'])
def test_should_return_N_A_when_the_data_sender_was_deleted_and_send_from_smart_phone( self): survey_response = SurveyResponse(TestDataSenderHelper.manager, TransportInfo("smartPhone", "*****@*****.**", "destination"), owner_uid=self.deleted_ds_id) data_sender = get_data_sender(TestDataSenderHelper.manager, survey_response) self.assertEqual(("M K Gandhi", u"del1"), data_sender[:2])
def test_should_return_data_sender_information_send_from_web(self): beany_tester_id = get_by_short_code_include_voided( TestDataSenderHelper.manager, "rep1", REPORTER_ENTITY_TYPE).id survey_response = SurveyResponse(TestDataSenderHelper.manager, TransportInfo( "web", "*****@*****.**", "destination"), owner_uid=beany_tester_id) data_sender = get_data_sender(TestDataSenderHelper.manager, survey_response) self.assertEqual(("Beany", "rep1", data_sender[2]), data_sender)
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 create_survey_response(submission, dbm): response = SurveyResponse( dbm, TransportInfo(submission.channel, submission.source, submission.destination), form_code=submission.form_code, form_model_revision=submission.form_model_revision, values=copy(submission.values)) response.create_migrated_response(submission.status, submission.errors, submission.is_void(), submission._doc.submitted_on, submission.test, submission.event_time, submission._doc.data_record_id) return response
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)
def errored_survey_response(self): survey_response = SurveyResponse(self.dbm, TransportInfo('web', 'web', 'web'), form_model_id="clinic") survey_response.set_status('previous error') return survey_response