maubot-tmdb/test_tmdb.py

163 lines
5.7 KiB
Python
Raw Normal View History

2020-06-22 20:21:22 +01:00
#!/usr/bin/env python3
import unittest
2021-03-16 16:56:02 +00:00
import requests
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
from tmdb.tmdb import TmdbBot, MessageConstructor
2020-06-23 16:34:34 +01:00
from tmdb.database import Database
from sqlalchemy import create_engine
2021-03-16 20:45:27 +00:00
2021-03-16 16:56:02 +00:00
def apiRequests(command):
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'})
return requests.get(url, params=params).json()
2020-06-22 20:21:22 +01:00
class TestTmdbMethods(unittest.TestCase):
# TMDB API
2020-06-22 20:21:22 +01:00
def test_search_item(self):
movie = Movie()
id = movie.search_title('Breakfast Club')
self.assertEqual(id, 2108)
2021-08-20 09:07:57 +01:00
self.assertEqual(movie.valid, True)
2020-06-22 20:21:22 +01:00
def test_cast(self):
movie = Movie()
movie.search_title('Breakfast Club')
self.assertEqual('Emilio Estevez', movie.cast[0])
2020-06-22 20:21:22 +01:00
def test_title(self):
movie = Movie()
movie.search_title('Breakfast Club')
self.assertEqual('The Breakfast Club', movie.title)
2020-06-22 20:21:22 +01:00
def test_overview(self):
movie = Movie()
movie.search_title('Breakfast Club')
description = 'Five high school students from different walks of'
2020-06-22 20:21:22 +01:00
self.assertEqual(description, movie.overview[:len(description)])
2020-06-22 20:21:22 +01:00
def test_change_language(self):
movie = Movie()
movie.set_language('en')
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)])
def test_html_construction(self):
movie = Movie()
movie.query_details('550')
constructor = MessageConstructor(movie)
constructor.overview_length = 10
message = constructor.construct_html_message()
2020-06-24 09:51:42 +01:00
self.assertEqual(message, """<p><a href="https://www.themoviedb.org/movie/550"><b>Fight Club</b></a></p>
<p>A ticking- [...]</p>
<p>Acting: Edward Norton, Brad Pitt, Helena Bonham Carter</p>
2020-06-24 10:17:19 +01:00
<p>Taken from www.themoviedb.org</p>""")
2020-06-23 16:34:34 +01:00
def test_database_language(self):
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')
2020-06-24 09:45:18 +01:00
def test_id_lookup(self):
movie = Movie()
movie.query_details('2108')
self.assertEqual('The Breakfast Club', movie.title)
def test_search_fails(self):
movie = Movie()
id = movie.search_title('Breakfast Club 2019')
self.assertEqual(id, None)
self.assertEqual(None, movie.title)
2021-08-20 09:07:57 +01:00
self.assertEqual(movie.valid, False)
2020-06-26 15:41:44 +01:00
def test_search_year(self):
movie = Movie()
id = movie.search_title('Dune')
self.assertEqual(id, 438631)
id = movie.search_title('Dune', 1984)
self.assertEqual(id, 841)
2020-06-26 15:41:44 +01:00
def test_split_year(self):
2021-08-20 09:31:47 +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)
def test_set_poster_size(self):
movie = Movie()
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")
size = movie.set_poster_size("w666")
self.assertEqual(size, None)
2021-08-20 09:39:08 +01:00
2021-08-20 09:07:57 +01:00
def test_year_no_y(self):
movie = Movie()
id = movie.search_title('infinite 2021')
self.assertEqual(id, None)
self.assertEqual(movie.valid, False)
2020-06-26 15:41:44 +01:00
# TV Shows
2020-06-24 09:45:18 +01:00
def test_search_tvshow(self):
movie = TvShow()
id = movie.search_title('The Flash')
self.assertEqual(id, 60735)
2020-06-24 09:45:18 +01:00
def test_tv_title(self):
movie = TvShow()
movie.search_title('The Flash')
self.assertEqual('The Flash', movie.title)
def test_cast_2(self):
2020-06-24 09:45:18 +01:00
movie = TvShow()
movie.search_title('The Flash')
self.assertEqual('Danielle Panabaker', movie.cast[2])
def test_poster_path(self):
movie = Movie()
movie.search_title('Dune')
2021-08-20 09:07:57 +01:00
self.assertEqual(movie.poster_url, "http://image.tmdb.org/t/p/w92/cDbNAY0KM84cxXhmj8f0dLWza3t.jpg")
2020-06-24 09:45:18 +01:00
2021-03-16 16:56:02 +00:00
def test_movie_popular_length(self):
results = apiRequests('/movie/popular')
list = MoviePopular()
self.assertEqual(list.query(), results['total_results'])
2021-03-16 20:45:27 +00:00
def test_movie_popular_id(self):
results = apiRequests('/movie/popular')
list = MoviePopular()
list.query()
self.assertEqual(list.list[2]['id'], results['results'][2]['id'])
def test_movie_popular_html(self):
results = apiRequests('/movie/popular')
list = MoviePopular()
list.query()
test_result = escape(results['results'][-1]['title'])
tested = list.getListHtml()[(len(results['results'][-1]['title']) + 8) * -1:]
self.assertEqual(tested, f"""{test_result}</a></p>""")
def test_movie_popular_text(self):
results = apiRequests('/movie/popular')
list = MoviePopular()
list.query()
test_result = results['results'][-1]['title']
tested = list.getListText()[(len(results['results'][-1]['title'])) * -1:]
self.assertEqual(tested, test_result)
2020-06-22 20:21:22 +01:00
if __name__ == '__main__':
unittest.main()