1# -*- coding: utf-8 -*-
2import pytest
3
4from flexget.api.app import base_message
5from flexget.components.tvmaze.api import ObjectsContainer as OC
6from flexget.utils import json
7
8
9@pytest.mark.online
10class TestTVMAzeSeriesLookupAPI:
11    config = 'tasks: {}'
12
13    def test_tvmaze_series_lookup_by_name(self, api_client, schema_match):
14        values = {
15            'language': 'English',
16            'name': 'The X-Files',
17            'network': 'FOX',
18            'show_type': 'Scripted',
19            'tvdb_id': 77398,
20            'tvmaze_id': 430,
21            'tvrage_id': 6312,
22            'url': 'http://www.tvmaze.com/shows/430/the-x-files',
23            'webchannel': None,
24            'year': 1993,
25        }
26
27        rsp = api_client.get('/tvmaze/series/The X-Files/')
28        assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code
29
30        data = json.loads(rsp.get_data(as_text=True))
31
32        errors = schema_match(OC.tvmaze_series_object, data)
33        assert not errors
34
35        for field, value in values.items():
36            assert data.get(field) == value
37
38        rsp = api_client.get('/tvmaze/series/sdfgv35wvg23vg2/')
39        assert rsp.status_code == 404, 'Response code is %s' % rsp.status_code
40
41        data = json.loads(rsp.get_data(as_text=True))
42
43        errors = schema_match(base_message, data)
44        assert not errors
45
46    def test_tvmaze_series_lookup_by_id(self, api_client, schema_match):
47        rsp = api_client.get('/tvmaze/series/13/')
48        assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code
49
50        data = json.loads(rsp.get_data(as_text=True))
51
52        errors = schema_match(OC.tvmaze_series_object, data)
53        assert not errors
54
55        values = {
56            'language': 'English',
57            'name': 'The Flash',
58            'network': 'The CW',
59            'show_type': 'Scripted',
60            'tvdb_id': 279121,
61            'tvmaze_id': 13,
62            'tvrage_id': 36939,
63            'url': 'http://www.tvmaze.com/shows/13/the-flash',
64            'year': 2014,
65        }
66
67        for field, value in values.items():
68            assert data.get(field) == value
69
70    def test_tvmaze_episode_by_ep_and_season(self, api_client, schema_match):
71        rsp = api_client.get('/tvmaze/episode/13/?season_num=1&ep_num=1')
72        assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code
73
74        data = json.loads(rsp.get_data(as_text=True))
75
76        errors = schema_match(OC.tvmaze_episode_object, data)
77        assert not errors
78
79        values = {
80            'number': 1,
81            'runtime': 60,
82            'season_number': 1,
83            'series_id': 13,
84            'title': 'City of Heroes',
85            'tvmaze_id': 592,
86            'url': 'http://www.tvmaze.com/episodes/592/the-flash-1x01-city-of-heroes',
87        }
88
89        for field, value in values.items():
90            assert data.get(field) == value
91
92    def test_tvmaze_episode_by_air_date(self, api_client, schema_match):
93        rsp = api_client.get('/tvmaze/episode/3928/?air_date=2016-09-12')
94        assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code
95
96        data = json.loads(rsp.get_data(as_text=True))
97
98        errors = schema_match(OC.tvmaze_episode_object, data)
99        assert not errors
100
101        values = {
102            'number': 113,
103            'runtime': 30,
104            'season_number': 2016,
105            'series_id': 3928,
106            'summary': '<p>Rapper T.I. Harris.</p>',
107            'title': 'T.I. Harris',
108            'tvmaze_id': 925421,
109            'url': 'http://www.tvmaze.com/episodes/925421/the-daily-show-with-trevor-noah-2016-09-12-ti-harris',
110        }
111
112        for field, value in values.items():
113            assert data.get(field) == value
114
115        rsp = api_client.get('/tvmaze/episode/3928/')
116        assert rsp.status_code == 400, 'Response code is %s' % rsp.status_code
117
118        data = json.loads(rsp.get_data(as_text=True))
119
120        errors = schema_match(base_message, data)
121        assert not errors
122
123        rsp = api_client.get('/tvmaze/episode/3928/?season_num=1')
124        assert rsp.status_code == 400, 'Response code is %s' % rsp.status_code
125
126        data = json.loads(rsp.get_data(as_text=True))
127
128        errors = schema_match(base_message, data)
129        assert not errors
130
131    def test_tvmaze_episode_lookup_error(self, api_client, schema_match):
132        rsp = api_client.get('/tvmaze/episode/13/?season_num=100&ep_num=100')
133        assert rsp.status_code == 404, 'Response code is %s' % rsp.status_code
134
135        data = json.loads(rsp.get_data(as_text=True))
136
137        errors = schema_match(base_message, data)
138        assert not errors
139