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_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_should_update_enriched_survey_response(self): survey_response = TestSurveyResponseBuilder(self.manager, form_model_id='CL1', values={ 'ID': '1', 'Q1': 'name', 'Q2': 21, 'Q3': 'a' }).build(self.owner_uid) form_model = get_form_model_by_code(self.manager, 'CL1') self.feed_manager._save_document( EnrichedSurveyResponseBuilder(self.manager, survey_response, form_model, {}).feed_document()) edited_values = {'ID': '1', 'Q1': 'name2', 'Q2': 24, 'Q3': 'b'} survey_response.set_form(form_model) survey_response.set_answers(edited_values) survey_response.save() doc = EnrichedSurveyResponseBuilder( self.manager, survey_response, form_model, {}).update_event_document(self.feed_manager) self.feed_manager._save_document(doc) edited_feed_document = get_feed_document_by_id(self.feed_manager, survey_response.uuid) expected_values = { 'id': { 'answer': { 'id': '1', 'name': 'clinic1', 'deleted': False }, 'is_entity_question': 'true', 'type': 'unique_id', 'unique_id_type': 'clinic', 'label': 'What is associated entity' }, 'q1': { 'answer': 'name2', 'type': 'text', 'label': 'What is your name' }, 'q2': { 'answer': 24, 'type': 'integer', 'label': "What is your Father's Age" }, 'q3': { 'answer': { 'b': 'YELLOW' }, 'type': 'select1', 'label': 'What is your favourite color' } } self.assertDictEqual(edited_feed_document.values, expected_values)
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_feed_datasender_is_none_when_migrating_survey_response_with_document_for_owner_id_not_found(self): survey_response = Mock(spec=SurveyResponse) type(survey_response).owner_uid = PropertyMock(return_value='data_sender_uid') type(survey_response).values = PropertyMock(return_value={}) builder = EnrichedSurveyResponseBuilder(self.dbm, survey_response, self.form_model, {}) with patch("mangrove.feeds.enriched_survey_response.Contact.get") as get_datasender: get_datasender.side_effect = DataObjectNotFound(Entity.__name__, 'id', None) data_sender = builder._data_sender() self.assertIsNone(data_sender['id']) self.assertIsNone(data_sender['last_name']) self.assertIsNone(data_sender['mobile_number']) self.assertIsNone(data_sender['deleted']) self.assertIsNone(data_sender['question_code'])
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_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_delete_status_updated(self): type(self.survey_response).status = PropertyMock(return_value=True) self.survey_response.is_void.return_value = True type(self.survey_response).values = PropertyMock(return_value={}) type(self.survey_response).modified = PropertyMock(return_value=datetime.now()) type(self.form_model).fields = PropertyMock(return_value=[]) builder = EnrichedSurveyResponseBuilder(self.dbm, self.survey_response, self.form_model, {}) def patch_data_sender(): return {} builder._data_sender = patch_data_sender doc = builder.feed_document() self.assertTrue(doc.void)
def edit_survey(self, form_code, values, reporter_names, survey_response, additional_feed_dictionary=None, owner_id=None): form_model = get_form_model_by_code(self.dbm, form_code) form = EditSurveyResponseForm(self.dbm, survey_response, form_model, values) try: if form.is_valid: if owner_id: reporter = by_short_code(self.dbm, owner_id, REPORTER_ENTITY_TYPE) survey_response.owner_uid = reporter.id survey_response.modified_by = self.admin_id or owner_id survey_response = form.save() try: feed_create_errors = None if self.feeds_dbm: builder = EnrichedSurveyResponseBuilder( self.dbm, survey_response, form_model, additional_feed_dictionary) event_document = builder.update_event_document( self.feeds_dbm) self.feeds_dbm._save_document(event_document) except Exception as e: feed_create_errors = 'error while editing feed doc for %s \n' % survey_response.id feed_create_errors += e.message + '\n' feed_create_errors += traceback.format_exc() except MangroveException as exception: raise return Response(reporter_names, survey_response.uuid, form.saved, form.errors, form.data_record_id, None, form._cleaned_data, form.is_registration, form.entity_type, form.form_model.form_code, feed_create_errors, created=survey_response.created, version=survey_response.version)
def test_field_values_not_calculated_when_submission_is_error(self): type(self.survey_response).status = PropertyMock(return_value=False) type(self.survey_response).values = PropertyMock(return_value={'q1': '1', 'q2': 'abc'}) type(self.survey_response).modified = PropertyMock(return_value=datetime.now()) fields_property_mock = PropertyMock(return_value={}) type(self.form_model).fields = fields_property_mock builder = EnrichedSurveyResponseBuilder(self.dbm, self.survey_response, self.form_model, {}) def patch_data_sender(): return {} builder._data_sender = patch_data_sender doc = builder.feed_document() self.assertFalse(fields_property_mock.called) self.assertDictEqual({'q1': '1', 'q2': 'abc'}, doc.values)
def test_status_is_error_for_invalid_not_deleted_survey_response(self): type(self.survey_response).status = PropertyMock(return_value=False) self.survey_response.is_void.return_value = False type(self.survey_response).values = PropertyMock(return_value={}) type(self.survey_response).modified = PropertyMock(return_value=datetime.now()) field = TextField('name', 'q1', 'A Question') type(self.form_model).fields = PropertyMock(return_value=[field]) builder = EnrichedSurveyResponseBuilder(self.dbm, self.survey_response, self.form_model, {}) def patch_data_sender(): return {} builder._data_sender = patch_data_sender doc = builder.feed_document() self.assertEqual('error', doc.status) self.assertFalse(doc.void)
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'))
def test_should_void_feed_document_and_set_status_as_deleted_when_submission_deleted( self): survey_response = TestSurveyResponseBuilder(self.manager, form_model_id='CL1', values={ 'ID': '1', 'Q1': 'name', 'Q2': 21, 'Q3': 'a' }).build(self.owner_uid) form_model = get_form_model_by_code(self.manager, 'CL1') self.feed_manager._save_document( EnrichedSurveyResponseBuilder(self.manager, survey_response, form_model, {}).feed_document()) EnrichedSurveyResponseBuilder(self.manager, survey_response, form_model, {}).delete_feed_document( self.feed_manager) deleted_feed_document = get_feed_document_by_id( self.feed_manager, survey_response.uuid) self.assertTrue(deleted_feed_document.void)
def test_datasender_info(self): survey_response = Mock(spec=SurveyResponse) type(survey_response).owner_uid = PropertyMock(return_value='data_sender_uid') type(survey_response).values = PropertyMock(return_value={}) with patch("mangrove.feeds.enriched_survey_response.Contact.get") as get_datasender: data_sender = Mock(spec=Entity) get_datasender.return_value = data_sender type(data_sender).data = PropertyMock( return_value={"name": {"value": "data sender name"}, "mobile_number": {"value": "+39882773662"}}) type(data_sender).short_code = PropertyMock(return_value="rep2423") data_sender.is_void.return_value = False type(self.form_model).fields = PropertyMock(return_value=[]) builder = EnrichedSurveyResponseBuilder(self.dbm, survey_response, self.form_model, {}) datasender_dict = builder._data_sender() self.assertEquals(datasender_dict.get('id'), 'rep2423') self.assertEquals(datasender_dict.get('last_name'), 'data sender name') self.assertEquals(datasender_dict.get('mobile_number'), '+39882773662') self.assertEquals(datasender_dict.get('question_code'), '') self.assertFalse(datasender_dict.get('deleted')) get_datasender.assert_called_once_with(self.dbm, 'data_sender_uid')
def delete_survey(self, survey_response, additional_details): feed_delete_errors = None try: survey_response.void() form_model = FormModel.get(self.dbm, survey_response.form_model_id) if self.feeds_dbm: feed_delete_errors = EnrichedSurveyResponseBuilder( self.dbm, survey_response, form_model, additional_details).delete_feed_document(self.feeds_dbm) except MangroveException as e: return Response(errors=e.message, feed_error_message=feed_delete_errors) return Response(success=True, feed_error_message=feed_delete_errors)
def save_survey(self, form_code, values, reporter_names, transport_info, reporter_id, additional_feed_dictionary=None, translation_processor=None): try: form_model = get_form_model_by_code(self.dbm, form_code) except FormModelDoesNotExistsException: form_model = get_active_form_model(self.dbm, form_code) #TODO : validate_submission should use form_model's bound values cleaned_data, errors = form_model.validate_submission(values=values) form_model.bind(form_model.remove_invalid_meta_answers(values)) if reporter_id is not None: survey_response = self.create_survey_response_from_known_datasender( transport_info, form_model, form_model.bound_values(), reporter_id, self.response) else: survey_response = self.create_survey_response_from_unknown_datasender( transport_info, form_model.id, form_model.bound_values(), self.response) survey_response.set_form(form_model) form_submission = DataFormSubmission(form_model, cleaned_data, errors) feed_create_errors = None try: if form_submission.is_valid: form_submission.save(self.dbm) except MangroveException as exception: errors = exception.message raise finally: if translation_processor is not None: translated_errors = translation_processor( form_model, self.response).process() survey_response.set_status(translated_errors) else: survey_response.set_status(errors) survey_response.create(form_submission.data_record_id) try: if self.feeds_dbm: builder = EnrichedSurveyResponseBuilder( self.dbm, survey_response, form_model, additional_feed_dictionary, ds_mobile_number=transport_info.source) event_document = builder.feed_document() self.feeds_dbm._save_document(event_document) except Exception as e: feed_create_errors = 'error while creating feed doc for %s \n' % survey_response.id feed_create_errors += e.message + '\n' feed_create_errors += traceback.format_exc() if self.response is None: errors = form_submission.errors success = form_submission.saved else: errors = self.response.errors success = False return Response(reporter_names, survey_response.uuid, success, errors, form_submission.data_record_id, form_submission.short_code, form_submission.cleaned_data, form_submission.is_registration, form_submission.entity_type, form_submission.form_model.form_code, feed_create_errors, created=survey_response.created, version=survey_response.version)
def enriched_dbm(self, survey_response): form_model = get_form_model_by_code(self.dbm, survey_response.form_code) builder = EnrichedSurveyResponseBuilder(self.dbm, survey_response, form_model, self.project_info(survey_response.form_code), self.logger) return builder.feed_document()