2020-06-22 20:21:22 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import unittest
|
2021-03-16 20:45:27 +00:00
|
|
|
from html import escape
|
2021-03-16 16:56:02 +00:00
|
|
|
from tmdb.tmdb_api import Movie, TvShow, MoviePopular
|
2021-09-23 21:22:35 +01:00
|
|
|
from tmdb.tmdb import TmdbBot
|
2020-06-23 16:34:34 +01:00
|
|
|
from tmdb.database import Database
|
|
|
|
from sqlalchemy import create_engine
|
2021-09-23 21:22:35 +01:00
|
|
|
import aiohttp
|
2021-03-16 20:45:27 +00:00
|
|
|
|
2021-09-23 21:22:35 +01:00
|
|
|
|
|
|
|
async def apiRequests(command):
|
2021-03-16 16:56:02 +00:00
|
|
|
api_key = '51d75c00dc1502dc894b7773ec3e7a15'
|
|
|
|
base_url = "https://api.themoviedb.org/3/"
|
|
|
|
url = base_url + command.lstrip('/')
|
2021-03-16 20:45:27 +00:00
|
|
|
params = {'api_key': api_key}
|
2021-03-16 16:56:02 +00:00
|
|
|
params.update({'language': 'en'})
|
2021-09-23 21:22:35 +01:00
|
|
|
async with aiohttp.ClientSession() as client:
|
|
|
|
async with client.get(url, params=params) as resp:
|
|
|
|
return await resp.json()
|
2021-03-16 16:56:02 +00:00
|
|
|
|
2020-06-22 20:21:22 +01:00
|
|
|
|
2021-05-14 16:15:13 +01:00
|
|
|
class TestTmdbMethods(unittest.IsolatedAsyncioTestCase):
|
2021-03-16 15:35:02 +00:00
|
|
|
# TMDB API
|
2021-05-14 16:15:13 +01:00
|
|
|
async def test_search_item(self):
|
2020-06-22 20:21:22 +01:00
|
|
|
movie = Movie()
|
2021-05-14 16:15:13 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
id = await movie.search_title('Breakfast Club')
|
2020-06-22 20:21:22 +01:00
|
|
|
self.assertEqual(id, 2108)
|
2021-05-14 16:15:13 +01:00
|
|
|
await movie.close_session()
|
2021-09-23 21:09:49 +01:00
|
|
|
|
|
|
|
async def test_cast(self):
|
2020-06-22 20:21:22 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.search_title('Breakfast Club')
|
2021-03-16 15:35:02 +00:00
|
|
|
self.assertEqual('Emilio Estevez', movie.cast[0])
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_title(self):
|
2020-06-22 20:21:22 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.search_title('Breakfast Club')
|
2020-06-22 20:21:22 +01:00
|
|
|
self.assertEqual('The Breakfast Club', movie.title)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_overview(self):
|
2020-06-22 20:21:22 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.search_title('Breakfast Club')
|
2020-09-09 14:18:22 +01:00
|
|
|
description = 'Five high school students from different walks of'
|
2020-06-22 20:21:22 +01:00
|
|
|
self.assertEqual(description, movie.overview[:len(description)])
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_change_language(self):
|
2020-06-22 20:21:22 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
2020-06-22 20:21:22 +01:00
|
|
|
movie.set_language('en')
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.search_title('Breakfast Club')
|
2020-06-22 20:21:22 +01:00
|
|
|
description = 'Five high school students from different walks of life endure a Saturday detention'
|
|
|
|
self.assertEqual(description, movie.overview[:len(description)])
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-23 14:47:40 +01:00
|
|
|
|
2020-06-23 16:34:34 +01:00
|
|
|
def test_database_language(self):
|
2021-03-16 15:35:02 +00:00
|
|
|
engine = create_engine('sqlite:///test.db', echo=True)
|
2020-06-23 16:34:34 +01:00
|
|
|
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')
|
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_id_lookup(self):
|
2020-06-24 09:45:18 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.query_details('2108')
|
2020-06-24 09:45:18 +01:00
|
|
|
self.assertEqual('The Breakfast Club', movie.title)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-24 09:45:18 +01:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_search_fails(self):
|
2020-06-24 09:45:18 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
id = await movie.search_title('Breakfast Club 2019')
|
2020-06-24 09:45:18 +01:00
|
|
|
self.assertEqual(id, None)
|
|
|
|
self.assertEqual(None, movie.title)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-26 15:41:44 +01:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_search_year(self):
|
2020-06-26 15:41:44 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
id = await movie.search_title('Dune')
|
2020-06-26 15:41:44 +01:00
|
|
|
self.assertEqual(id, 438631)
|
2021-09-23 21:09:49 +01:00
|
|
|
id = await movie.search_title('Dune', 1984)
|
2021-01-16 08:23:51 +00:00
|
|
|
self.assertEqual(id, 841)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-26 15:41:44 +01:00
|
|
|
|
|
|
|
def test_split_year(self):
|
2021-09-23 21:32:07 +01:00
|
|
|
tmdb = TmdbBot("", "", "", "", "", "", "", "", "", "")
|
2020-06-26 15:41:44 +01:00
|
|
|
title, year = tmdb.split_title_year('Dune')
|
|
|
|
self.assertEqual('Dune', title)
|
|
|
|
self.assertEqual(None, year)
|
|
|
|
title, year = tmdb.split_title_year('Dune y:2020 ')
|
|
|
|
self.assertEqual('Dune', title)
|
|
|
|
self.assertEqual(2020, year)
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_set_poster_size(self):
|
2020-09-09 14:18:22 +01:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
2020-09-09 14:18:22 +01:00
|
|
|
size = movie.set_poster_size("w500")
|
|
|
|
self.assertEqual(size, "w500")
|
2020-09-11 10:15:04 +01:00
|
|
|
self.assertEqual(movie.base_url_poster, f"{movie.base_url_images}w500")
|
2020-09-09 14:18:22 +01:00
|
|
|
size = movie.set_poster_size("w666")
|
|
|
|
self.assertEqual(size, None)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-26 15:41:44 +01:00
|
|
|
|
2021-03-16 15:35:02 +00:00
|
|
|
# TV Shows
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_search_tvshow(self):
|
2020-06-24 09:45:18 +01:00
|
|
|
movie = TvShow()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
id = await movie.search_title('The Flash')
|
2021-03-15 15:04:15 +00:00
|
|
|
self.assertEqual(id, 60735)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-24 09:45:18 +01:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_tv_title(self):
|
2020-06-24 09:45:18 +01:00
|
|
|
movie = TvShow()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.search_title('The Flash')
|
2020-06-24 09:45:18 +01:00
|
|
|
self.assertEqual('The Flash', movie.title)
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-24 09:45:18 +01:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_cast_2(self):
|
2020-06-24 09:45:18 +01:00
|
|
|
movie = TvShow()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.search_title('The Flash')
|
2021-03-15 15:04:15 +00:00
|
|
|
self.assertEqual('Danielle Panabaker', movie.cast[2])
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_poster_path(self):
|
2021-02-16 14:00:01 +00:00
|
|
|
movie = Movie()
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.load_parameters()
|
|
|
|
await movie.search_title('Dune')
|
2021-11-08 21:48:18 +00:00
|
|
|
self.assertEqual(movie.poster_url, "http://image.tmdb.org/t/p/w92/d5NXSklXo0qyIYkgV94XAgMIckC.jpg")
|
2021-09-23 21:09:49 +01:00
|
|
|
await movie.close_session()
|
2020-06-24 09:45:18 +01:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_movie_popular_length(self):
|
2021-09-23 21:22:35 +01:00
|
|
|
results = await apiRequests('/movie/popular')
|
2021-03-16 16:56:02 +00:00
|
|
|
list = MoviePopular()
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.load_parameters()
|
|
|
|
text = await list.query()
|
|
|
|
self.assertEqual(text, results['total_results'])
|
|
|
|
await list.close_session()
|
2021-03-16 15:35:02 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_movie_popular_id(self):
|
2021-09-23 21:22:35 +01:00
|
|
|
results = await apiRequests('/movie/popular')
|
2021-03-16 20:45:27 +00:00
|
|
|
list = MoviePopular()
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.load_parameters()
|
|
|
|
await list.query()
|
2021-03-16 20:45:27 +00:00
|
|
|
self.assertEqual(list.list[2]['id'], results['results'][2]['id'])
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.close_session()
|
2021-03-16 20:45:27 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_movie_popular_html(self):
|
2021-09-23 21:22:35 +01:00
|
|
|
results = await apiRequests('/movie/popular')
|
2021-03-16 20:45:27 +00:00
|
|
|
list = MoviePopular()
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.load_parameters()
|
|
|
|
await list.query()
|
2021-03-16 20:45:27 +00:00
|
|
|
test_result = escape(results['results'][-1]['title'])
|
2021-11-08 21:48:18 +00:00
|
|
|
tested = list.getListHtml()
|
2021-09-23 21:09:49 +01:00
|
|
|
tested = tested[(len(results['results'][-1]['title']) + 8) * -1:]
|
2021-03-16 20:45:27 +00:00
|
|
|
self.assertEqual(tested, f"""{test_result}</a></p>""")
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.close_session()
|
2021-03-16 20:45:27 +00:00
|
|
|
|
2021-09-23 21:09:49 +01:00
|
|
|
async def test_movie_popular_text(self):
|
2021-09-23 21:22:35 +01:00
|
|
|
results = await apiRequests('/movie/popular')
|
2021-03-16 20:45:27 +00:00
|
|
|
list = MoviePopular()
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.load_parameters()
|
|
|
|
await list.query()
|
2021-03-16 20:45:27 +00:00
|
|
|
test_result = results['results'][-1]['title']
|
2021-11-08 21:48:18 +00:00
|
|
|
tested = list.getListText()
|
2021-09-23 21:09:49 +01:00
|
|
|
tested = tested[(len(results['results'][-1]['title'])) * -1:]
|
2021-03-16 20:45:27 +00:00
|
|
|
self.assertEqual(tested, test_result)
|
2021-09-23 21:09:49 +01:00
|
|
|
await list.close_session()
|
2021-03-16 20:45:27 +00:00
|
|
|
|
2021-09-23 21:22:35 +01:00
|
|
|
|
2020-06-22 20:21:22 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|