From 9f11b2e538ced11479b7a66d5d524982ccc3bdb1 Mon Sep 17 00:00:00 2001 From: rkwyu Date: Thu, 25 Apr 2024 18:17:17 +0100 Subject: [PATCH] Add unit test for agdm.tv --- anime_dl/scrapper/agdm_tv_scrapper.py | 16 ++--- tests/scrapper/test_agdm_tv.py | 88 +++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 tests/scrapper/test_agdm_tv.py diff --git a/anime_dl/scrapper/agdm_tv_scrapper.py b/anime_dl/scrapper/agdm_tv_scrapper.py index c4ebb40..2390f83 100644 --- a/anime_dl/scrapper/agdm_tv_scrapper.py +++ b/anime_dl/scrapper/agdm_tv_scrapper.py @@ -23,7 +23,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]: for episode in episodes: e, x_servers = self.parse_episode(episode.referer_url) # check if video source available - if requests.get(e.video_url, headers=general.REQUEST["header"]).ok: + if self.test_connectivity(e.video_url): episode = ( episode.set_series_name(e.series_name) .set_season(e.season) @@ -44,9 +44,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]: ) logger.info(f"try another server: {x_url}") e, _ = self.parse_episode(x_url) - if requests.get( - e.video_url, headers=general.REQUEST["header"] - ).ok: + if self.test_connectivity(e.video_url): episode = ( episode.set_series_name(e.series_name) .set_season(e.season) @@ -65,7 +63,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]: episode_no = m.groups()[2] episode, x_servers = self.parse_episode(url) # check if video source available - if requests.get(episode.video_url, headers=general.REQUEST["header"]).ok: + if self.test_connectivity(episode.video_url): return [episode] # try other servers else: @@ -74,9 +72,7 @@ def get_episodes(self, url: str) -> typing.List[Episode]: x_url = f"https://agdm.tv/play/{id}-{x_server}-{episode_no}.html" logger.info(f"try another server: {x_url}") episode, _ = self.parse_episode(x_url) - if requests.get( - episode.video_url, headers=general.REQUEST["header"] - ).ok: + if self.test_connectivity(episode.video_url): return [episode] else: logger.warning(f"video source fail: {x_url} ({episode.video_url})") @@ -151,3 +147,7 @@ def parse_episode(self, url: str) -> tuple[Episode, typing.List[int]]: except Exception as e: logger.error(f"{url}: {e}") return Episode() + + def test_connectivity(url): + headers = general.REQUEST["header"] + return requests.get(url, headers=headers).ok diff --git a/tests/scrapper/test_agdm_tv.py b/tests/scrapper/test_agdm_tv.py new file mode 100644 index 0000000..05f02ab --- /dev/null +++ b/tests/scrapper/test_agdm_tv.py @@ -0,0 +1,88 @@ +import pytest + +from unittest.mock import Mock, patch +from anime_dl.object.episode import Episode +from anime_dl.scrapper.agdm_tv_creator import AgdmTvCreator +from anime_dl.scrapper.agdm_tv_scrapper import AgdmTvScrapper + + +@pytest.fixture() +def url(): + return { + "episode": "https://agdm.tv/play/35480-1-1.html", + "series": "https://agdm.tv/vod/35480.html", + } + + +def test_creator(): + creator = AgdmTvCreator() + scrapper = creator.factory_method() + assert isinstance(scrapper, AgdmTvScrapper) + + +@pytest.fixture() +def scrapper(): + return AgdmTvScrapper() + + +@pytest.mark.scrapping +def test_get_episodes_empty(scrapper): + with pytest.raises(Exception) as e_info: + scrapper.get_episodes("") + + +@pytest.mark.scrapping +def test_get_episodes_episode(scrapper, url): + url = url["episode"] + # given + scrapper.parse_episode = Mock( + return_value=( + ( + Episode() + .set_series_name("SN") + .set_season("na") + .set_episode_name("EN") + .set_episode_no("EN") + .set_video_url("VU") + .set_referer_url(url) + .set_image_src("na") + ), [], + ) + ) + scrapper.test_connectivity = Mock(return_value=True) + # test + res = scrapper.get_episodes(url) + assert len(res) == 1 and res[0].series_name == "SN" and res[0].video_url == "VU" + + +@pytest.mark.scrapping +def test_get_episodes_series(scrapper, url): + url = url["series"] + # given + scrapper.parse_episode = Mock( + return_value=( + ( + Episode() + .set_series_name("SN") + .set_season("na") + .set_episode_name("EN") + .set_episode_no("EN") + .set_video_url("VU") + .set_referer_url(url) + .set_image_src("na") + ), [], + ) + ) + scrapper.parse_series = Mock( + return_value=[ + ( + Episode() + .set_referer_url(url) + .set_image_src("IS") + ) + ] + ) + scrapper.test_connectivity = Mock(return_value=True) + # test + res = scrapper.get_episodes(url) + assert len(res) == 1 and res[0].video_url == "VU"