|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import tempfile |
| 4 | +import unittest |
| 5 | + |
| 6 | +from quote_to_image import fit_text, measure_layout, render_entry |
| 7 | + |
| 8 | +_HERE = os.path.dirname(os.path.abspath(__file__)) |
| 9 | +REGULAR = os.path.join(_HERE, 'LinLibertine_RZ.ttf') |
| 10 | +BOLD = os.path.join(_HERE, 'LinLibertine_RB.ttf') |
| 11 | +CREDIT = os.path.join(_HERE, 'LinLibertine_RZI.ttf') |
| 12 | + |
| 13 | +FONTS_AVAILABLE = all(os.path.exists(p) for p in (REGULAR, BOLD, CREDIT)) |
| 14 | + |
| 15 | +SHORT_QUOTE = "It was half past three in the afternoon." |
| 16 | +TIME_NAME = "half past three" |
| 17 | +TIME_START = 3 # word index of "half" |
| 18 | +TIME_COUNT = 2 # "half past three" = 3 words → count = 2 |
| 19 | + |
| 20 | +SAMPLE_ENTRY = { |
| 21 | + 'time': '15:30', |
| 22 | + 'time_name': TIME_NAME, |
| 23 | + 'quote': SHORT_QUOTE, |
| 24 | + 'source': 'Test Book', |
| 25 | + 'author': 'Test Author', |
| 26 | + 'idx': 0, |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +@unittest.skipUnless(FONTS_AVAILABLE, 'LinLibertine fonts not found') |
| 31 | +class TestMeasureLayout(unittest.TestCase): |
| 32 | + |
| 33 | + def test_returns_positive_height_for_short_quote(self): |
| 34 | + h = measure_layout(SHORT_QUOTE.split(), 600, 40, |
| 35 | + TIME_START, TIME_COUNT, 26, REGULAR, BOLD) |
| 36 | + self.assertIsNotNone(h) |
| 37 | + self.assertGreater(h, 0) |
| 38 | + |
| 39 | + def test_returns_none_when_font_too_large_for_narrow_width(self): |
| 40 | + # Width of 10px — any word at size 300 won't fit |
| 41 | + h = measure_layout(SHORT_QUOTE.split(), 10, 300, |
| 42 | + TIME_START, TIME_COUNT, 0, REGULAR, BOLD) |
| 43 | + self.assertIsNone(h) |
| 44 | + |
| 45 | + def test_larger_font_produces_greater_height(self): |
| 46 | + words = SHORT_QUOTE.split() |
| 47 | + kwargs = dict(time_start=TIME_START, time_count=TIME_COUNT, |
| 48 | + margin=26, regular_path=REGULAR, bold_path=BOLD) |
| 49 | + h_small = measure_layout(words, 600, 20, **kwargs) |
| 50 | + h_large = measure_layout(words, 600, 60, **kwargs) |
| 51 | + self.assertIsNotNone(h_small) |
| 52 | + self.assertIsNotNone(h_large) |
| 53 | + self.assertGreater(h_large, h_small) |
| 54 | + |
| 55 | + |
| 56 | +@unittest.skipUnless(FONTS_AVAILABLE, 'LinLibertine fonts not found') |
| 57 | +class TestFitText(unittest.TestCase): |
| 58 | + |
| 59 | + def test_returns_image_for_normal_quote(self): |
| 60 | + img = fit_text(SHORT_QUOTE.split(), 600, 800, |
| 61 | + TIME_START, TIME_COUNT, 26, REGULAR, BOLD) |
| 62 | + self.assertIsNotNone(img) |
| 63 | + |
| 64 | + def test_returned_image_has_correct_dimensions(self): |
| 65 | + img = fit_text(SHORT_QUOTE.split(), 600, 800, |
| 66 | + TIME_START, TIME_COUNT, 26, REGULAR, BOLD) |
| 67 | + self.assertEqual(img.size, (600, 800)) |
| 68 | + |
| 69 | + def test_returns_none_for_unrenderable_quote(self): |
| 70 | + # A single extremely long word that can never fit at size 18 |
| 71 | + words = ['A' * 500] |
| 72 | + img = fit_text(words, 100, 100, 0, 0, 0, REGULAR, BOLD) |
| 73 | + self.assertIsNone(img) |
| 74 | + |
| 75 | + def test_chosen_size_fits_within_height(self): |
| 76 | + """The rendered image should require height < height - 100.""" |
| 77 | + words = SHORT_QUOTE.split() |
| 78 | + margin = 26 |
| 79 | + width, height = 600, 800 |
| 80 | + |
| 81 | + img = fit_text(words, width, height, TIME_START, TIME_COUNT, |
| 82 | + margin, REGULAR, BOLD) |
| 83 | + self.assertIsNotNone(img) |
| 84 | + |
| 85 | + # Verify the winning size actually fits |
| 86 | + from quote_to_image import measure_layout as ml |
| 87 | + # Find what size fit_text chose by checking sizes around the result |
| 88 | + # We trust binary search is correct if size+1 overflows |
| 89 | + for size in range(18, 301): |
| 90 | + h = ml(words, width, size, TIME_START, TIME_COUNT, margin, REGULAR, BOLD) |
| 91 | + if h is None or h >= height - 100: |
| 92 | + winning_size = size - 1 |
| 93 | + break |
| 94 | + else: |
| 95 | + winning_size = 300 |
| 96 | + |
| 97 | + h_winner = ml(words, width, winning_size, TIME_START, TIME_COUNT, |
| 98 | + margin, REGULAR, BOLD) |
| 99 | + self.assertIsNotNone(h_winner) |
| 100 | + self.assertLess(h_winner, height - 100) |
| 101 | + |
| 102 | + |
| 103 | +@unittest.skipUnless(FONTS_AVAILABLE, 'LinLibertine fonts not found') |
| 104 | +class TestRenderEntrySaveWithoutCredits(unittest.TestCase): |
| 105 | + |
| 106 | + def setUp(self): |
| 107 | + self.tmpdir = tempfile.TemporaryDirectory() |
| 108 | + self._orig_dir = os.getcwd() |
| 109 | + os.chdir(self.tmpdir.name) |
| 110 | + os.makedirs(os.path.join('images', 'metadata'), exist_ok=True) |
| 111 | + |
| 112 | + def tearDown(self): |
| 113 | + os.chdir(self._orig_dir) |
| 114 | + self.tmpdir.cleanup() |
| 115 | + |
| 116 | + def _credits_path(self): |
| 117 | + return os.path.join('images', 'metadata', 'quote_1530_0_credits.png') |
| 118 | + |
| 119 | + def _quote_path(self): |
| 120 | + return os.path.join('images', 'quote_1530_0.png') |
| 121 | + |
| 122 | + def test_default_only_writes_credits(self): |
| 123 | + render_entry(SAMPLE_ENTRY, 600, 800, |
| 124 | + (REGULAR, BOLD, CREDIT), save_without_credits=False) |
| 125 | + self.assertTrue(os.path.exists(self._credits_path())) |
| 126 | + self.assertFalse(os.path.exists(self._quote_path())) |
| 127 | + |
| 128 | + def test_flag_writes_both_files(self): |
| 129 | + render_entry(SAMPLE_ENTRY, 600, 800, |
| 130 | + (REGULAR, BOLD, CREDIT), save_without_credits=True) |
| 131 | + self.assertTrue(os.path.exists(self._credits_path())) |
| 132 | + self.assertTrue(os.path.exists(self._quote_path())) |
| 133 | + |
| 134 | + def test_skip_if_credits_already_exists(self): |
| 135 | + # Pre-create the credits file |
| 136 | + credits_path = self._credits_path() |
| 137 | + with open(credits_path, 'wb') as f: |
| 138 | + f.write(b'placeholder') |
| 139 | + |
| 140 | + render_entry(SAMPLE_ENTRY, 600, 800, |
| 141 | + (REGULAR, BOLD, CREDIT), save_without_credits=False) |
| 142 | + |
| 143 | + # File should be unchanged (still our placeholder) |
| 144 | + with open(credits_path, 'rb') as f: |
| 145 | + self.assertEqual(f.read(), b'placeholder') |
| 146 | + |
| 147 | + def test_skip_if_both_files_exist_with_flag(self): |
| 148 | + quote_path = self._quote_path() |
| 149 | + credits_path = self._credits_path() |
| 150 | + |
| 151 | + for path in (quote_path, credits_path): |
| 152 | + with open(path, 'wb') as f: |
| 153 | + f.write(b'placeholder') |
| 154 | + |
| 155 | + render_entry(SAMPLE_ENTRY, 600, 800, |
| 156 | + (REGULAR, BOLD, CREDIT), save_without_credits=True) |
| 157 | + |
| 158 | + for path in (quote_path, credits_path): |
| 159 | + with open(path, 'rb') as f: |
| 160 | + self.assertEqual(f.read(), b'placeholder') |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == '__main__': |
| 164 | + unittest.main() |
0 commit comments