def add_sessions_ratings_and_speakers(): first_tag = Tag(description='javascript') last_tag = Tag(description='ember-js') first_tag.save() last_tag.save() first_session = Session(name='first', room='A', desc='javascript') last_session = Session(name='last', room='Z', desc='python') first_session.save() last_session.save() first_session.tags.add(first_tag) last_session.tags.add(last_tag) first_session.save() last_session.save() first_rating = Rating(score=9, feedback='legit', session=first_session) last_rating = Rating(score=2, feedback='broken', session=first_session) first_rating.save() last_rating.save() first_speaker = Speaker(name='foo', session=first_session) last_speaker = Speaker(name='bar', session=last_session) first_speaker.save() last_speaker.save() return first_session, last_session, first_rating, last_rating, first_speaker, last_speaker, first_tag, last_tag
def setUp(self): self.first_speaker = Speaker(name='foo') self.last_speaker = Speaker(name='bar') self.first_speaker.save() self.last_speaker.save()
class SpeakerTests(TestCase): def setUp(self): self.first_speaker = Speaker(name='foo') self.last_speaker = Speaker(name='bar') self.first_speaker.save() self.last_speaker.save() def test_will_return_json_with_list_of_speakers(self): response = self.client.get('/codecamp/speakers') speakers = json.loads(response.content) self.assertEqual(response.status_code, 200) self.assertEqual(len(speakers), 2) def test_speakers_json_has_an_attribute_for_each_item(self): response = self.client.get('/codecamp/speakers') speakers = json.loads(response.content) self.assertEqual(speakers[0]['name'], 'foo') def test_speakers_json_returns_last_speaker_json(self): response = self.client.get('/codecamp/speakers') speakers = json.loads(response.content) self.assertEqual(speakers[1]['name'], 'bar') def test_detail_speakers_endpoint_returns_attributes_for_given_speaker_id(self): response = self.client.get('/codecamp/speakers/{}/'.format(self.first_speaker.pk)) self.assertEqual(response.status_code, 200) self.assertEqual(response.content, '{"id": 1, "name": "foo"}') def test_http_post_will_create_speaker_and_return_201(self): data = {'name': 'who'} response = self.client.post('/codecamp/speakers', data) self.assertEqual(response.status_code, 201) def test_http_post_will_create_speaker_and_return_created_speaker_json(self): data = {'name': 'who'} response = self.client.post('/codecamp/speakers', data) created_speaker = json.loads(response.content) self.assertEqual(created_speaker['name'], 'who') def test_http_post_without_data_returns_400(self): response = self.client.post('/codecamp/speakers', {}) self.assertEqual(response.status_code, 400) def test_http_put_will_update_first_speaker_and_return_200(self): data = {'name': 'updated name'} response = self.client.put('/codecamp/speakers/{}/'.format(self.first_speaker.pk), data) self.assertEqual(response.status_code, 200) def test_http_put_will_update_first_speaker_and_return_updated_speaker_json(self): data = {'name': 'updated name'} response = self.client.put('/codecamp/speakers/{}/'.format(self.first_speaker.pk), data) updated_speaker = json.loads(response.content) self.assertEqual(updated_speaker['name'], 'updated name') def test_http_delete_will_remove_first_speaker_and_return_204(self): response = self.client.delete('/codecamp/speakers/{}/'.format(self.first_speaker.pk)) self.assertEqual(response.status_code, 204) def test_http_delete_will_remove_first_speaker_and_return_empty_content(self): response = self.client.delete('/codecamp/speakers/{}/'.format(self.first_speaker.pk)) self.assertEqual(response.content, '') def test_http_delete_will_return_404_when_incorrect_id_used_to_delete_speaker(self): response = self.client.delete('/codecamp/speakers/999999999999999999/') self.assertEqual(response.status_code, 404)