Coverage for tests\movie\test_movie_api.py: 100%
50 statements
« prev ^ index » next coverage.py v7.10.1, created at 2025-10-19 21:13 +0800
« prev ^ index » next coverage.py v7.10.1, created at 2025-10-19 21:13 +0800
1from pydantic import Field
2from requests.exceptions import RequestException
3from src.utility.models import ListParams, ReviewParams, SearchParams
4from tests.conftest import get_json_val
5import pytest
8@pytest.mark.parametrize("path", [
9 "/movies",
10 "/movies//reviews",
11 "/list-movie",
12 "/list-movie/vod",
13 "/list-movie/award",
14 "/list-movie/year/2010s/2019",
15 "/list-movie/year",
16 "/list-movie/country",
17 "/list-movie/genre",
18 "/list-movie/distributor",
19 "/list-movie/series",
20 "/list-movie/tag",
21 "/list-movie/person",
22])
23def test_invalid_endpoint_base(client_nc, path) -> None:
24 resp = client_nc.get(path)
25 resp_data = resp.json()
27 assert resp.status_code == 404
28 assert get_json_val(resp_data, "$.detail") == "Not Found"
31@pytest.mark.parametrize("path", [
32 "/movies/9999999999",
33 "/movies/9999999999/reviews",
34 "/movies/9999999999/reviews/9999999999",
35 "/list-movie/vod/invalid_vod",
36 "/list-movie/award/9999999999",
37 "/list-movie/year/999s",
38 "/list-movie/year/999",
39 "/list-movie/country/9999999999",
40 "/list-movie/genre/9999999999",
41 "/list-movie/distributor/9999999999",
42 "/list-movie/series/9999999999",
43 "/list-movie/tag/invalid_tag",
44 "/list-movie/person/9999999999",
45])
46def test_invalid_endpoint_filmarks(client_nc, path, caplog) -> None:
47 resp = client_nc.get(path)
48 resp_data = resp.json()
50 assert resp.status_code == 404
51 assert get_json_val(resp_data, "$.detail") == "The requested resource could not be found."
52 assert "Invalid Filmarks page requested" in caplog.text
55@pytest.mark.parametrize("test_data", [
56 (
57 "/search/movies?q=test500",
58 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
59 "Failed to search movies."
60 ),
61 (
62 "/movies/500",
63 "src.scrape.info.info_movie_scraper.InfoMovieScraper.scrape",
64 "Failed to retrieve information for movie with ID: 500."
65 ),
66 (
67 "/movies/500/reviews",
68 "src.scrape.info.info_movie_scraper.InfoMovieScraper.scrape",
69 "Failed to retrieve reviews for movie with ID: 500."
70 ),
71 (
72 "/movies/500/reviews/500",
73 "src.scrape.info.info_movie_scraper.InfoMovieScraper.scrape",
74 "Failed to retrieve review for movie with ID: 500 and review ID: 500."
75 ),
76 (
77 "/list-movie/now",
78 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
79 "Failed to fetch currently screening movies."
80 ),
81 (
82 "/list-movie/coming-soon",
83 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
84 "Failed to fetch upcoming movies."
85 ),
86 (
87 "/list-movie/opening-this-week",
88 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
89 "Failed to fetch movies opening this week."
90 ),
91 (
92 "/list-movie/trend",
93 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
94 "Failed to fetch trending movies."
95 ),
96 (
97 "/list-movie/vod/500_vod",
98 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
99 "Failed to fetch movies from VOD service: 500_vod."
100 ),
101 (
102 "/list-movie/award/500",
103 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
104 "Failed to fetch movies with award ID: 500."
105 ),
106 (
107 "/list-movie/year/500s",
108 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
109 "Failed to fetch movies from year series: 500s."
110 ),
111 (
112 "/list-movie/year/500",
113 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
114 "Failed to fetch movies from year: 500."
115 ),
116 (
117 "/list-movie/country/500",
118 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
119 "Failed to fetch movies with country ID: 500."
120 ),
121 (
122 "/list-movie/genre/500",
123 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
124 "Failed to fetch movies with genre ID: 500."
125 ),
126 (
127 "/list-movie/distributor/500",
128 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
129 "Failed to fetch movies with distributor ID: 500."
130 ),
131 (
132 "/list-movie/series/500",
133 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
134 "Failed to fetch movies with series ID: 500."
135 ),
136 (
137 "/list-movie/tag/500_tag",
138 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
139 "Failed to fetch movies with tag: 500_tag."
140 ),
141 (
142 "/list-movie/person/500",
143 "src.scrape.search.search_movie_scraper.SearchMovieScraper.scrape",
144 "Failed to fetch movies with person ID: 500."
145 ),
146])
147def test_scrape_error_500_server_error(client_nc, mocker, test_data, caplog) -> None:
148 mocker.patch(
149 target=test_data[1],
150 side_effect=Exception("Testing - 500 Internal Server Error"),
151 )
153 resp = client_nc.get(test_data[0])
154 resp_data = resp.json()
156 assert resp.status_code == 500
157 assert get_json_val(resp_data, "$.detail") == "The server encountered an unexpected error."
158 assert "Testing - 500 Internal Server Error" in caplog.text
159 assert test_data[2] in caplog.text
162@pytest.mark.parametrize("path", [
163 "/search/movies?q=test503",
164 "/movies/503",
165 "/movies/503/reviews",
166 "/movies/503/reviews/503",
167 "/list-movie/now",
168 "/list-movie/coming-soon",
169 "/list-movie/opening-this-week",
170 "/list-movie/trend",
171 "/list-movie/vod/503_vod",
172 "/list-movie/award/503",
173 "/list-movie/year/503s",
174 "/list-movie/year/503",
175 "/list-movie/country/503",
176 "/list-movie/genre/503",
177 "/list-movie/distributor/503",
178 "/list-movie/series/503",
179 "/list-movie/tag/503_tag",
180 "/list-movie/person/503",
181])
182def test_scrape_error_503_service_unavailable_session(client_nc, mocker, path, caplog) -> None:
183 mocker.patch(
184 target="src.scrape.base_scraper.Session",
185 side_effect=RequestException("Testing - 503 Service Unavailable"),
186 )
188 resp = client_nc.get(path)
189 resp_data = resp.json()
191 assert resp.status_code == 503
192 assert get_json_val(resp_data, "$.detail") == "The service is currently unavailable."
193 assert "Request to Filmarks failed: 'Testing - 503 Service Unavailable'" in caplog.text
196@pytest.mark.parametrize("path", [
197 "/movies/14348/reviews?page=9999999999999999999",
198 "/list-movie/now?page=999999999999999999",
199 "/list-movie/coming-soon?page=999999999999999999",
200 "/list-movie/opening-this-week?page=999999999999999999",
201 "/list-movie/trend?page=999999999999999999",
202 "/list-movie/vod/prime_video?page=999999999999999999",
203 "/list-movie/award/19?page=999999999999999999",
204 "/list-movie/year/2010s?page=999999999999999999",
205 "/list-movie/year/2001?page=999999999999999999",
206 "/list-movie/country/5?page=999999999999999999",
207 "/list-movie/genre/903?page=999999999999999999",
208 "/list-movie/distributor/503?page=999999999999999999",
209 "/list-movie/series/1?page=999999999999999999",
210 "/list-movie/tag/洋画?page=999999999999999999",
211 "/list-movie/person/93709?page=999999999999999999",
212])
213def test_scrape_error_503_service_unavailable_filmarks(client_nc, path, caplog) -> None:
214 class CustomParams():
215 page: int = Field(1, gt=0)
216 client_nc.app.dependency_overrides[SearchParams] = CustomParams
217 client_nc.app.dependency_overrides[ReviewParams] = CustomParams
218 client_nc.app.dependency_overrides[ListParams] = CustomParams
220 resp = client_nc.get(path)
221 resp_data = resp.json()
223 assert resp.status_code == 503
224 assert get_json_val(resp_data, "$.detail") == "The service is currently unavailable."
225 assert "Filmarks is temporarily unavailable" in caplog.text
227 del client_nc.app.dependency_overrides[SearchParams]
228 del client_nc.app.dependency_overrides[ReviewParams]
229 del client_nc.app.dependency_overrides[ListParams]