def test_authorize_url(self, fake): client = Client(host='host.com', raise_for_status=False) base_url = client._build_url(path='www.themoviedb.org/') with self.mock_request(fake, json=dict(request_token='token')): expected_url = '%s/%s' % (base_url, 'token') self.assertEqual(client.authorize_url, expected_url) # check if this works with a redirect_uri client = Client(host='host.com', redirect_url='/redirect.com') expected_url = '%s?%s' % (expected_url, '/redirect.com') self.assertEqual(client.authorize_url, expected_url)
def test_ignore_failing_delete_request(self, fake): client = Client(raise_for_status=False) for status in (500, 501, 502, 504, 505): with self.mock_request(fake, status): try: client.delete('/path') Client().delete('path') # test the default version except HTTPError: self.fail('client.delete() raises an exception when' '``raise_for_status`` is set to False')
def setUp(self): self.tmdb_client = Client() self.request_response = { "results": [{ "title": "0940", "id": 9 }, { "title": "RGui", "id": 41 }], "total_results": 2, "page": 1, "total_pages": 1 }
class NowPlayingTest(TestCase): def setUp(self): self.tmdb_client = Client() for i in range(5): Movie.objects.create(id=i, title="movie %d" % i, update_level=100) npq = NowPlayingQuery.objects.create(page=1, total_pages=2, total_results=4) npq.movies.add(Movie.objects.get(id=0)) npq.movies.add(Movie.objects.get(id=1)) npq = NowPlayingQuery.objects.create(page=2, total_pages=2, total_results=4) npq.movies.add(Movie.objects.get(id=2)) npq.movies.add(Movie.objects.get(id=3)) def tearDown(self): Movie.objects.all().delete() NowPlayingQuery.objects.all().delete() def test_returns_list_with_movies(self): data = self.tmdb_client.get_nowplaying_movies(page=1) movies_id = set(movie["id"] for movie in data["movies"]) self.assertEqual(movies_id, set([0, 1])) def test_returns_proper_page(self): data = self.tmdb_client.get_nowplaying_movies(page=2) self.assertEqual(data["page"], 2) @patch("tmdb.models.tmdb_request") def test_returns_empty_list_for_out_of_range_page(self, tmdb_mock): tmdb_mock.return_value = { "page": 3, "results": [], "total_pages": 2, "total_results": 4 } data = self.tmdb_client.get_nowplaying_movies(page=3) self.assertEqual(len(data["movies"]), 0) @patch("tmdb.models.tmdb_request") def test_returns_status_error_when_request_exception(self, mock_request): mock_request.side_effect = RequestException() data = self.tmdb_client.get_nowplaying_movies(page=3) self.assertIn("w2w_status", data) self.assertEqual(data["w2w_status"], "ERROR")
def setUp(self): self.tmdb_client = Client() for i in range(5): Movie.objects.create(id=i, title="movie %d" % i, update_level=100) npq = NowPlayingQuery.objects.create(page=1, total_pages=2, total_results=4) npq.movies.add(Movie.objects.get(id=0)) npq.movies.add(Movie.objects.get(id=1)) npq = NowPlayingQuery.objects.create(page=2, total_pages=2, total_results=4) npq.movies.add(Movie.objects.get(id=2)) npq.movies.add(Movie.objects.get(id=3))
def popular_movies(request): if request.method == "POST": page = int(request.POST.get("page", 1)) else: page = int(request.GET.get("page", 1)) data = Client().get_popular_movies( page = page, force_update = TMDB_FORCE_UPDATE ) return JsonResponse(data)
def search_movies(request): if request.method == "POST": page = int(request.POST.get("page", 1)) query = request.POST.get("query", None) else: page = int(request.GET.get("page", 1)) query = request.GET.get("query", None) if query: query = "+".join(re.findall(r"(\w+)", query)) data = Client().search_movies(query, page) return JsonResponse(data)
def test_returns_status_error_when_movie_exception(self, mock_request): mock_request.return_value = self.mock_response mock_request.side_effect = Movie.DoesNotExist() data = Client().get_movie(id=550) self.assertIn("w2w_status", data) self.assertEqual(data["w2w_status"], "ERROR")
def test_returns_status_ok(self, mock_request): mock_request.return_value = self.mock_response data = Client().get_movie(id=550) self.assertIn("w2w_status", data) self.assertEqual(data["w2w_status"], "OK")
def test_returns_data(self, mock_request): mock_request.return_value = self.mock_response data = Client().get_movie(id=550) self.assertTrue( all(item in data.items() for item in self.mock_response.items()))
def test_returns_dict_instance(self, mock_request): mock_request.return_value = self.mock_response data = Client().get_movie(id=550) self.assertIsInstance(data, dict)
class ClientSearchTest(TestCase): def setUp(self): self.tmdb_client = Client() self.request_response = { "results": [{ "title": "0940", "id": 9 }, { "title": "RGui", "id": 41 }], "total_results": 2, "page": 1, "total_pages": 1 } @patch("tmdb.client.tmdb_request") def test_call_search_with_proper_arguments(self, tmdb_mock): query = "Terminator" response = self.tmdb_client.search_movies(query=query, page=1) tmdb_mock.assert_called_once_with(method="POST", path="search/movie", params={ "page": 1, "query": query }) @patch("requests.request") def test_get_searching_results(self, request_mock): request_mock.return_value.json.return_value = self.request_response response = self.tmdb_client.search_movies(query="terminator", page=1) self.assertEqual(response["total_results"], 2) self.assertEqual(len(response["movies"]), 2) @patch("requests.request") def test_for_multikeywords_search(self, request_mock): request_mock.return_value.json.return_value = self.request_response response = self.tmdb_client.search_movies(query="fight club", page=1) self.assertEqual(response["total_results"], 2) self.assertEqual(len(response["movies"]), 2) @patch("requests.request") def test_save_search_movies_in_database(self, request_mock): request_mock.return_value.json.return_value = self.request_response self.tmdb_client.search_movies(query="terminator", page=1) self.assertEqual(Movie.objects.count(), 2) @patch("requests.request") def test_search_empty_query(self, request_mock): request_mock.return_value.json.return_value = { "page": 1, "results": [], "total_pages": 1, "total_results": 0 } response = self.tmdb_client.search_movies(query="sdfsdfsfsdf", page=1) self.assertEqual(response["total_results"], 0) self.assertEqual(response["page"], 1) @patch("requests.request") def test_search_for_no_matching_query(self, request_mock): request_mock.return_value.json.return_value = { "page": 1, "results": [], "total_results": 0, "total_pages": 1 } response = self.tmdb_client.search_movies(query="sdfsdfsfsdf", page=1) self.assertEqual(response["total_results"], 0) self.assertEqual(response["page"], 1)
def test_api_key(self): client = Client() self.assertEqual(client.api_key, 'key')
def movie_info(request, id): data = Client().get_movie(id = int(id), min_update_level = INFO_UPDATE_LEVEL, force_update = TMDB_FORCE_UPDATE) return JsonResponse(data, safe=False)
def test_method_does_not_exist(self): with self.assertRaises(AttributeError): Client().method() with self.assertRaises(TypeError): Client()._request('unknown')
def test_delete_request_fails(self, fake): client = Client(raise_for_status=True) for status in (500, 501, 502, 504, 505): with self.mock_request(fake, status): self.assertRaises(HTTPError, lambda: client.delete('/path'))
def setUp(self): self.tmdb_client = Client() self.mock_response = {"id": 550, "title": "Fight Club"}
def test_session_id(self, fake): client = Client(access_token='token') with self.mock_request(fake, json={'session_id': 'id'}): self.assertEqual(client.session_id, 'id')