BlueGit

timfa hfy-novels

Files

hfy-novels / indexer / tests / test_links.py

test_links.py

test_links.py 1.5 KiB Download Raw
1import unittest
2
3from indexer.links import extract_labeled_links, extract_links, extract_post_id
4
5
6class LinkTests(unittest.TestCase):
7 def test_extracts_variants(self):
8 text = """
9 [Next](https://www.reddit.com/r/HFY/comments/aaa222/the_last_terran_part_2/)
10 [Prev](https://redd.it/aaa111)
11 See also /r/HFY/comments/abcd123/something/ and <a href="/comments/ffff001/x">first</a>
12 """
13 links = extract_links(text)
14 self.assertEqual(extract_post_id("https://old.reddit.com/r/HFY/comments/aaa222/slug/"), "aaa222")
15 self.assertIn("aaa222", links.next_ids)
16 self.assertIn("aaa111", links.prev_ids)
17 self.assertIn("ffff001", links.first_ids)
18 self.assertIn("abcd123", links.all_ids)
19
20 def test_empty(self):
21 links = extract_links("")
22 self.assertEqual(links.all_ids, ())
23
24 def test_labeled_links_keep_document_order(self):
25 text = """
26 [Prev](https://redd.it/aaa111)
27 See [this chapter](https://www.reddit.com/r/HFY/comments/abcd123/something/)
28 [Next](https://www.reddit.com/r/HFY/comments/aaa222/the_last_terran_part_2/)
29 """
30 labeled = extract_labeled_links(text)
31 self.assertEqual([ln.id for ln in labeled], ["aaa111", "abcd123", "aaa222"])
32 self.assertEqual([ln.kind for ln in labeled], ["prev", "other", "next"])
33 self.assertEqual(extract_links(text).links, tuple(labeled))
34
35
36if __name__ == "__main__":
37 unittest.main()