| 1 | import json |
| 2 | import subprocess |
| 3 | import sys |
| 4 | import tempfile |
| 5 | import unittest |
| 6 | from pathlib import Path |
| 7 | |
| 8 | ROOT = Path(__file__).resolve().parents[2] |
| 9 | |
| 10 | |
| 11 | class BuildCliTests(unittest.TestCase): |
| 12 | def test_fixture_build(self): |
| 13 | with tempfile.TemporaryDirectory() as tmp: |
| 14 | dest = Path(tmp) / "public" |
| 15 | proc = subprocess.run( |
| 16 | [ |
| 17 | sys.executable, |
| 18 | str(ROOT / "indexer" / "build_index.py"), |
| 19 | "--from-fixture", |
| 20 | str(ROOT / "testdata" / "posts.json"), |
| 21 | "--dest", |
| 22 | str(dest), |
| 23 | "--full", |
| 24 | ], |
| 25 | check=True, |
| 26 | cwd=ROOT, |
| 27 | capture_output=True, |
| 28 | text=True, |
| 29 | ) |
| 30 | self.assertIn("3 verhalen", proc.stdout) |
| 31 | meta = json.loads((dest / "catalog-meta.json").read_text(encoding="utf-8")) |
| 32 | self.assertEqual(meta["novelCount"], 3) |
| 33 | self.assertEqual(meta["chapterCount"], 6) |
| 34 | state = json.loads((dest / "state.json").read_text(encoding="utf-8")) |
| 35 | self.assertEqual(state["novelCount"], 3) |
| 36 | |
| 37 | def test_wiki_fixture_build(self): |
| 38 | with tempfile.TemporaryDirectory() as tmp: |
| 39 | dest = Path(tmp) / "public" |
| 40 | proc = subprocess.run( |
| 41 | [ |
| 42 | sys.executable, |
| 43 | str(ROOT / "indexer" / "build_index.py"), |
| 44 | "--from-wiki-fixture", |
| 45 | str(ROOT / "testdata" / "wiki"), |
| 46 | "--from-fixture", |
| 47 | str(ROOT / "testdata" / "posts.json"), |
| 48 | "--dest", |
| 49 | str(dest), |
| 50 | "--full", |
| 51 | ], |
| 52 | check=True, |
| 53 | cwd=ROOT, |
| 54 | capture_output=True, |
| 55 | text=True, |
| 56 | ) |
| 57 | self.assertIn("3 verhalen", proc.stdout) |
| 58 | meta = json.loads((dest / "catalog-meta.json").read_text(encoding="utf-8")) |
| 59 | self.assertEqual(meta["novelCount"], 3) |
| 60 | self.assertIn("wiki", meta["source"]) |
| 61 | terran = json.loads((dest / "series" / "a" / "aaa111.json").read_text(encoding="utf-8")) |
| 62 | self.assertEqual(terran["name"], "The Last Terran") |
| 63 | self.assertEqual(terran["author"], "WriterOne") |
| 64 | self.assertEqual(terran["wikiPath"], "/r/hfy/wiki/series/the_last_terran") |
| 65 | self.assertEqual([c["redditId"] for c in terran["chapterList"]], ["aaa111", "aaa222", "aaa333"]) |
| 66 | self.assertNotIn("It begins", json.dumps(terran)) |
| 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | unittest.main() |