diff --git a/tests/tasks/conftest.py b/tests/tasks/conftest.py new file mode 100644 index 0000000..5045004 --- /dev/null +++ b/tests/tasks/conftest.py @@ -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 . +# + + +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() diff --git a/tests/tasks/test_update.py b/tests/tasks/test_update.py index 3e061d3..70f0705 100644 --- a/tests/tasks/test_update.py +++ b/tests/tasks/test_update.py @@ -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):