| 1 | import json |
| 2 | import unittest |
| 3 | from pathlib import Path |
| 4 | |
| 5 | from indexer.cluster import Post, cluster_posts |
| 6 | from indexer.write_index import write_index |
| 7 | |
| 8 | ROOT = Path(__file__).resolve().parents[2] |
| 9 | FIXTURE = ROOT / "testdata" / "posts.json" |
| 10 | |
| 11 | |
| 12 | class ClusterTests(unittest.TestCase): |
| 13 | def setUp(self): |
| 14 | raw = json.loads(FIXTURE.read_text(encoding="utf-8"))["data"] |
| 15 | self.posts = [Post.from_reddit(p) for p in raw] |
| 16 | self.posts = [p for p in self.posts if p] |
| 17 | |
| 18 | def test_groups_a_series_by_title_and_links(self): |
| 19 | novels = {n.name.lower(): n for n in cluster_posts(self.posts)} |
| 20 | # Meta en link-posts vallen af; drie verhalen over. |
| 21 | self.assertEqual(len(novels), 3) |
| 22 | terran = novels["the last terran"] |
| 23 | self.assertEqual([c.id for c in terran.chapters], ["aaa111", "aaa222", "aaa333"]) |
| 24 | self.assertEqual(terran.author, "WriterOne") |
| 25 | self.assertFalse(terran.oneshot) |
| 26 | self.assertEqual(terran.id, "aaa111") |
| 27 | |
| 28 | def test_oneshot_and_nsfw(self): |
| 29 | novels = {n.author: n for n in cluster_posts(self.posts)} |
| 30 | self.assertTrue(novels["WriterTwo"].oneshot) |
| 31 | self.assertTrue(novels["WriterThree"].over_18) |
| 32 | self.assertEqual(len(novels["WriterThree"].chapters), 2) |
| 33 | |
| 34 | def test_write_index_contains_no_story_text(self): |
| 35 | import tempfile |
| 36 | |
| 37 | novels = cluster_posts(self.posts) |
| 38 | with tempfile.TemporaryDirectory() as tmp: |
| 39 | meta = write_index(novels, Path(tmp), page_size=2) |
| 40 | blob = Path(tmp).joinpath("series/a/aaa111.json").read_text(encoding="utf-8") |
| 41 | self.assertNotIn("It begins", blob) |
| 42 | self.assertNotIn("The end of this chapter", blob) |
| 43 | self.assertIn("aaa111", blob) |
| 44 | self.assertEqual(meta["novelCount"], 3) |
| 45 | self.assertEqual(meta["pageCount"], 2) |
| 46 | self.assertTrue((Path(tmp) / "catalog/page-0001.json").exists()) |
| 47 | self.assertTrue((Path(tmp) / "search-index.json").exists()) |
| 48 | import json |
| 49 | payload = json.loads((Path(tmp) / "catalog/page-0001.json").read_text(encoding="utf-8")) |
| 50 | cover = payload["novels"][0]["cover"] |
| 51 | self.assertTrue(cover and cover.startswith("covers/") and (cover.endswith(".png") or cover.endswith(".svg"))) |
| 52 | first = (Path(tmp) / cover).read_bytes() |
| 53 | self.assertTrue(first.startswith(b"\x89PNG") or first.startswith(b"<svg")) |
| 54 | write_index(novels, Path(tmp), page_size=2) |
| 55 | self.assertEqual((Path(tmp) / cover).read_bytes(), first) |
| 56 | |
| 57 | |
| 58 | if __name__ == "__main__": |
| 59 | unittest.main() |