From 86559d9d5df4e1e511c1ebf6d5cb841903fc124e Mon Sep 17 00:00:00 2001 From: Joakim Soderlund Date: Sat, 7 Jan 2017 22:55:12 +0100 Subject: [PATCH] Add mapper for story path --- fimfarchive/mappers.py | 18 ++++++++++++++++++ tests/test_mappers.py | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/fimfarchive/mappers.py b/fimfarchive/mappers.py index df44e25..aee6802 100644 --- a/fimfarchive/mappers.py +++ b/fimfarchive/mappers.py @@ -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) diff --git a/tests/test_mappers.py b/tests/test_mappers.py index 9b36280..91e29d7 100644 --- a/tests/test_mappers.py +++ b/tests/test_mappers.py @@ -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()