Implement fetch method in fetcher base class

This commit is contained in:
Joakim Soderlund 2016-12-18 19:46:46 +01:00
parent dbebdfae98
commit 80e105abb9
2 changed files with 92 additions and 2 deletions

View file

@ -32,6 +32,7 @@ from zipfile import ZipFile, BadZipFile
import requests import requests
from fimfarchive.exceptions import InvalidStoryError, StorySourceError from fimfarchive.exceptions import InvalidStoryError, StorySourceError
from fimfarchive.stories import Story
StreamReader = codecs.getreader('utf-8') StreamReader = codecs.getreader('utf-8')
@ -62,12 +63,14 @@ class Fetcher:
""" """
pass pass
def fetch(self, key): def fetch(self, key, prefetch_meta=None, prefetch_data=None):
""" """
Fetches story information. Fetches story information.
Args: Args:
key: Primary key of the story. key: Primary key of the story.
prefetch_meta: Force prefetching of meta.
prefetch_data: Force prefetching of data.
Returns: Returns:
Story: A new `Story` object. Story: A new `Story` object.
@ -76,7 +79,23 @@ class Fetcher:
InvalidStoryError: If a valid story is not found. InvalidStoryError: If a valid story is not found.
StorySourceError: If source does not return any data. 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): def fetch_data(self, key):
""" """

View file

@ -22,6 +22,8 @@ Fetcher tests.
# #
from unittest.mock import MagicMock
import pytest import pytest
from fimfarchive.exceptions import InvalidStoryError, StorySourceError from fimfarchive.exceptions import InvalidStoryError, StorySourceError
@ -36,6 +38,75 @@ PROTECTED_STORY_KEY = 208799
FIMFARCHIVE_PATH = 'fimfarchive-20160525.zip' 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: class TestFimfictionFetcher:
""" """
FimfictionFetcher tests. FimfictionFetcher tests.