Files
omdb/tests/api_test.py
Nettika 300c7d7763 Write API wrapper class
- Query by IMDb IMDb
- Query by title

Remove the pytest-httpx plugin

Cleanup some of the omdb.result unit tests
2024-05-06 12:42:20 -07:00

131 lines
4.1 KiB
Python

from unittest.mock import patch
import pytest
from httpx import Client, MockTransport, QueryParams, Request, Response
from omdb.api import OMDB_SEARCH_API, OmdbApi
from omdb.error import OmdbError
from omdb.result import SeriesExtendedResult
def test_omdb_api():
"""Tests the `OmdbApi` class initialization."""
client = Client()
omdb = OmdbApi("key", client)
assert omdb.api_key == "key"
assert omdb.client is client
def test_omdb_api_search_data():
"""Tests the `OmdbApi._search` method when data is returned."""
def handler(request: Request):
assert request.method == "GET"
assert request.url.copy_with(params=None) == OMDB_SEARCH_API
assert request.url.params == QueryParams(apikey="key")
return Response(200, json={"Response": "True"})
omdb = OmdbApi("key", Client(transport=MockTransport(handler)))
data = omdb._search(QueryParams())
assert data == {"Response": "True"}
def test_omdb_api_search_error():
"""Tests the `OmdbApi._search` method when an error is raised."""
def handler(request: Request):
assert request.method == "GET"
assert request.url.copy_with(params=None) == OMDB_SEARCH_API
assert request.url.params == QueryParams(apikey="key")
return Response(404, json={"Response": "False", "Error": "Not Found"})
omdb = OmdbApi("key", Client(transport=MockTransport(handler)))
with pytest.raises(OmdbError, match="Not Found"):
omdb._search(QueryParams())
def test_omdb_api_query_imdb_id():
"""Tests the `OmdbApi.query_imdb_id` method."""
def handler(request: Request):
assert request.method == "GET"
assert request.url.copy_with(params=None) == OMDB_SEARCH_API
assert request.url.params == QueryParams(
apikey="key",
i="tt0000000",
plot="short",
)
return Response(200, json={})
omdb = OmdbApi("key", Client(transport=MockTransport(handler)))
with patch("omdb.result.ExtendedResult.parse") as mock_parse:
omdb.query_imdb_id("tt0000000", plot_mode="short")
mock_parse.assert_called()
def test_omdb_api_query_title():
"""Tests the `OmdbApi.query_title` method."""
def handler(request: Request):
assert request.method == "GET"
assert request.url.copy_with(params=None) == OMDB_SEARCH_API
assert request.url.params == QueryParams(
apikey="key",
t="Title",
y="2000",
plot="short",
)
return Response(200, json={})
omdb = OmdbApi("key", Client(transport=MockTransport(handler)))
with patch("omdb.result.ExtendedResult.parse") as mock_parse:
omdb.query_title("Title", year=2000, plot_mode="short")
mock_parse.assert_called()
def test_omdb_api_query_movie_title():
"""Tests the `OmdbApi.query_movie_title` method."""
def handler(request: Request):
assert request.method == "GET"
assert request.url.copy_with(params=None) == OMDB_SEARCH_API
assert request.url.params == QueryParams(
apikey="key",
t="Title",
type="movie",
y="2000",
plot="short",
)
return Response(200, json={})
omdb = OmdbApi("key", Client(transport=MockTransport(handler)))
with patch("omdb.result.MovieExtendedResult.parse") as mock_parse:
omdb.query_movie_title("Title", year=2000, plot_mode="short")
mock_parse.assert_called()
def test_omdb_api_query_series_title():
"""Tests the `OmdbApi.query_series_title` method."""
def handler(request: Request):
assert request.method == "GET"
assert request.url.copy_with(params=None) == OMDB_SEARCH_API
assert request.url.params == QueryParams(
apikey="key",
t="Title",
type="series",
y="2000",
plot="short",
)
return Response(200, json={})
omdb = OmdbApi("key", Client(transport=MockTransport(handler)))
with patch("omdb.result.SeriesExtendedResult.parse") as mock_parse:
omdb.query_series_title("Title", year=2000, plot_mode="short")
mock_parse.assert_called()