From 3f63fa914a613816912abb9b681bf21e7d95fda1 Mon Sep 17 00:00:00 2001 From: QasimK Date: Sat, 28 May 2016 16:13:59 +0100 Subject: [PATCH] Add test for unknown number of episodes --- MANIFEST.in | 2 + setup.cfg | 3 + src/mal_scraper/anime.py | 14 +- ...s6aHR0cDovL215YW5pbWVsaXN0Lm5ldC9hbmltZS8x | 0 tests/mal_scraper/conftest.py | 71 +- .../manual_responses/garbled_anime_page | 1 + .../manual_responses/unknown_episodes | 1163 +++++++++++++++++ tests/mal_scraper/test_anime.py | 22 +- 8 files changed, 1239 insertions(+), 37 deletions(-) rename tests/mal_scraper/{responses => auto_responses}/Z2V0Ois6aHR0cDovL215YW5pbWVsaXN0Lm5ldC9hbmltZS8x (100%) create mode 100644 tests/mal_scraper/manual_responses/garbled_anime_page create mode 100644 tests/mal_scraper/manual_responses/unknown_episodes diff --git a/MANIFEST.in b/MANIFEST.in index 92351c4..e412ee8 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,6 +4,8 @@ graft src graft ci graft tests +resursive-include tests/mal_scraper * + include .bumpversion.cfg include .coveragerc include .cookiecutterrc diff --git a/setup.cfg b/setup.cfg index 27d7dc8..c48a345 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,6 +6,9 @@ max-line-length = 100 max-complexity = 5 [pytest] +testpaths = + tests + docs norecursedirs = .git .tox diff --git a/src/mal_scraper/anime.py b/src/mal_scraper/anime.py index d4708d4..d89fc43 100644 --- a/src/mal_scraper/anime.py +++ b/src/mal_scraper/anime.py @@ -15,15 +15,17 @@ logger = logging.getLogger(__name__) # pass -def retrieve_anime(id_ref, requester=request_passthrough): +def retrieve_anime(id_ref=1, requester=request_passthrough): """Return the metadata for a particular show. TODO. - id_ref: identifier that can be used in iterations - requester: funnel requests through a request maker, this allows - us to limit requests globally + Args: + id_ref (Optional(int)): Internal show identifier + requester (Optional(requests-like)): HTTP request maker + This allows us to control/limit/mock requests. - Return a dictionary. - See tests/mal_scraper/test_anime.py::test_download_first for the keys. + Return: + A dictionary. + See tests/mal_scraper/test_anime.py::test_download_first for the keys. """ url = get_url_from_id_ref(id_ref) response = requester.get(url) diff --git a/tests/mal_scraper/responses/Z2V0Ois6aHR0cDovL215YW5pbWVsaXN0Lm5ldC9hbmltZS8x b/tests/mal_scraper/auto_responses/Z2V0Ois6aHR0cDovL215YW5pbWVsaXN0Lm5ldC9hbmltZS8x similarity index 100% rename from tests/mal_scraper/responses/Z2V0Ois6aHR0cDovL215YW5pbWVsaXN0Lm5ldC9hbmltZS8x rename to tests/mal_scraper/auto_responses/Z2V0Ois6aHR0cDovL215YW5pbWVsaXN0Lm5ldC9hbmltZS8x diff --git a/tests/mal_scraper/conftest.py b/tests/mal_scraper/conftest.py index 3166040..629ecdf 100644 --- a/tests/mal_scraper/conftest.py +++ b/tests/mal_scraper/conftest.py @@ -9,48 +9,63 @@ import responses class ResponsesWrapper: """Mock ALL requests more easily using saved files when use_live. - You can pass fake in instead which will ALWAYS use the fake response. + You can ALWAYS mock a request if there is no safe live page to use. """ - RESPONSES_DIR = os.path.join(os.path.dirname(__file__), 'responses') + TODO_DIR = os.path.join(os.path.dirname(__file__), '0_todo_responses') + AUTO_DIR = os.path.join(os.path.dirname(__file__), 'auto_responses') + MANUAL_DIR = os.path.join(os.path.dirname(__file__), 'manual_responses') def __init__(self, rsps, use_live): self.rsps = rsps self.use_live = use_live # Use live unless we are faking - def add(self, url, method='get', fake=None): - assert method == 'get', 'TODO: Implement other methods.' - filename = self._encode(method, url) - filepath = os.path.join(self.RESPONSES_DIR, filename) + def optional_mock(self, url): + """Add an automatic mock for a URL, this can be disabled if desired.""" + filename = self._encode('get', url) + filepath = os.path.join(self.AUTO_DIR, filename) - pass_method = { - 'get': responses.GET - }[method] - - if fake: - # Always mock the response whether it is live or not - self.rsps.add(pass_method, url, body=fake) - elif os.path.isfile(filepath): # pragma: no cover - # Only mock the response if we are not using live - # Otherwise allow a live request to be made + if os.path.isfile(filepath): # pragma: no cover + # Mock the response unless we are using live if not self.use_live: with open(filepath, 'rb') as fin: body = fin.read() - self.rsps.add(pass_method, url, body=body) else: - # To avoid an error let's make the request and add it in as mocked + # Make the request manually, avoiding requests which is mocked. with urllib.request.urlopen(url) as nin: - self.rsps.add(pass_method, url, body=nin.read()) + body = nin.read() + + self.rsps.add(responses.GET, url, body=body) else: # pragma: no cover # Produce a file to mock this request (needs to be checked manually) - todo_filename = 'TODO_' + filename - todo_filepath = os.path.join(self.RESPONSES_DIR, todo_filename) - with urllib.request.urlopen(url) as nin,\ - open(todo_filepath, 'wb') as fout: - fout.write(nin.read()) - print('Response Mocked:', method, url) - print('File in responses tests directory: ', todo_filename) - print('Look at it and rename it by removing "TODO_".') - assert False, 'Look at stdout because a response was mocked for you.' + self._save_url(url, filename) + + def always_mock(self, url, filename): + """Always mock this response, never test against the live url. + + Args: + url (str): The URL to mock. + filename (str): The file inside self.RESPONSES_DIR to use. + """ + filepath = os.path.join(self.MANUAL_DIR, filename) + if os.path.isfile(filepath): + with open(filepath, 'rb') as fin: + self.rsps.add(responses.GET, url, body=fin.read()) + else: # pragma: no cover + # Produce a file to mock this request (needs to be checked manually) + self._save_url(url, filename) + + def _save_url(self, url, filename): + os.makedirs(self.TODO_DIR, exist_ok=True) + todo_filepath = os.path.join(self.TODO_DIR, filename) + with urllib.request.urlopen(url) as nin,\ + open(todo_filepath, 'wb') as fout: + fout.write(nin.read()) + + print('Response Mocked:', url) + print('File in TODO responses directory:', filename) + print('If you want to edit the file, move it into manual responses and use always_mock.') + print('Otherwise, move it into the auto responses folder.') + assert False, 'Look at stdout because a response was mocked for you.' @staticmethod def _encode(method, url): diff --git a/tests/mal_scraper/manual_responses/garbled_anime_page b/tests/mal_scraper/manual_responses/garbled_anime_page new file mode 100644 index 0000000..a7f8d9e --- /dev/null +++ b/tests/mal_scraper/manual_responses/garbled_anime_page @@ -0,0 +1 @@ +bla diff --git a/tests/mal_scraper/manual_responses/unknown_episodes b/tests/mal_scraper/manual_responses/unknown_episodes new file mode 100644 index 0000000..dbfc293 --- /dev/null +++ b/tests/mal_scraper/manual_responses/unknown_episodes @@ -0,0 +1,1163 @@ + + + + + + + + + +Sousei no Onmyouji (Twin Star Exorcists) - MyAnimeList.net + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
+
+ +
+
+ + + +
+ +
+
+ +
+ Edit Anime Information +
+
+ +
What would you like to edit?
+ +   +
+
+

Sousei no Onmyouji

+ + + + + +
+ +
+ + Sousei no Onmyouji + +
+ + + + + + + + + +

+ +

Alternative Titles

+ English: Twin Star Exorcists +
+ Synonyms: Sousei no Onmyouji +
+ Japanese: 双星の陰陽師 +

+ +

Information

+ +
+ Type: TV +
+ +
+ Episodes: + Unknown +
+ +
+ Status: + Currently Airing +
+ +
+ Aired: + Apr 6, 2016 to ? +
+ +
+ Premiered: + Spring 2016 +
+ +
+ Broadcast: + Wednesdays at 18:25 (JST) +
+ + +
+ Licensors: + None found, add some
+
+ Studios: + Studio Pierrot
+ +
+ Source: + Manga +
+ + + +
+ Duration: + 24 min. +
+ +
+ Rating: + PG-13 - Teens 13 or older +
+ +
+ +

Statistics

+
+ Score: + 7.091 (scored by 14,650 users) + + +
+ 1 + indicates a weighted score. Please note that 'Not yet aired' titles are excluded. + +
+
+
+ Ranked: + #30112 +
+ 2 + based on the top anime page. Please note that 'Not yet aired' and 'R18+' titles are excluded. + +
+
+
+ Popularity: + #672 +
+
+ Members: + 73,379 +
+
+ Favorites: + 250 +
+ + +
+ + +
+
+ +
+
+
+ +
+ + + + + + + + + + + + +
+ 7.09 +
Ranked #3011Popularity #672Members 73,379
Add to List
+ Episodes: /?
+

Synopsis

Grotesque monsters known as Impurities reside in Magano, a realm parallel to the present day. Exorcists purify these evil apparitions to protect the people. Rokuro Enmado is opposed to becoming an exorcist, even though he has the talent to be one, but an oracle gives him the title of "Twin Star Exorcist," the name bestowed upon married exorcists. Benio Adashino, the girl who receives the title along with Rokuro, is determined to exorcise all the Kegare, or Impurities, in the world. The two find themselves wrapped up in more and more battles together as the Twin Star Exorcists...
+
+(Source: Crunchyroll)

Background

No background information has been added to this title. Help improve our database by adding background information here.

Episode Videos

+ +
+
+ +
+
+
+ +
+
+ +
+
+

Related Anime


Characters & Voice Actors

+ + + + + +
+
Adashino, Benio
+
+ Adashino, Benio +
Main
+
+ + + + + +
+ Han, Megumi
+ Japanese +
Han, Megumi
+
+
+ + + + + +
+
Enmadou, Rokuro
+
+ Enmadou, Rokuro +
Main
+
+ + + + + +
+ Hanae, Natsuki
+ Japanese +
Hanae, Natsuki
+
+
+ + + + + +
+
Ikaruga, Shimon
+
+ Ikaruga, Shimon +
Supporting
+
+ + + + + +
+ Ishikawa, Kaito
+ Japanese +
Ishikawa, Kaito
+
+
+ + + + + +
+
Tsuchimikado, Arima
+
+ Tsuchimikado, Arima +
Supporting
+
+ + + + + +
+ Namikawa, Daisuke
+ Japanese +
Namikawa, Daisuke
+
+

Staff

+ + + + + + + + + + + + + + + + + + + +
Taguchi, Tomohisa
Taguchi, Tomohisa +
Director, Storyboard
Takakuwa, Hajime
Takakuwa, Hajime +
Sound Director
Arakawa, Naruhisa
Arakawa, Naruhisa +
Script, Series Composition
Kamiya, Jun
Kamiya, Jun +
Storyboard


+
+

+ Edit + Opening Theme +

+
"Valkyrie -Ikusa Otome- (Valkyrie -戦乙女-)" by Wagakki Band
+
+
+ +
+

+ Edit + Ending Theme +

+
#1: "Valkyrie -Ikusa Otome- (Valkyrie-戦乙女-)" by Wagakki Band (ep 1)
#2: "Eyes (アイズ)" by Hitomi Kaji (eps 2-)
+
+
+

+ +
+
+ +
+
+
+ +
+
+ +
+
+

More reviewsReviews

+
+ +
+ + + + + + +
+
+
+
+
+
+
+ + astray71
+ + +
Apr 28, 2016
+ + +
+
+
+ + So far, it's your average shounen anime with the main characters entrusted to save the world.
+
+The story isn't anything special as it's been done quite a number of times over before. There are exorcists and Kegare (the demons they fight). The two on the cover are the "Twin Stars" which are the key pieces to beating all of the Kegare.
+
+The artwork is probably the only thing good about the anime as it is pretty good. Everything is quite detailed even in messy fights and flashbacks with a lot going on background.
+
+The animation is a completely different story. The fights are poorly animated, not read more
+
+
+ I found this review Helpful
+
+ +
+ +
+ + + + + + +
+
+
+
+
+
+
+ + thelectricow
+ + +
May 12, 2016
+ + +
+
+
+ + Sousei no Onmyouji was really high in the expectations in my book, especially since the author of Naruto himself recommended reading the manga. Well, I guess Naruto fans might enjoy it, but I sure as heck DIDN'T.
+__________________________________________________________________________
+STORY: 1/10
+
+Behold, the insanely unique story about heroes beating monsters! Yeah. That's it. Oh, I guess there's a parallel world too, because that makes it an absolute must watch!
+
+sarcasm
+/ˈsärˌkazəm/
+noun
+the use of irony to mock or convey contempt.
+
+In all seriousness, the plot of Sousei no Onmyouji is completely ripped through and through, and I honestly have nothing good to say about it, so I'll make this short: If you have read more
+
+
+ I found this review Helpful
+
+ +
+ +
+ + + + + + +
+
+
+
+
+
+
+ + Luxanity
+ + +
May 4, 2016
+ + +
+
+
+ + Sousei no Onmyouji is Spring 2016 season's average shounen anime with a theme that has already been used many times. It might seem at least a bit nice at first but, it quickly becomes a somewhat repetitive and uninteresting ride.
+
+Story: 4/10
+The story of Sousei no Onmyouji, isn't something new or very original, as we have already seen lots of anime that center on certain people who possess a special ability (Exorcists) and fight against supernatural enemies (Kegare) in order to protect the world. The problem is not that it uses an overused theme, but that it doesn't manage it in a way that will make read more
+
+
+ I found this review Helpful
+
+ +
+ +
+ + + + + + +
+
+
+
+
+
+
+ + 3xTripple
+ + +
May 8, 2016
+ + +
+
+
+ + Do you like Shounen?
+Do you like Exorcists?
+Do you like shows with pretty good art but the plot and characters are not that good?
+
+If you answered yes to the first two and aren't sure about the last one, then this show is definitely a show you might want to consider giving a chance. But just know that so far it's not too special in any way form or fashion.
+
+This review is going to be relatively short, but straight to the point from my perspective as a seasoned anime watcher.
+_____________________________________________________________
+Story:(6/10)
+Overpowered "chosen one" kids with sad background stories fight to save the world as read more
+
+
+ I found this review Helpful
+
+
+ +
+
+ +
+
+

+ + Recommendations +


Recent News

TV Anime 'Sousei no Onmyouji' Announces More Cast Members

TV Anime 'Sousei no Onmyouji' Announces More Cast Members

+

The official website of the Spring 2016 anime Sousei no Onmyouji announced on Monday additional voice cast members for the series. Studio Pierrot's adaptation o...read more

+
+
Spring 2016 Simulcast List [Update 5/25]

Spring 2016 Simulcast List [Update 5/25]

+

In this thread, you'll find a comprehensive list of anime acquired for simulcast release during the Spring 2016 season. Anime series licensed for home video rel...read more

+
+

Recent Forum Discussion

+ + + + + + + + + + + + +
Poll: Sousei no Onmyouji Episode 8 Discussion ( 1 2 )
Stark700 - May 25
72 repliesby salmon-kun »»
8 hours ago
Poll: Sousei no Onmyouji Episode 7 Discussion ( 1 2 )
Stark700 - May 18
69 repliesby SakurasouBusters »»
May 26, 9:04 PM
+
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+
+ + + + + + +
+ + + +
+ + +
+ +
+
+ + + + + + + + + + + diff --git a/tests/mal_scraper/test_anime.py b/tests/mal_scraper/test_anime.py index afc27cc..c91e1e2 100644 --- a/tests/mal_scraper/test_anime.py +++ b/tests/mal_scraper/test_anime.py @@ -7,7 +7,7 @@ import mal_scraper def test_download_first(mock_requests): """Can we retrieve the first show ever?""" - mock_requests.add('http://myanimelist.net/anime/1') + mock_requests.optional_mock('http://myanimelist.net/anime/1') info = mal_scraper.retrieve_anime(1) # Fuzzy match datetime @@ -15,7 +15,7 @@ def test_download_first(mock_requests): assert datetime.utcnow() - retrieval < timedelta(seconds=30) assert info == { - 'scraper_retrieved_at': retrieval, + 'scraper_retrieved_at': retrieval, # UTC Datetime 'id_ref': 1, 'name': 'Cowboy Bebop', 'name_english': 'Cowboy Bebop', @@ -36,5 +36,21 @@ def test_download_first(mock_requests): def test_download_first_fail(mock_requests): """Do we get None if the page was bad??""" - mock_requests.add('http://myanimelist.net/anime/1', fake='bla') + mock_requests.always_mock( + 'http://myanimelist.net/anime/1', + 'garbled_anime_page', + ) assert mal_scraper.retrieve_anime(1) is None + + +class TestParsing: + """Test parsing of particular parts of the page""" + + def test_unknown_episodes(self, mock_requests): + """Do we return None for an unknown number of episodes?""" + mock_requests.always_mock( + 'http://myanimelist.net/anime/32105', + 'unknown_episodes', + ) + + assert mal_scraper.retrieve_anime(32105)['episodes'] is None