From a66ff04f321e91de3a32596fc3e6f4190f161f92 Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Wed, 25 Oct 2017 19:47:03 +0200 Subject: [PATCH] Add selector for refetching data during updates --- fimfarchive/selectors.py | 13 +++++++++ tests/test_selectors.py | 62 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/fimfarchive/selectors.py b/fimfarchive/selectors.py index a579337..3f206e4 100644 --- a/fimfarchive/selectors.py +++ b/fimfarchive/selectors.py @@ -29,6 +29,7 @@ from fimfarchive.mappers import StoryDateMapper __all__ = ( 'Selector', + 'RefetchSelector', 'UpdateSelector', ) @@ -148,3 +149,15 @@ class UpdateSelector(Selector): return self.flavored(old, UpdateStatus.DELETED) else: return None + + +class RefetchSelector(UpdateSelector): + """ + Selects the new story if it is available. + """ + + def filter_unchanged(self, old, new): + """ + Returns the new story. + """ + return new diff --git a/tests/test_selectors.py b/tests/test_selectors.py index 28d49ff..e33c0b0 100644 --- a/tests/test_selectors.py +++ b/tests/test_selectors.py @@ -30,7 +30,7 @@ from fimfarchive.exceptions import InvalidStoryError from fimfarchive.fetchers import Fetcher from fimfarchive.flavors import UpdateStatus from fimfarchive.mappers import StoryDateMapper -from fimfarchive.selectors import UpdateSelector +from fimfarchive.selectors import RefetchSelector, UpdateSelector from fimfarchive.stories import Story @@ -134,7 +134,7 @@ class TestUpdateSelector: def test_filter_unchanged_for_unchanged(self, selector, story): """ - Tests `filter_unchanged` drops changed stories. + Tests `filter_unchanged` drops unchanged stories. """ old = self.populate(story, 0) new = self.populate(story, 0) @@ -288,3 +288,61 @@ class TestUpdateSelector: selected = selector(None, None) assert selected is None + + +class TestRefetchSelector(TestUpdateSelector): + """ + RefetchSelector tests. + """ + + @pytest.fixture + def selector(self): + """ + Returns a new refetch selector. + """ + return RefetchSelector() + + def test_filter_unchanged_for_unchanged(self, selector, story): + """ + Tests `filter_unchanged` keeps unchanged stories. + """ + old = self.populate(story, 0) + new = self.populate(story, 0) + + selected = selector.filter_unchanged(old, new) + + assert selected is new + + def test_filter_unchanged_missing_old_date(self, selector, story): + """ + Tests `filter_unchanged` keeps stories when missing old date. + """ + old = self.populate(story, None) + new = self.populate(story, 1) + + selected = selector.filter_unchanged(old, new) + + assert selected is new + + def test_filter_unchanged_missing_new_date(self, selector, story): + """ + Tests `filter_unchanged` keeps stories when missing new date. + """ + old = self.populate(story, 1) + new = self.populate(story, None) + + selected = selector.filter_unchanged(old, new) + + assert selected is new + + def test_revived_selection(self, selector, story): + """ + Tests behavior for stories that have not updated. + """ + old = self.populate(story, 1) + new = self.populate(story, 1) + + selected = selector(old, new) + + assert selected is new + assert UpdateStatus.UPDATED in new.flavors