"""Tests for the three-up board composer (no network, no fixture images).""" import json import unittest from pathlib import Path from tempfile import TemporaryDirectory from unittest import mock from PIL import Image import three_up as tu def _img(w, h, colour=(200, 100, 50)): return Image.new("RGB", (w, h), colour) class ComposeTests(unittest.TestCase): def test_panels_share_the_height_and_labels_are_baked_in(self): board = tu.compose_board(_img(1600, 800), _img(1280, 720), _img(640, 480)) self.assertEqual(board.height, tu.PANEL_H + tu.LABEL_H) widths = [p.width for p in [board.crop((tu.GUTTER, tu.LABEL_H, tu.GUTTER + 720, board.height))]] # three panels + four gutters span the full width expected = (720 + 640 + 480) + tu.GUTTER * 4 self.assertEqual(board.width, expected) def test_board_is_never_darker_than_the_frame_colour(self): board = tu.compose_board(_img(100, 100, (0, 0, 0)), _img(100, 100), _img(100, 100)) # the label strip keeps the frame colour even with black panels strip = board.crop((0, 0, board.width, tu.LABEL_H)) colours = set(strip.getdata()) self.assertIn((24, 24, 24), colours) class NamingTests(unittest.TestCase): def test_board_name_must_be_a_slug_ending_in_board(self): for bad in ("", "with space-board", "slash/board", "no-suffix", "x.jpg"): with self.assertRaises(SystemExit, msg=bad): tu._board_path(bad) def test_board_path_lands_in_the_boards_dir(self): self.assertEqual(tu._board_path("elcapitan-day-board"), tu.BOARDS_DIR / "elcapitan-day-board.jpg") class MissingInputTests(unittest.TestCase): def test_compose_exits_on_a_missing_image(self): with self.assertRaises(SystemExit): tu._load("definitely/not/here.png") class RowsTests(unittest.TestCase): def test_rows_emit_captions_from_the_sidecars(self): with mock.patch.object(tu, "BOARDS_DIR", Path(self.enterContext(TemporaryDirectory()))): board = tu.BOARDS_DIR / "x-board.jpg" board.write_bytes(b"jpg") board.with_suffix(".json").write_text(json.dumps( {"caption": "A venue, day."})) with mock.patch("builtins.print") as printed: self.assertEqual(tu.cmd_rows(None), 0) out = "\n".join(str(c[0][0]) for c in printed.call_args_list) self.assertIn("![three-up board: x](x-board.jpg)", out) self.assertIn("A venue, day.", out) if __name__ == "__main__": unittest.main()