Add mapper for story path

This commit is contained in:
Joakim Soderlund 2017-01-07 22:55:12 +01:00
parent 8209897df5
commit 86559d9d5d
2 changed files with 55 additions and 1 deletions

View file

@ -22,6 +22,9 @@ Mappers for Fimfarchive.
#
import os
class Mapper:
"""
Callable which maps something to something else.
@ -41,3 +44,18 @@ class StaticMapper(Mapper):
def __call__(self, *args, **kwargs):
return self.value
class StoryPathMapper(Mapper):
"""
Returns a key-based file path for a story.
"""
def __init__(self, directory):
self.directory = directory
def __call__(self, story):
directory = str(self.directory)
key = str(story.key)
return os.path.join(directory, key)

View file

@ -22,9 +22,12 @@ Mapper tests.
#
import os
from unittest.mock import MagicMock
import pytest
from fimfarchive.mappers import StaticMapper
from fimfarchive.mappers import StaticMapper, StoryPathMapper
class TestStaticMapper:
@ -66,3 +69,36 @@ class TestStaticMapper:
"""
mapper = StaticMapper(value)
assert mapper(a=1, b=2) is value
class TestStoryPathMapper:
"""
StoryPathMapper tests.
"""
def test_joins_paths(self, story):
"""
Tests returns directory joined with story key.
"""
directory = os.path.join('some', 'directory')
path = os.path.join(directory, str(story.key))
mapper = StoryPathMapper(directory)
assert mapper(story) == path
def test_casts_values(self, tmpdir, story):
"""
Tests casts all values to string when joining.
"""
directory = MagicMock()
directory.__str__.return_value = 'dir'
story.key = MagicMock()
story.key.__str__.return_value = 'key'
mapper = StoryPathMapper(directory)
assert mapper(story) == os.path.join('dir', 'key')
assert directory.__str__.called_once_with()
assert story.key.__str__.called_once_with()