def test_filter_by_genre(self): # GIVEN movie1 = Movie.objects.create(title='TestTitle1', year=111, plot='TetPlot1', genre='TestGenre1') movie2 = Movie.objects.create(title='OtherTitle', year=111, plot='TetPlot1', genre='OtherGenre') movie3 = Movie.objects.create(title='TestTitle2', year=111, plot='TetPlot1', genre='TestGenre1 and other') # WHEN view = MoviesListViewSet.as_view() request = factory.get('/', {'genre__icontains': 'testgenre1'}) response = view(request) # THEN self.assertEqual(response.status_code, 200) self.assertEqual(response.data['count'], 2) self.assertSetEqual({res['title'] for res in response.data['results']}, {movie1.title, movie3.title})
def test_post_method_for_response_false_from_external_api( self, requests_mock): # GIVEN requests_mock.get = mocked_requests_response_false # WHEN view = MoviesListViewSet.as_view() request = factory.post('/', data={'title': 'TestTitle'}, format='json') response = view(request) self.assertEqual(response.status_code, 404)
def test_post_method_for_existing_title(self, requests_mock, serializer_mock): # GIVEN requests_mock.get = mocked_requests_get serializer_mock().is_valid = lambda: False # WHEN view = MoviesListViewSet.as_view() request = factory.post('/', data={'title': 'TestTitle'}, format='json') response = view(request) self.assertEqual(response.status_code, 417)
def test_post_method_for_existing_title(self, requests_mock): # GIVEN requests_mock.get = mocked_requests_get Movie.objects.create(title='TestTitle', year=111, plot='TetPlot', genre='TestGenre') # WHEN view = MoviesListViewSet.as_view() request = factory.post('/', data={'title': 'TestTitle'}, format='json') response = view(request) self.assertEqual(response.status_code, 409)
def test_post_method(self, requests_mock): # GIVEN requests_mock.get = mocked_requests_get # WHEN view = MoviesListViewSet.as_view() request = factory.post('/', data={'title': 'TestTitle'}, format='json') response = view(request) self.assertEqual(response.status_code, 200) self.assertSetEqual(set(response.data.keys()), {'movie_object', 'externalAPI_data'}) self.assertEqual(Movie.objects.all().count(), 1) movie = Movie.objects.all()[0] self.assertEqual(movie.title, 'TestTitle')
def test_get_method(self): # GIVEN m1 = Movie.objects.create(title='TestTitle1', year=111, plot='TetPlot1', genre='TestGenre') m2 = Movie.objects.create(title='TestTitle2', year=111, plot='TetPlot2', genre='TestGenre') # WHEN view = MoviesListViewSet.as_view() request = factory.get("/") response = view(request) # THEN response.render() self.assertEqual(response.status_code, 200) self.assertEqual(response.data['count'], 2) self.assertSetEqual( {m1.title, m2.title}, {movie['title'] for movie in response.data['results']})