Coverage for tests\anime\test_anime_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 "/animes",
10 "/animes/1",
11 "/animes//1",
12 "/animes/1//reviews"
13 "/animes//1/reviews"
14 "/list-anime",
15 "/list-anime/vod",
16 "/list-anime/year",
17 "/list-anime/company",
18 "/list-anime/tag",
19 "/list-anime/person",
20])
21def test_invalid_endpoint_base(client_nc, path) -> None:
22 resp = client_nc.get(path)
23 resp_data = resp.json()
25 assert resp.status_code == 404
26 assert get_json_val(resp_data, "$.detail") == "Not Found"
29@pytest.mark.parametrize("path", [
30 "/animes/9999999999/9999999999",
31 "/animes/9999999999/9999999999/reviews",
32 "/animes/9999999999/9999999999/reviews/9999999999",
33 "/list-anime/vod/invalid_vod",
34 "/list-anime/year/999s",
35 "/list-anime/year/999",
36 "/list-anime/year/2019/9",
37 "/list-anime/company/9999999999",
38 "/list-anime/tag/invalid_tag",
39 "/list-anime/person/9999999999",
40])
41def test_invalid_endpoint_filmarks(client_nc, path, caplog) -> None:
42 resp = client_nc.get(path)
43 resp_data = resp.json()
45 assert resp.status_code == 404
46 assert get_json_val(resp_data, "$.detail") == "The requested resource could not be found."
47 assert "Invalid Filmarks page requested" in caplog.text
50@pytest.mark.parametrize("test_data", [
51 (
52 "/search/animes?q=test500",
53 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
54 "Failed to search animes."
55 ),
56 (
57 "/animes/500/500",
58 "src.scrape.info.info_anime_scraper.InfoAnimeScraper.scrape",
59 "Failed to retrieve information for anime with series ID: 500 and season ID: 500."
60 ),
61 (
62 "/animes/500/500/reviews",
63 "src.scrape.info.info_anime_scraper.InfoAnimeScraper.scrape",
64 "Failed to retrieve reviews for anime with series ID: 500 and season ID: 500."
65 ),
66 (
67 "/animes/500/500/reviews/500",
68 "src.scrape.info.info_anime_scraper.InfoAnimeScraper.scrape",
69 "Failed to retrieve review for anime with series ID: 500, season ID: 500, and review ID: 500."
70 ),
71 (
72 "/list-anime/trend",
73 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
74 "Failed to fetch trending animes."
75 ),
76 (
77 "/list-anime/vod/500_vod",
78 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
79 "Failed to fetch animes from VOD service: 500_vod."
80 ),
81 (
82 "/list-anime/year/500s",
83 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
84 "Failed to fetch animes from year series: 500s."
85 ),
86 (
87 "/list-anime/year/500",
88 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
89 "Failed to fetch animes from year: 500."
90 ),
91 (
92 "/list-anime/year/500/1",
93 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
94 "Failed to fetch animes from year: 500 with season ID: 1."
95 ),
96 (
97 "/list-anime/company/500",
98 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
99 "Failed to fetch animes with company ID: 500."
100 ),
101 (
102 "/list-anime/tag/500_tag",
103 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
104 "Failed to fetch animes with tag: 500_tag."
105 ),
106 (
107 "/list-anime/person/500",
108 "src.scrape.search.search_anime_scraper.SearchAnimeScraper.scrape",
109 "Failed to fetch animes with person ID: 500."
110 ),
111])
112def test_scrape_error_500_server_error(client_nc, mocker, test_data, caplog) -> None:
113 mocker.patch(
114 target=test_data[1],
115 side_effect=Exception("Testing - 500 Internal Server Error"),
116 )
118 resp = client_nc.get(test_data[0])
119 resp_data = resp.json()
121 assert resp.status_code == 500
122 assert get_json_val(resp_data, "$.detail") == "The server encountered an unexpected error."
123 assert "Testing - 500 Internal Server Error" in caplog.text
124 assert test_data[2] in caplog.text
127@pytest.mark.parametrize("path", [
128 "/search/animes?q=test503",
129 "/animes/503/503",
130 "/animes/503/503/reviews",
131 "/animes/503/503/reviews/503",
132 "/list-anime/trend",
133 "/list-anime/vod/503_vod",
134 "/list-anime/year/503s",
135 "/list-anime/year/503",
136 "/list-anime/year/503/1",
137 "/list-anime/company/503",
138 "/list-anime/tag/503_tag",
139 "/list-anime/person/503",
140])
141def test_scrape_error_503_service_unavailable_session(client_nc, mocker, path, caplog) -> None:
142 mocker.patch(
143 target="src.scrape.base_scraper.Session",
144 side_effect=RequestException("Testing - 503 Service Unavailable"),
145 )
147 resp = client_nc.get(path)
148 resp_data = resp.json()
150 assert resp.status_code == 503
151 assert get_json_val(resp_data, "$.detail") == "The service is currently unavailable."
152 assert "Request to Filmarks failed: 'Testing - 503 Service Unavailable'" in caplog.text
155@pytest.mark.parametrize("path", [
156 "/animes/2592/3304/reviews?page=9999999999999999999",
157 "/list-anime/trend?page=999999999999999999",
158 "/list-anime/vod/prime_video?page=999999999999999999",
159 "/list-anime/year/2020s?page=999999999999999999",
160 "/list-anime/year/2025?page=999999999999999999",
161 "/list-anime/year/2019/1?page=999999999999999999",
162 "/list-anime/year/2019/99",
163 "/list-anime/company/41?page=999999999999999999",
164 "/list-anime/tag/駄作?page=999999999999999999",
165 "/list-anime/person/274563?page=999999999999999999",
166])
167def test_scrape_error_503_service_unavailable_filmarks(client_nc, path, caplog) -> None:
168 class CustomParams():
169 page: int = Field(1, gt=0)
170 client_nc.app.dependency_overrides[SearchParams] = CustomParams
171 client_nc.app.dependency_overrides[ReviewParams] = CustomParams
172 client_nc.app.dependency_overrides[ListParams] = CustomParams
174 resp = client_nc.get(path)
175 resp_data = resp.json()
177 assert resp.status_code == 503
178 assert get_json_val(resp_data, "$.detail") == "The service is currently unavailable."
179 assert "Filmarks is temporarily unavailable" in caplog.text
181 del client_nc.app.dependency_overrides[SearchParams]
182 del client_nc.app.dependency_overrides[ReviewParams]
183 del client_nc.app.dependency_overrides[ListParams]