BlueGit

timfa hfy-novels

Files

hfy-novels / indexer / tests / test_chain.py

test_chain.py

test_chain.py 2.8 KiB Download Raw
1import unittest
2
3from indexer.chain import follow_chain, novel_from_wiki
4from indexer.cluster import Post
5from indexer.links import Link
6from indexer.similarity import LexicalEmbedder
7from indexer.wiki import WikiSeries
8
9
10def _post(pid, title, author="WriterOne", nxt=None, created=1):
11 next_ids = tuple(nxt or ())
12 links = tuple(Link(i, "Next", "next") for i in next_ids)
13 return Post(
14 id=pid,
15 title=title,
16 author=author,
17 created_utc=created,
18 permalink=f"/r/HFY/comments/{pid}/",
19 flair="OC-Series",
20 next_ids=next_ids,
21 links=links,
22 )
23
24
25class ChainTests(unittest.TestCase):
26 def test_follows_next_with_similar_titles(self):
27 posts = {
28 "a1": _post("a1", "The Last Terran Part 1", nxt=["a2"], created=1),
29 "a2": _post("a2", "The Last Terran Part 2", nxt=["a3"], created=2),
30 "a3": _post("a3", "The Last Terran Part 3", created=3),
31 }
32 chain = follow_chain(posts["a1"], posts, embedder=LexicalEmbedder())
33 self.assertEqual([p.id for p in chain], ["a1", "a2", "a3"])
34
35 def test_skips_unrelated_next_link(self):
36 posts = {
37 "a1": _post("a1", "The Last Terran Part 1", nxt=["zzz"], created=1),
38 "zzz": _post("zzz", "Completely Different Cookbook", author="SomeoneElse", created=2),
39 }
40 chain = follow_chain(posts["a1"], posts, embedder=LexicalEmbedder())
41 self.assertEqual([p.id for p in chain], ["a1"])
42
43 def test_fetcher_fills_missing_posts(self):
44 seed = _post("a1", "Terra Part 1", nxt=["a2"], created=1)
45 hidden = _post("a2", "Terra Part 2", created=2)
46 by_id = {seed.id: seed}
47
48 def fetch(ids):
49 return [hidden] if "a2" in ids else []
50
51 chain = follow_chain(seed, by_id, embedder=LexicalEmbedder(), fetcher=fetch)
52 self.assertEqual([p.id for p in chain], ["a1", "a2"])
53 self.assertIn("a2", by_id)
54
55 def test_wiki_toc_then_follow_tail(self):
56 by_id = {
57 "a1": _post("a1", "Terra Part 1", nxt=["a2"], created=1),
58 "a2": _post("a2", "Terra Part 2", nxt=["a3"], created=2),
59 "a3": _post("a3", "Terra Part 3", created=3),
60 }
61 entry = WikiSeries(
62 title="Terra",
63 wiki_path="/r/hfy/wiki/series/terra",
64 author="WriterOne",
65 chapter_ids=("a1", "a2"),
66 first_id="a1",
67 page_found=True,
68 )
69 novel = novel_from_wiki(entry, by_id, embedder=LexicalEmbedder())
70 self.assertIsNotNone(novel)
71 self.assertEqual(novel.name, "Terra")
72 self.assertEqual([c.id for c in novel.chapters], ["a1", "a2", "a3"])
73 self.assertEqual(novel.wiki_path, "/r/hfy/wiki/series/terra")
74
75
76if __name__ == "__main__":
77 unittest.main()