def test_create_survey_responses_throws_exception_when_user_answers_same_survey_twice( self): survey = Survey() survey.available_places = 10 survey.save() survey_response.create(survey.id, 1234) with self.assertRaises(ValueError) as user_error: survey_response.create(survey.id, 1234) self.assertEqual('User 1234 has already completed survey', str(user_error.exception))
def test_create_survey_responses_checks_for_duplicate_user_when_user_id_is_string( self): survey = Survey() survey.available_places = 10 survey.save() survey_response.create(survey.id, 1234) with self.assertRaises(ValueError) as user_error: survey_response.create(survey.id, "1234") self.assertEqual('User 1234 has already completed survey', str(user_error.exception))
def test_create_survey_response_throws_exception_when_reached_max_responses( self): survey = Survey() survey.available_places = 2 survey.save() survey_response.create(survey.id, 1234) survey_response.create(survey.id, 5678) with self.assertRaises(ValueError) as max_responses_error: survey_response.create(survey.id, 91011) self.assertEqual( 'The maximum number (2) of survey responses has been reached', str(max_responses_error.exception))
def create(name, available_places, user): survey = Survey() survey.name = name survey.available_places = available_places survey.user = user survey.save()
def create(survey_id, user_id): survey = Survey.objects(id=survey_id).first() validate(survey, user_id) survey_response = SurveyResponse() survey_response.user = user_id survey.responses.append(survey_response) survey.save()
def test_create_survey_response_sets_user_id(self): survey = Survey() survey.available_places = 1 survey.save() survey_response.create(survey.id, 1234) result = Survey.objects().first() self.assertEqual(1234, result.responses[0].user)
def test_create_survey_response_is_inserted_into_the_database(self): survey = Survey() survey.available_places = 1 survey.save() survey_response.create(survey.id, 1234) result = Survey.objects().first() self.assertEqual(1, len(result.responses))
def test_create_survey_response_sets_created_at(self): survey = Survey() survey.available_places = 1 survey.save() survey_response.create(survey.id, 1234) result = Survey.objects().first() self.assertEqual(datetime.datetime, type(result.responses[0].created_at))
def test_list_surveys_returns_all_surveys(self): first_survey = Survey() first_survey.name = 'First Survey' second_survey = Survey() second_survey.name = 'Second Survey' Survey.objects.insert([first_survey, second_survey]) result = survey.list_surveys() self.assertEqual(2, len(result)) self.assertEqual('First Survey', result[0].name) self.assertEqual('Second Survey', result[1].name)
def tearDown(self): Survey.objects().delete()
def test_create_survey_sets_available_places(self): survey.create('test_survey', 12, 63) saved_survey = Survey.objects().first() self.assertEqual(12, saved_survey.available_places)
def test_create_survey_sets_survey_name(self): survey.create('test_survey', 12, 63) saved_survey = Survey.objects().first() self.assertEqual('test_survey', saved_survey.name)
def test_create_survey_saves_document_to_database(self): survey.create('test_survey', 12, '63') result = Survey.objects() self.assertEqual(1, len(result))