Share dummy fetcher between task tests

This commit is contained in:
Joakim Soderlund 2020-03-21 19:48:34 +01:00
parent 0eb102b56e
commit feb0cbacaf
2 changed files with 88 additions and 51 deletions

81
tests/tasks/conftest.py Normal file
View file

@ -0,0 +1,81 @@
"""
Common task fixtures.
"""
#
# Fimfarchive, preserves stories from Fimfiction.
# Copyright (C) 2020 Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from typing import Dict
import pytest
from fimfarchive.exceptions import InvalidStoryError
from fimfarchive.fetchers import Fetcher
from fimfarchive.stories import Story
class DummyFetcher(Fetcher):
"""
Fetcher with local instance storage.
"""
def __init__(self):
"""
Constructor.
"""
self.stories: Dict[int, Story] = dict()
def add(self, key, date, flavors=()):
"""
Adds a story to the fetcher.
"""
story = Story(
key=key,
flavors=flavors,
data=f'Story {key}'.encode(),
meta={
'id': key,
'date_modified': date,
'chapters': [
{'id': key},
],
},
)
self.stories[key] = story
return story
def fetch(self, key):
"""
Returns a previously stored story.
"""
try:
return self.stories[key]
except KeyError:
raise InvalidStoryError()
@pytest.fixture
def dummy() -> DummyFetcher:
"""
Returns a `Fetcher` dummy.
"""
return DummyFetcher()

View file

@ -5,7 +5,7 @@ Update task tests.
#
# Fimfarchive, preserves stories from Fimfiction.
# Copyright (C) 2015 Joakim Soderlund
# Copyright (C) 2020 Joakim Soderlund
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -22,81 +22,37 @@ Update task tests.
#
from typing import Dict
from copy import deepcopy
from unittest.mock import MagicMock, call, patch
import pytest
from fimfarchive.exceptions import InvalidStoryError, StorySourceError
from fimfarchive.fetchers import Fetcher
from fimfarchive.exceptions import StorySourceError
from fimfarchive.flavors import DataFormat, StorySource, UpdateStatus
from fimfarchive.selectors import RefetchSelector, UpdateSelector
from fimfarchive.stories import Story
from fimfarchive.tasks.update import (
UpdateTask, SUCCESS_DELAY, SKIPPED_DELAY, FAILURE_DELAY,
)
class DummyFetcher(Fetcher):
"""
Fetcher with local instance storage.
"""
def __init__(self):
"""
Constructor.
"""
self.stories: Dict[int, Story] = dict()
def add(self, key, date, flavors=()):
"""
Adds a story to the fetcher.
"""
story = Story(
key=key,
flavors=flavors,
data=f'Story {key}',
meta={
'id': key,
'date_modified': date,
'chapters': [
{'id': key},
],
},
)
self.stories[key] = story
return story
def fetch(self, key):
"""
Returns a previously stored story.
"""
try:
return self.stories[key]
except KeyError:
raise InvalidStoryError()
class TestUpdateTask:
"""
Tests update task.
"""
@pytest.fixture
def fimfiction(self):
def fimfiction(self, dummy):
"""
Returns a `Fetcher` simulating Fimfiction.
"""
return DummyFetcher()
return deepcopy(dummy)
@pytest.fixture
def fimfarchive(self):
def fimfarchive(self, dummy):
"""
Returns a `Fetcher` simulating Fimfarchive.
"""
return DummyFetcher()
return deepcopy(dummy)
@pytest.fixture
def selector(self):