def _load_questionnaire(form_code, dbm): assert isinstance(dbm, DatabaseManager) assert is_string(form_code) rows = dbm.load_all_rows_in_view('questionnaire', key=form_code) if not len(rows): raise FormModelDoesNotExistsException(form_code) return rows[0]['value']
def get_active_form_model(dbm, form_code): projects = dbm.load_all_rows_in_view("all_projects") for project_row in projects: project_doc = ProjectDocument.wrap(project_row.get('value')) project = Project.new_from_doc(dbm, project_doc) if project.active: return project raise FormModelDoesNotExistsException(form_code)
def test_exception_is_raised_for_invalid_short_code_submissions(self): survey_response_service = SurveyResponseService(self.manager) with patch( 'mangrove.transport.services.survey_response_service.get_active_form_model') as mock_get_active_form_model: mock_get_active_form_model.side_effect = FormModelDoesNotExistsException("form_code") values = {'ID': "invalid", 'Q1': 'name', 'Q2': '80', 'Q3': 'a'} transport_info = TransportInfo('web', 'src', 'dest') request = Request(values, transport_info) self.assertRaises(MangroveException, survey_response_service.save_survey, 'CL1', values, [], transport_info, '')
def get_form_model_by_code(dbm, code): assert isinstance(dbm, DatabaseManager) assert is_string(code) rows = dbm.load_all_rows_in_view('questionnaire', key=code) if not len(rows): raise FormModelDoesNotExistsException(code) doc = dbm._load_document(rows[0]['value']['_id'], FormModelDocument) form = FormModel.new_from_doc(dbm, doc) return form
def check_answers_numbers(self, is_poll, submission_values, linked_datasender=True): exception = self.request.get('exception', False) if exception and isinstance(exception, SMSParserWrongNumberOfAnswersException): if linked_datasender: raise exception raise self._get_exception() elif not linked_datasender and is_poll: raise FormModelDoesNotExistsException(submission_values['q1'])
def test_error_when_form_code_invalid(self): request = HttpRequest() request.GET['start_date'] = urllib2.quote("21-12-2001 12:12:57".encode("utf-8")) request.GET['end_date'] = urllib2.quote("21-12-2002 12:12:57".encode("utf-8")) request.user = '******' with patch('datawinners.feeds.views.get_form_model_by_code') as get_form_model_by_code: with patch('datawinners.feeds.views.get_database_manager') as get_db_manager: get_db_manager.return_value = Mock(spec=DatabaseManager) get_form_model_by_code.side_effect = FormModelDoesNotExistsException('non-existent-form-code') response = feed_entries(request, "non-existent-form-code") self.assertEqual(400, response.status_code) response_content = jsonpickle.decode(response.content) self.assertEquals(response_content.get('ERROR_CODE'), 101) self.assertEquals(response_content.get('ERROR_MESSAGE'), 'Invalid form code provided')
def test_should_generate_unique_questionnaire_code(self): patcher = patch("datawinners.project.helper.models") models_mock = patcher.start() patcher1 = patch("datawinners.project.helper.get_form_model_by_code") form_code_mock = patcher1.start() dbm = Mock(spec=DatabaseManager) form_code_mock.side_effect = FormModelDoesNotExistsException('') models_mock.count_projects.return_value = 0 self.assertEqual(helper.generate_questionnaire_code(dbm), "001") models_mock.count_projects.return_value = 1 self.assertEqual(helper.generate_questionnaire_code(dbm), "002") patcher.stop() patcher1.stop()
def expected_side_effect(*args, **kwargs): code = kwargs.get('code') or args[1] if code == "003": raise FormModelDoesNotExistsException('') if code == "002": return Mock(spec=FormModel)
def test_should_return_default_message_for_exception_for_no_channel_passed( self): message = get_exception_message_for( exception=FormModelDoesNotExistsException("QC1")) expected_message = "Questionnaire ID QC1 doesnt exist." self.assertEqual(expected_message, message)
def test_should_return_message_for_exception_for_channel(self): message = get_exception_message_for( exception=FormModelDoesNotExistsException("QC1"), channel="sms") expected_message = "Error with Questionnaire code QC1. Find the Questionnaire code on the printed questionnaire and resend SMS starting with this questionnaire code." self.assertEqual(expected_message, message)
def test_should_fail_submission_if_invalid_form_code(self): self.get_form_model_mock.side_effect = FormModelDoesNotExistsException( "INVALID_CODE") with self.assertRaises(FormModelDoesNotExistsException): self.submission_handler.accept(self.submission_request)
def expected_side_effect(*args, **kwargs): request = kwargs.get('request') or args[0] if request.form_code == 'clf2': raise FormModelDoesNotExistsException('') return SubmissionResponse(success=True, submission_id=1)