]> git.scottworley.com Git - paperdoorknob/commitdiff
FakeImageStore for tests
authorScott Worley <scottworley@scottworley.com>
Wed, 20 Dec 2023 21:10:55 +0000 (13:10 -0800)
committerScott Worley <scottworley@scottworley.com>
Wed, 20 Dec 2023 21:10:55 +0000 (13:10 -0800)
args.py
images.py
images_test.py
paperdoorknob_test.py

diff --git a/args.py b/args.py
index d90de8a78b7e32207c66fe6ee19d7939de634483..f966ba92ddcd0d6f2044556d9323d2508c296f43 100644 (file)
--- 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 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
 
 from spec import Spec
 from texify import PandocTexifier
 
@@ -67,7 +67,7 @@ def spec_from_commandline_args() -> Iterator[Spec]:
             yield Spec(
                 args.url,
                 fetcher,
             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'),
                 lambda x: ApplyHTMLFilters(args.htmlfilters, x),
                 lambda x: ApplyDOMFilters(args.domfilters, x),
                 PandocTexifier(args.pandoc or 'pandoc'),
index 178f7085f5a10a4feeae780b04707aacc92b07ae..d9926a3c1387b072919855b3c78d9bc023a8e719 100644 (file)
--- 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.
 
 # 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
 
 
 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 ImageStoreError(Exception):
     pass
 
 
-class ImageStore:
+class DiskImageStore(ImageStore):
 
     def __init__(self, root_path: str, fetcher: Fetcher) -> None:
         self._root_path = root_path
 
     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]
             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
index 8ada1eec9bdd51991f6a99f4ac1f4a17fcecdf03..3f3fd13b3e7c2329321f1885cc1bfb07f08308c4 100644 (file)
@@ -8,7 +8,7 @@
 import unittest
 
 from fetch import FakeFetcher
 import unittest
 
 from fetch import FakeFetcher
-from images import ImageStore
+from images import DiskImageStore
 
 
 class TestImageStore(unittest.TestCase):
 
 
 class TestImageStore(unittest.TestCase):
@@ -22,7 +22,7 @@ class TestImageStore(unittest.TestCase):
             'https://example.com/other_images/carol': b'CAROLINA'})
 
     def testFetchOnce(self) -> None:
             '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)
         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:
         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)
         self.assertEqual(self._fetcher.request_count(), 0)
         b1 = store.get_image('https://example.com/images/bob.jpeg')
         self.assertEqual(self._fetcher.request_count(), 1)
index 249b6e42e936fa5f60a18e021c133dc824ca15a6..7ceb3eaf2cd77f7f98f0a28b6e1886baa1d801b1 100644 (file)
@@ -15,7 +15,7 @@ import paperdoorknob
 from testing.fakeserver import FakeGlowficServer
 from domfilter import ApplyDOMFilters
 from fetch import DirectFetcher, FakeFetcher, Fetcher
 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
 
 from spec import Spec
 from texify import DirectTexifier, PandocTexifier, VerifyingTexifier
 
@@ -37,7 +37,7 @@ class BaseTestProcess(ABC):
         spec = Spec(
             self.url(),
             self.fetcher(),
         spec = Spec(
             self.url(),
             self.fetcher(),
-            ImageStore('is', self.fetcher()),
+            FakeImageStore(),
             lambda x: x,
             lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
             PandocTexifier('pandoc'),
             lambda x: x,
             lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
             PandocTexifier('pandoc'),
@@ -60,7 +60,7 @@ Pretty sure.
         spec = Spec(
             self.url(),
             self.fetcher(),
         spec = Spec(
             self.url(),
             self.fetcher(),
-            ImageStore('is', self.fetcher()),
+            FakeImageStore(),
             lambda x: x,
             lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
             texifier,
             lambda x: x,
             lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
             texifier,
@@ -73,7 +73,7 @@ Pretty sure.
             spec = Spec(
                 self.url(),
                 self.fetcher(),
             spec = Spec(
                 self.url(),
                 self.fetcher(),
-                ImageStore('is', self.fetcher()),
+                FakeImageStore(),
                 lambda x: x,
                 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
                 PandocTexifier('pandoc'),
                 lambda x: x,
                 lambda x: ApplyDOMFilters('NoEdit,NoFooter', x),
                 PandocTexifier('pandoc'),