Add selector to update task arguments

This commit is contained in:
Joakim Soderlund 2017-10-25 21:09:50 +02:00
parent a66ff04f32
commit c3b014d06e
2 changed files with 39 additions and 4 deletions

View file

@ -30,7 +30,7 @@ from fimfarchive.exceptions import InvalidStoryError
from fimfarchive.fetchers import Fetcher from fimfarchive.fetchers import Fetcher
from fimfarchive.flavors import DataFormat, UpdateStatus from fimfarchive.flavors import DataFormat, UpdateStatus
from fimfarchive.mappers import StoryPathMapper from fimfarchive.mappers import StoryPathMapper
from fimfarchive.selectors import UpdateSelector from fimfarchive.selectors import Selector, UpdateSelector
from fimfarchive.signals import Signal, SignalSender from fimfarchive.signals import Signal, SignalSender
from fimfarchive.stories import Story from fimfarchive.stories import Story
from fimfarchive.utils import PersistedDict from fimfarchive.utils import PersistedDict
@ -74,6 +74,7 @@ class UpdateTask(SignalSender):
self, self,
fimfarchive: Fetcher, fimfarchive: Fetcher,
fimfiction: Fetcher, fimfiction: Fetcher,
selector: Selector = None,
workdir: str = DEFAULT_WORKDIR, workdir: str = DEFAULT_WORKDIR,
retries: int = DEFAULT_RETRIES, retries: int = DEFAULT_RETRIES,
skips: int = DEFAULT_SKIPS, skips: int = DEFAULT_SKIPS,
@ -84,14 +85,19 @@ class UpdateTask(SignalSender):
Args: Args:
fimfarchive: Fetcher for the old release. fimfarchive: Fetcher for the old release.
fimfiction: Fetcher for the new release. fimfiction: Fetcher for the new release.
selector: Selector for which story to save.
workdir: Path for storage of state and stories. workdir: Path for storage of state and stories.
retries: Number of retries before giving up. retries: Number of retries before giving up.
skips: Number of skips before giving up. skips: Number of skips before giving up.
""" """
super().__init__() super().__init__()
if selector is None:
selector = UpdateSelector()
self.fimfarchive = fimfarchive self.fimfarchive = fimfarchive
self.fimfiction = fimfiction self.fimfiction = fimfiction
self.select = selector
self.workdir = workdir self.workdir = workdir
self.retries = retries self.retries = retries
self.skips = skips self.skips = skips
@ -100,8 +106,6 @@ class UpdateTask(SignalSender):
state_path = os.path.join(self.workdir, self.state_file) state_path = os.path.join(self.workdir, self.state_file)
self.state = PersistedDict(state_path, self.state_vars) self.state = PersistedDict(state_path, self.state_vars)
self.select = UpdateSelector()
meta_mapper = self.get_mapper('meta') meta_mapper = self.get_mapper('meta')
skip_mapper = self.get_mapper('skip') skip_mapper = self.get_mapper('skip')
epub_mapper = self.get_mapper('epub') epub_mapper = self.get_mapper('epub')

View file

@ -29,6 +29,7 @@ import pytest
from fimfarchive.exceptions import InvalidStoryError, StorySourceError from fimfarchive.exceptions import InvalidStoryError, StorySourceError
from fimfarchive.fetchers import Fetcher from fimfarchive.fetchers import Fetcher
from fimfarchive.flavors import DataFormat, UpdateStatus from fimfarchive.flavors import DataFormat, UpdateStatus
from fimfarchive.selectors import RefetchSelector, UpdateSelector
from fimfarchive.stories import Story from fimfarchive.stories import Story
from fimfarchive.tasks.update import ( from fimfarchive.tasks.update import (
UpdateTask, SUCCESS_DELAY, SKIPPED_DELAY, FAILURE_DELAY, UpdateTask, SUCCESS_DELAY, SKIPPED_DELAY, FAILURE_DELAY,
@ -97,13 +98,21 @@ class TestUpdateTask:
return DummyFetcher() return DummyFetcher()
@pytest.fixture @pytest.fixture
def task(self, fimfarchive, fimfiction, tmpdir): def selector(self):
"""
Returns an `UpdateSelector` instance.
"""
return UpdateSelector()
@pytest.fixture
def task(self, fimfarchive, fimfiction, selector, tmpdir):
""" """
Returns an `UpdateTask` instance. Returns an `UpdateTask` instance.
""" """
return UpdateTask( return UpdateTask(
fimfiction=fimfiction, fimfiction=fimfiction,
fimfarchive=fimfarchive, fimfarchive=fimfarchive,
selector=selector,
workdir=str(tmpdir), workdir=str(tmpdir),
retries=2, retries=2,
skips=2, skips=2,
@ -267,3 +276,25 @@ class TestUpdateTask:
with pytest.raises(ValueError): with pytest.raises(ValueError):
task.write(story) task.write(story)
class TestRefetchingUpdateTask(TestUpdateTask):
"""
Tests update task with a refetch selector.
"""
@pytest.fixture
def selector(self):
"""
Returns a `RefetchSelector` instance.
"""
return RefetchSelector()
def test_revived_story(self, task, fimfarchive, fimfiction):
"""
Tests updating for a revived story.
"""
fimfarchive.add(key=1, date=1)
target = fimfiction.add(key=1, date=1)
self.verify_fetch(task, target, UpdateStatus.UPDATED)