From 80e105abb96b16142662a1ced3566cdb9ce88211 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sun, 18 Dec 2016 19:46:46 +0100 Subject: [PATCH] Implement fetch method in fetcher base class --- fimfarchive/fetchers.py | 23 +++++++++++-- tests/test_fetchers.py | 71 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/fimfarchive/fetchers.py b/fimfarchive/fetchers.py index 823cee8..3fb0de7 100644 --- a/fimfarchive/fetchers.py +++ b/fimfarchive/fetchers.py @@ -32,6 +32,7 @@ from zipfile import ZipFile, BadZipFile import requests from fimfarchive.exceptions import InvalidStoryError, StorySourceError +from fimfarchive.stories import Story StreamReader = codecs.getreader('utf-8') @@ -62,12 +63,14 @@ class Fetcher: """ pass - def fetch(self, key): + def fetch(self, key, prefetch_meta=None, prefetch_data=None): """ Fetches story information. Args: key: Primary key of the story. + prefetch_meta: Force prefetching of meta. + prefetch_data: Force prefetching of data. Returns: Story: A new `Story` object. @@ -76,7 +79,23 @@ class Fetcher: InvalidStoryError: If a valid story is not found. StorySourceError: If source does not return any data. """ - raise NotImplementedError() + if prefetch_meta is None: + prefetch_meta = self.prefetch_meta + + if prefetch_meta: + meta = self.fetch_meta(key) + else: + meta = None + + if prefetch_data is None: + prefetch_data = self.prefetch_data + + if prefetch_data: + data = self.fetch_data(key) + else: + data = None + + return Story(key, self, meta, data) def fetch_data(self, key): """ diff --git a/tests/test_fetchers.py b/tests/test_fetchers.py index 1d3dc15..c9b5e37 100644 --- a/tests/test_fetchers.py +++ b/tests/test_fetchers.py @@ -22,6 +22,8 @@ Fetcher tests. # +from unittest.mock import MagicMock + import pytest from fimfarchive.exceptions import InvalidStoryError, StorySourceError @@ -36,6 +38,75 @@ PROTECTED_STORY_KEY = 208799 FIMFARCHIVE_PATH = 'fimfarchive-20160525.zip' +class TestFetcher: + """ + Fetcher tests. + """ + + def test_fetch_with_prefetch_meta(self, fetcher): + """ + Tests `fetch_meta` is called when prefetch_meta is enabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_meta=True) + fetcher.fetch_meta.assert_called_once_with(VALID_STORY_KEY) + + def test_fetch_without_prefetch_meta(self, fetcher): + """ + Tests `fetch_meta` is not called when prefetch_meta is disabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_meta=False) + fetcher.fetch_meta.assert_not_called() + + def test_fetch_with_prefetch_data(self, fetcher): + """ + Tests `fetch_data` is called when prefetch_data is enabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_data=True) + fetcher.fetch_data.assert_called_once_with(VALID_STORY_KEY) + + def test_fetch_without_prefetch_data(self, fetcher): + """ + Tests `fetch_data` is not called when prefetch_data is disabled. + """ + fetcher.fetch(VALID_STORY_KEY, prefetch_data=False) + fetcher.fetch_data.assert_not_called() + + def test_fetch_with_default_prefetch(self, fetcher): + """ + Tests prefetching can be enabled using attributes. + """ + fetcher.prefetch_meta = True + fetcher.prefetch_data = True + + fetcher.fetch(VALID_STORY_KEY) + + fetcher.fetch_meta.assert_called_once_with(VALID_STORY_KEY) + fetcher.fetch_data.assert_called_once_with(VALID_STORY_KEY) + + def test_fetch_without_default_prefetch(self, fetcher): + """ + Tests prefetching can be disabled using attributes. + """ + fetcher.prefetch_meta = False + fetcher.prefetch_data = False + + fetcher.fetch(VALID_STORY_KEY) + + fetcher.fetch_meta.assert_not_called() + fetcher.fetch_data.assert_not_called() + + def test_close_is_called_on_exit(self, fetcher): + """ + Test `close` is called on exit in with statement. + """ + fetcher.close = MagicMock(method='close') + + with fetcher: + pass + + fetcher.close.assert_called_once_with() + + class TestFimfictionFetcher: """ FimfictionFetcher tests.