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_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)