Add flavor stamper

This commit is contained in:
Joakim Soderlund 2019-06-16 13:52:19 +02:00
parent c4ba2e8983
commit 9ca1872343
2 changed files with 56 additions and 4 deletions

View file

@ -26,7 +26,7 @@ from typing import Any, Callable, Dict, Optional, Set
import arrow
from fimfarchive.flavors import UpdateStatus
from fimfarchive.flavors import Flavor, UpdateStatus
from fimfarchive.stories import Story
from fimfarchive.utils import find_flavor
@ -108,6 +108,27 @@ class UpdateStamper(Stamper):
archive[key] = None
class FlavorStamper(Stamper):
"""
Adds flavors to stories.
"""
def __init__(self, mapper: Callable[[Story], Optional[Flavor]]) -> None:
"""
Constructor.
Args:
mapper: Callable returning the flavor to stamp with.
"""
self.map = mapper
def __call__(self, story: Story) -> None:
flavor = self.map(story)
if flavor:
story.flavors.add(flavor)
class PathStamper(Stamper):
"""
Adds archive paths to stories.
@ -118,7 +139,7 @@ class PathStamper(Stamper):
Constructor.
Args:
mapper: Callable returning the path to stamp.
mapper: Callable returning the path to stamp with.
"""
self.map = mapper

View file

@ -28,9 +28,11 @@ from typing import Dict
import arrow
import pytest
from fimfarchive.flavors import UpdateStatus
from fimfarchive.flavors import DataFormat, MetaFormat, UpdateStatus
from fimfarchive.mappers import StaticMapper
from fimfarchive.stampers import Stamper, PathStamper, UpdateStamper
from fimfarchive.stampers import (
Stamper, FlavorStamper, PathStamper, UpdateStamper,
)
class TestStamper:
@ -190,6 +192,35 @@ class TestUpdateStamper:
assert archive['date_updated'] == prev
class TestFlavorStamper:
"""
FlavorStamper tests.
"""
@pytest.mark.parametrize('value', (None, ''))
def test_ignored_blank_value(self, story, value):
"""
Tests blank values are ignored.
"""
stamp = FlavorStamper(StaticMapper(value))
stamp(story)
assert value not in story.flavors
@pytest.mark.parametrize('value', (
DataFormat.EPUB,
MetaFormat.ALPHA,
))
def test_stamped_value(self, story, value):
"""
Tests values are stamped to stories.
"""
stamp = FlavorStamper(StaticMapper(value))
stamp(story)
assert value in story.flavors
class TestPathStamper:
"""
PathStamper tests.