maubot-tmdb/test_tmdb.py
Scott Wallace cc89e95403
Refactoring:
* Remove the movie poster parsing
* Fix some code issues
* Increase blurb trunctation length
2024-11-28 09:04:03 +00:00

241 lines
8.2 KiB
Python

"""
Unit tests
"""
import unittest
import aiohttp
from mautrix.util.logging.trace import TraceLogger
from sqlalchemy import create_engine
from tmdb.database import Database
from tmdb.tmdb import TmdbBot
from tmdb.tmdb_api import Movie, MoviePopular, TvShow
# pylint: disable=missing-class-docstring,missing-function-docstring
async def api_requests(command):
api_key = "51d75c00dc1502dc894b7773ec3e7a15"
base_url = "https://api.themoviedb.org/3/"
url = base_url + command.lstrip("/")
params = {"api_key": api_key}
params.update({"language": "en"})
async with aiohttp.ClientSession() as client:
async with client.get(url, params=params) as resp:
return await resp.json()
class TestTmdbMethods(unittest.IsolatedAsyncioTestCase):
# TMDB API
async def test_search_item(self):
movie = Movie()
await movie.load_parameters()
movie_id = await movie.search_title("Breakfast Club")
self.assertEqual(movie_id, 2108)
self.assertEqual(movie.valid, True)
await movie.close_session()
async def test_vote(self):
movie = Movie()
await movie.load_parameters()
await movie.search_title("Dune")
vote = movie.vote_average
self.assertGreaterEqual(vote, 7)
await movie.close_session()
async def test_cast(self):
movie = Movie()
await movie.load_parameters()
await movie.search_title("Breakfast Club")
self.assertEqual("Emilio Estevez", movie.cast[0])
await movie.close_session()
async def test_title(self):
movie = Movie()
await movie.load_parameters()
await movie.search_title("Breakfast Club")
self.assertEqual("The Breakfast Club", movie.title)
await movie.close_session()
async def test_overview(self):
movie = Movie()
await movie.load_parameters()
await movie.search_title("Breakfast Club")
description = "Five high school students from different walks of"
self.assertEqual(description, movie.overview[: len(description)])
await movie.close_session()
async def test_change_language(self):
movie = Movie()
await movie.load_parameters()
movie.set_language("en")
await movie.search_title("Breakfast Club")
description = (
"Five high school students from different walks of "
"life endure a Saturday detention"
)
self.assertEqual(description, movie.overview[: len(description)])
await movie.close_session()
def test_database_language(self):
engine = create_engine("sqlite:///test.db", echo=True)
db = Database(engine)
db.set_language("@testuser:example.com", "de")
self.assertEqual(str(db.get_language("@testuser:example.com")), "de")
db.set_language("@testuser:example.com", "en")
self.assertEqual(str(db.get_language("@testuser:example.com")), "en")
async def test_id_lookup(self):
movie = Movie()
await movie.load_parameters()
await movie.query_details("2108")
self.assertEqual("The Breakfast Club", movie.title)
await movie.close_session()
async def test_search_fails(self):
movie = Movie()
await movie.load_parameters()
movie_id = await movie.search_title("Breakfast Club 2019")
self.assertEqual(movie_id, 0)
self.assertEqual("", movie.title)
self.assertEqual(movie.valid, False)
await movie.close_session()
async def test_search_year(self):
movie = Movie()
await movie.load_parameters()
movie_id = await movie.search_title("Dune")
self.assertEqual(movie_id, 438631)
movie_id = await movie.search_title("Dune", 1984)
self.assertEqual(movie_id, 841)
await movie.close_session()
def test_split_year(self):
tmdb = TmdbBot(
client=None, # pyright: ignore[reportArgumentType]
loop=None, # pyright: ignore[reportArgumentType]
http=None, # pyright: ignore[reportArgumentType]
instance_id="",
log=TraceLogger(""),
config=None,
database=None,
webapp=None,
webapp_url="",
loader=None, # pyright: ignore[reportArgumentType]
)
title, year = tmdb.split_title_year("Dune")
self.assertEqual("Dune", title)
self.assertEqual(0, year)
title, year = tmdb.split_title_year("Dune y:2020 ")
self.assertEqual("Dune", title)
self.assertEqual(2020, year)
async def test_set_poster_size(self):
movie = Movie()
await movie.load_parameters()
size = movie.set_poster_size("w500")
self.assertEqual(size, "w500")
self.assertEqual(movie.base_url_poster, f"{movie.base_url_images}w500")
size = movie.set_poster_size("w666")
self.assertEqual(size, None)
await movie.close_session()
async def test_year_no_y(self):
movie = Movie()
await movie.load_parameters()
movie_id = await movie.search_title("infinite y:2021")
self.assertEqual(movie_id, 0)
self.assertEqual(movie.valid, False)
# TV Shows
async def test_search_tvshow(self):
movie = TvShow()
await movie.load_parameters()
movie_id = await movie.search_title("The Flash")
self.assertEqual(movie_id, 60735)
await movie.close_session()
async def test_tv_title(self):
movie = TvShow()
await movie.load_parameters()
await movie.search_title("The Flash")
self.assertEqual("The Flash", movie.title)
await movie.close_session()
async def test_search_tvshow_v(self):
show = TvShow()
await show.load_parameters()
mid = await show.search_title("V 2009")
self.assertEqual(mid, 21494)
await show.close_session()
async def test_tv_title_v(self):
movie = TvShow()
await movie.load_parameters()
await movie.search_title("V 1983")
self.assertEqual("V", movie.title)
await movie.close_session()
async def test_search_tvshow_v_1983(self):
movie = TvShow()
await movie.load_parameters()
mid = await movie.search_title("V", 1983)
self.assertEqual(mid, 14141)
await movie.close_session()
async def test_tv_title_v_1983(self):
movie = TvShow()
await movie.load_parameters()
await movie.search_title("V", 1983)
self.assertEqual("V", movie.title)
await movie.close_session()
async def test_cast_2(self):
movie = TvShow()
await movie.load_parameters()
await movie.search_title("The Flash")
self.assertEqual("Danielle Panabaker", movie.cast[2])
await movie.close_session()
# async def test_poster_path(self):
# movie = Movie()
# await movie.load_parameters()
# await movie.search_title("Dune")
# self.assertEqual(
# movie.poster_url,
# "http://image.tmdb.org/t/p/w92/d5NXSklXo0qyIYkgV94XAgMIckC.jpg",
# )
# await movie.close_session()
async def test_movie_popular_length(self):
results = await api_requests("/movie/popular")
movie_list = MoviePopular()
await movie_list.load_parameters()
text = await movie_list.query()
self.assertEqual(text, results["total_results"])
await movie_list.close_session()
async def test_movie_popular_id(self):
results = await api_requests("/movie/popular")
movie_list = MoviePopular()
await movie_list.load_parameters()
await movie_list.query()
self.assertEqual(movie_list.list[2]["id"], results["results"][2]["id"])
await movie_list.close_session()
async def test_movie_popular_text(self):
results = await api_requests("/movie/popular")
movie_list = MoviePopular()
await movie_list.load_parameters()
await movie_list.query()
test_result = results["results"][-1]["title"]
tested = movie_list.getListText()
tested = tested[(len(results["results"][-1]["title"])) * -1 :]
self.assertEqual(tested, test_result)
await movie_list.close_session()
if __name__ == "__main__":
unittest.main()