Coverage for tests\movie\test_movie_search.py: 100%
112 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 random import choice
2from tests.conftest import get_json_val, MOVIE_JPN, MOVIE_ENG
3import json
4import pytest
7@pytest.mark.parametrize("query", [
8 "?limit",
9 "?limit=",
10 "?limit=abc123",
11 "?limit=1",
12 "?page",
13 "?page=",
14 "?page=def456",
15 "?page=2",
16 "?q=ハリーポッター&limit=a",
17 "?q=ちはやふる&page=z",
18 "?q=ラストマイル&limit=@&page=1",
19])
20def test_search_input_not_valid_integer(client_nc, query) -> None:
21 resp = client_nc.get(f"/search/movies{query}")
22 resp_data = resp.json()
24 assert resp.status_code == 422
25 for err in get_json_val(resp_data, "$.detail"):
26 assert get_json_val(err, "$.msg") == "Input should be a valid integer, unable to parse string as an integer"
29@pytest.mark.parametrize("query", [
30 "?limit=-1",
31 "?limit=0",
32 "?page=-5",
33 "?page=0",
34 "?q=test1&limit=-1",
35 "?q=test2&page=-2",
36 "?q=test3&limit=1&page=0",
37])
38def test_search_input_less_than_min_threshold(client_nc, query) -> None:
39 resp = client_nc.get(f"/search/movies{query}")
40 resp_data = resp.json()
42 assert resp.status_code == 422
43 for err in get_json_val(resp_data, "$.detail"):
44 assert get_json_val(err, "$.msg") == "Input should be greater than 0"
47@pytest.mark.parametrize("query", [
48 "?limit=101",
49 "?page=10001",
50 "?q=test1&limit=101",
51 "?q=test2&page=10001",
52 "?q=test3&limit=101&page=3",
53])
54def test_search_input_more_than_max_threshold(client_nc, query) -> None:
55 resp = client_nc.get(f"/search/movies{query}")
56 resp_data = resp.json()
58 assert resp.status_code == 422
59 for err in get_json_val(resp_data, "$.detail"):
60 assert get_json_val(err, "$.msg") in {"Input should be less than or equal to 100", "Input should be less than or equal to 10000"}
63@pytest.mark.parametrize("query", [
64 "",
65 "?",
66 "?q",
67 "?q&page=2",
68 "?q=",
69 "?q=&limit=1",
70])
71def test_search_empty_query(client_nc, query, caplog) -> None:
72 resp = client_nc.get(f"/search/movies{query}")
73 resp_data = resp.json()
75 assert resp.status_code == 200
76 assert get_json_val(resp_data, "$.query") == ""
77 assert get_json_val(resp_data, "$.heading") == ""
78 assert MOVIE_ENG in caplog.text
79 assert len(get_json_val(resp_data, "$.results.movies")) == 0
82def test_search_without_results_page_1(client_nc, caplog) -> None:
83 query = '".*&^'
84 resp = client_nc.get(f"/search/movies?q={query}")
85 resp_data = resp.json()
87 assert resp.status_code == 200
88 assert get_json_val(resp_data, "$.query") == '".*'
89 assert get_json_val(resp_data, "$.heading").startswith('".*')
90 assert MOVIE_JPN in get_json_val(resp_data, "$.heading")
91 assert MOVIE_ENG in caplog.text
92 assert len(get_json_val(resp_data, "$.results.movies")) == 0
95def test_search_without_results_page_2(client_nc, caplog) -> None:
96 query = "ちはやふる" # page 1 returns results, refer to 'test_search_with_results_multiple'
97 resp = client_nc.get(f"/search/movies?q={query}&page=2")
98 resp_data = resp.json()
100 assert resp.status_code == 200
101 assert get_json_val(resp_data, "$.query") == query
102 assert get_json_val(resp_data, "$.heading").startswith(query)
103 assert MOVIE_JPN in get_json_val(resp_data, "$.heading")
104 assert MOVIE_ENG in caplog.text
105 assert len(get_json_val(resp_data, "$.results.movies")) == 0
108@pytest.mark.parametrize(
109 "test_data",
110 [
111 {
112 "title": "プラダを着た悪魔",
113 "rating": 4.1,
114 "mark_count": 378397,
115 "clip_count": 89715,
116 "movie_id": 2151,
117 "link": "https://filmarks.com/movies/2151",
118 "screening_date": "2006年11月18日",
119 "screening_time": "110分",
120 "country_of_origin": ["アメリカ"],
121 "genre": ["ドラマ", "恋愛", "コメディ"],
122 "distributor": ["20世紀フォックス映画"],
123 "director": ["デヴィッド・フランケル"],
124 "scriptwriter": ["アライン・ブロッシュ・マッケンナ"],
125 "cast": ["メリル・ストリープ", "アン・ハサウェイ"],
126 },
127 ],
128)
129def test_search_with_results_single(client_nc, test_data, caplog) -> None:
130 query = "プラダを着た悪魔"
131 resp = client_nc.get(f"/search/movies?q={query}&limit=1")
132 resp_data = resp.json()
133 movies = get_json_val(resp_data, "$.results.movies")
135 assert resp.status_code == 200
136 assert get_json_val(resp_data, "$.query") == query
137 assert get_json_val(resp_data, "$.heading").startswith(query)
138 assert MOVIE_JPN in get_json_val(resp_data, "$.heading")
139 assert MOVIE_ENG in caplog.text
140 assert len(movies) == 1
142 fields = [
143 "title",
144 "movie_id",
145 "link",
146 "screening_date",
147 "screening_time",
148 "country_of_origin",
149 "genre",
150 "distributor",
151 "director",
152 "scriptwriter",
153 "cast",
154 ]
155 for field in fields:
156 assert get_json_val(movies[0], f"$.{field}") == get_json_val(test_data, f"$.{field}")
158 assert get_json_val(movies[0], "$.rating") == pytest.approx(get_json_val(test_data, "$.rating"), abs=0.5)
159 assert get_json_val(movies[0], "$.mark_count") >= get_json_val(test_data, "$.mark_count")
160 assert get_json_val(movies[0], "$.clip_count") >= get_json_val(test_data, "$.clip_count")
162 assert get_json_val(movies[0], "$.poster") is not None
165@pytest.mark.parametrize(
166 "test_data",
167 [
168 {
169 "title": "ちはやふる 上の句",
170 "movie_id": 62655,
171 "link": "https://filmarks.com/movies/62655",
172 "screening_date": "2016年03月19日",
173 "screening_time": "111分",
174 "country_of_origin": ["日本"],
175 "director": ["小泉徳宏"],
176 "cast": ["広瀬すず", "野村周平"],
177 },
178 {
179 "title": "ちはやふる 下の句",
180 "movie_id": 62920,
181 "link": "https://filmarks.com/movies/62920",
182 "screening_date": "2016年04月29日",
183 "screening_time": "103分",
184 "country_of_origin": ["日本"],
185 "director": ["小泉徳宏"],
186 "cast": ["広瀬すず", "野村周平", "新田真剣佑"],
187 },
188 {
189 "title": "ちはやふる ー結びー",
190 "movie_id": 73729,
191 "link": "https://filmarks.com/movies/73729",
192 "screening_date": "2018年03月17日",
193 "screening_time": "128分",
194 "country_of_origin": ["日本"],
195 "director": ["小泉徳宏"],
196 "cast": ["広瀬すず", "野村周平"],
197 },
198 ],
199)
200def test_search_with_results_multiple(client_nc, test_data, caplog) -> None:
201 query = "ちはやふる"
202 resp = client_nc.get(f"/search/movies?q={query}&page=1&limit=3")
203 resp_data = resp.json()
204 movie = next(m for m in get_json_val(resp_data, "$.results.movies") if get_json_val(m, "$.title") == get_json_val(test_data, "$.title"))
206 assert resp.status_code == 200
207 assert get_json_val(resp_data, "$.query") == query
208 assert get_json_val(resp_data, "$.heading").startswith(query)
209 assert MOVIE_JPN in get_json_val(resp_data, "$.heading")
210 assert MOVIE_ENG in caplog.text
212 fields = [
213 "title",
214 "movie_id",
215 "link",
216 "screening_date",
217 "screening_time",
218 "country_of_origin",
219 "director",
220 "cast",
221 ]
222 for field in fields:
223 assert get_json_val(movie, f"$.{field}") == get_json_val(test_data, f"$.{field}")
225 assert get_json_val(movie, "$.rating") is not None
226 assert get_json_val(movie, "$.mark_count") is not None
227 assert get_json_val(movie, "$.clip_count") is not None
228 assert get_json_val(movie, "$.poster") is not None
229 assert get_json_val(movie, "$.genre") is not None
230 assert get_json_val(movie, "$.distributor") is not None
233def test_search_with_results_random(client_nc, caplog) -> None:
234 with open(file="tests/movie/100_movies.json", mode="r", encoding="utf-8") as f:
235 test_data = json.load(f)
236 movie = choice(test_data)
238 query = get_json_val(movie, "$.title")
239 movie_id = get_json_val(movie, "$.id")
241 resp = client_nc.get(f"/search/movies?q={query}&limit=1")
242 resp_data = resp.json()
244 assert resp.status_code == 200
245 assert get_json_val(resp_data, "$.query") == query
246 assert get_json_val(resp_data, "$.heading").startswith(query)
247 assert MOVIE_JPN in get_json_val(resp_data, "$.heading")
248 assert MOVIE_ENG in caplog.text
250 assert get_json_val(resp_data, "$.results.movies[0].title") == query
251 assert get_json_val(resp_data, "$.results.movies[0].rating") is not None
252 assert get_json_val(resp_data, "$.results.movies[0].mark_count") is not None
253 assert get_json_val(resp_data, "$.results.movies[0].clip_count") is not None
254 assert get_json_val(resp_data, "$.results.movies[0].movie_id") == movie_id
255 assert get_json_val(resp_data, "$.results.movies[0].link") is not None