mirror of
https://github.com/JockeTF/fimfarchive.git
synced 2024-11-22 13:28:00 +01:00
Add mapper for story path
This commit is contained in:
parent
8209897df5
commit
86559d9d5d
2 changed files with 55 additions and 1 deletions
|
@ -22,6 +22,9 @@ Mappers for Fimfarchive.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Mapper:
|
class Mapper:
|
||||||
"""
|
"""
|
||||||
Callable which maps something to something else.
|
Callable which maps something to something else.
|
||||||
|
@ -41,3 +44,18 @@ class StaticMapper(Mapper):
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
return self.value
|
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)
|
||||||
|
|
|
@ -22,9 +22,12 @@ Mapper tests.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
from unittest.mock import MagicMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from fimfarchive.mappers import StaticMapper
|
from fimfarchive.mappers import StaticMapper, StoryPathMapper
|
||||||
|
|
||||||
|
|
||||||
class TestStaticMapper:
|
class TestStaticMapper:
|
||||||
|
@ -66,3 +69,36 @@ class TestStaticMapper:
|
||||||
"""
|
"""
|
||||||
mapper = StaticMapper(value)
|
mapper = StaticMapper(value)
|
||||||
assert mapper(a=1, b=2) is 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()
|
||||||
|
|
Loading…
Reference in a new issue