]> git.scottworley.com Git - paperdoorknob/blame_incremental - images_test.py
Fix flaky test: Increment request count before responding
[paperdoorknob] / images_test.py
... / ...
CommitLineData
1# paperdoorknob: Print glowfic
2#
3# This program is free software: you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation, version 3.
6
7
8import unittest
9
10from fetch import FakeFetcher
11from images import DiskImageStore
12
13
14class TestImageStore(unittest.TestCase):
15 def setUp(self) -> None:
16 self._fetcher = FakeFetcher({
17 'https://example.com/images/alice.png': b'ALICE',
18 'https://example.com/images/bob.jpeg': b'BOB',
19 'https://example.com/alt_images/bob.jpeg': b'BOBBY',
20 'https://example.com/images/carol': b'CAROL',
21 'https://example.com/alt_images/carol': b'CAROLINE',
22 'https://example.com/other_images/carol': b'CAROLINA'})
23
24 def testFetchOnce(self) -> None:
25 store = DiskImageStore('istest_fetch_once', self._fetcher)
26 self.assertEqual(self._fetcher.request_count(), 0)
27 a1 = store.get_image('https://example.com/images/alice.png')
28 self.assertEqual(self._fetcher.request_count(), 1)
29 a2 = store.get_image('https://example.com/images/alice.png')
30 self.assertEqual(self._fetcher.request_count(), 1)
31 self.assertEqual(a1, a2)
32 self.assertEqual(a1, 'istest_fetch_once/alice.png')
33 with open(a1, 'rb') as f:
34 self.assertEqual(f.read(), b'ALICE')
35
36 self.assertEqual(self._fetcher.request_count(), 1)
37 b1 = store.get_image('https://example.com/images/bob.jpeg')
38 self.assertEqual(self._fetcher.request_count(), 2)
39 b2 = store.get_image('https://example.com/images/bob.jpeg')
40 self.assertEqual(self._fetcher.request_count(), 2)
41 self.assertEqual(b1, b2)
42 self.assertEqual(b1, 'istest_fetch_once/bob.jpeg')
43 with open(b1, 'rb') as f:
44 self.assertEqual(f.read(), b'BOB')
45
46 a3 = store.get_image('https://example.com/images/alice.png')
47 self.assertEqual(self._fetcher.request_count(), 2)
48 self.assertEqual(a1, a3)
49
50 def testNameCollision(self) -> None:
51 store = DiskImageStore('istest_name_collision', self._fetcher)
52 self.assertEqual(self._fetcher.request_count(), 0)
53 b1 = store.get_image('https://example.com/images/bob.jpeg')
54 self.assertEqual(self._fetcher.request_count(), 1)
55 b2 = store.get_image('https://example.com/alt_images/bob.jpeg')
56 self.assertEqual(self._fetcher.request_count(), 2)
57 self.assertNotEqual(b1, b2)
58 self.assertEqual(b1, 'istest_name_collision/bob.jpeg')
59 self.assertEqual(b2, 'istest_name_collision/bob-0000.jpeg')
60 with open(b1, 'rb') as f:
61 self.assertEqual(f.read(), b'BOB')
62 with open(b2, 'rb') as f:
63 self.assertEqual(f.read(), b'BOBBY')
64
65 self.assertEqual(self._fetcher.request_count(), 2)
66 c1 = store.get_image('https://example.com/images/carol')
67 self.assertEqual(self._fetcher.request_count(), 3)
68 c2 = store.get_image('https://example.com/alt_images/carol')
69 self.assertEqual(self._fetcher.request_count(), 4)
70 c3 = store.get_image('https://example.com/other_images/carol')
71 self.assertEqual(self._fetcher.request_count(), 5)
72 self.assertNotEqual(c1, c2)
73 self.assertNotEqual(c2, c3)
74 self.assertNotEqual(c1, c3)
75 self.assertEqual(c1, 'istest_name_collision/carol')
76 self.assertEqual(c2, 'istest_name_collision/carol-0000')
77 self.assertEqual(c3, 'istest_name_collision/carol-0001')
78 with open(c1, 'rb') as f:
79 self.assertEqual(f.read(), b'CAROL')
80 with open(c2, 'rb') as f:
81 self.assertEqual(f.read(), b'CAROLINE')
82 with open(c3, 'rb') as f:
83 self.assertEqual(f.read(), b'CAROLINA')
84
85
86if __name__ == '__main__':
87 unittest.main()