From: Scott Worley Date: Wed, 20 Dec 2023 21:10:55 +0000 (-0800) Subject: FakeImageStore for tests X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/4b7d905ec23e1190bcb65a3041816e07f2663d9b FakeImageStore for tests --- diff --git a/args.py b/args.py index d90de8a..f966ba9 100644 --- a/args.py +++ b/args.py @@ -16,7 +16,7 @@ from xdg_base_dirs import xdg_cache_home from domfilter import ApplyDOMFilters, DOMFilters from fetch import CachingFetcher from htmlfilter import ApplyHTMLFilters, HTMLFilters -from images import ImageStore +from images import DiskImageStore from spec import Spec from texify import PandocTexifier @@ -67,7 +67,7 @@ def spec_from_commandline_args() -> Iterator[Spec]: yield Spec( args.url, fetcher, - ImageStore(args.out + '_images', fetcher), + DiskImageStore(args.out + '_images', fetcher), lambda x: ApplyHTMLFilters(args.htmlfilters, x), lambda x: ApplyDOMFilters(args.domfilters, x), PandocTexifier(args.pandoc or 'pandoc'), diff --git a/images.py b/images.py index 178f708..d9926a3 100644 --- a/images.py +++ b/images.py @@ -4,17 +4,25 @@ # under the terms of the GNU General Public License as published by the # Free Software Foundation, version 3. +from abc import ABC, abstractmethod import os import os.path from fetch import Fetcher +class ImageStore(ABC): + + @abstractmethod + def get_image(self, url: str) -> str: + raise NotImplementedError() + + class ImageStoreError(Exception): pass -class ImageStore: +class DiskImageStore(ImageStore): def __init__(self, root_path: str, fetcher: Fetcher) -> None: self._root_path = root_path @@ -46,3 +54,9 @@ class ImageStore: self._filenames.add(name) self._images[url] = path return self._images[url] + + +class FakeImageStore(ImageStore): + + def get_image(self, url: str) -> str: + return 'stored:' + url diff --git a/images_test.py b/images_test.py index 8ada1ee..3f3fd13 100644 --- a/images_test.py +++ b/images_test.py @@ -8,7 +8,7 @@ import unittest from fetch import FakeFetcher -from images import ImageStore +from images import DiskImageStore class TestImageStore(unittest.TestCase): @@ -22,7 +22,7 @@ class TestImageStore(unittest.TestCase): 'https://example.com/other_images/carol': b'CAROLINA'}) def testFetchOnce(self) -> None: - store = ImageStore('istest_fetch_once', self._fetcher) + store = DiskImageStore('istest_fetch_once', self._fetcher) self.assertEqual(self._fetcher.request_count(), 0) a1 = store.get_image('https://example.com/images/alice.png') self.assertEqual(self._fetcher.request_count(), 1) @@ -48,7 +48,7 @@ class TestImageStore(unittest.TestCase): self.assertEqual(a1, a3) def testNameCollision(self) -> None: - store = ImageStore('istest_name_collision', self._fetcher) + store = DiskImageStore('istest_name_collision', self._fetcher) self.assertEqual(self._fetcher.request_count(), 0) b1 = store.get_image('https://example.com/images/bob.jpeg') self.assertEqual(self._fetcher.request_count(), 1) diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index 249b6e4..7ceb3ea 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -15,7 +15,7 @@ import paperdoorknob from testing.fakeserver import FakeGlowficServer from domfilter import ApplyDOMFilters from fetch import DirectFetcher, FakeFetcher, Fetcher -from images import ImageStore +from images import FakeImageStore from spec import Spec from texify import DirectTexifier, PandocTexifier, VerifyingTexifier @@ -37,7 +37,7 @@ class BaseTestProcess(ABC): spec = Spec( self.url(), self.fetcher(), - ImageStore('is', self.fetcher()), + FakeImageStore(), lambda x: x, lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), PandocTexifier('pandoc'), @@ -60,7 +60,7 @@ Pretty sure. spec = Spec( self.url(), self.fetcher(), - ImageStore('is', self.fetcher()), + FakeImageStore(), lambda x: x, lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), texifier, @@ -73,7 +73,7 @@ Pretty sure. spec = Spec( self.url(), self.fetcher(), - ImageStore('is', self.fetcher()), + FakeImageStore(), lambda x: x, lambda x: ApplyDOMFilters('NoEdit,NoFooter', x), PandocTexifier('pandoc'),