Files
bindery/tests/test_merge.py
T
tim 6c2574cf0f Store chapters, merge chapter lists, diff and apply rules
The SQLite store keeps every chapter revision, so a chapter that the site
removes stays in the book. The merge puts new chapters after the chapter
that comes before them on the site. Diffs compare text, not HTML, and
replacement rules run on chapter HTML when a book is built.
2026-09-27 21:23:20 +02:00

56 lines
2.1 KiB
Python

from bindery.merge import merge_chapter_lists
def urls(*numbers):
return [f"c{n}" for n in numbers]
def test_stubbed_chapters_stay_and_new_chapter_goes_last():
existing = urls(*range(1, 61))
site = urls(1, 2, 3, 4, *range(46, 62))
assert merge_chapter_lists(existing, site) == urls(*range(1, 62))
def test_new_chapter_goes_after_its_site_predecessor():
assert merge_chapter_lists(urls(1, 2, 3), ["c1", "c2", "x", "c3"]) == ["c1", "c2", "x", "c3"]
def test_new_chapter_after_a_gap_goes_before_removed_chapters():
# The author replaced 2-3 with a new chapter x.
assert merge_chapter_lists(urls(1, 2, 3, 4), ["c1", "x", "c4"]) == ["c1", "x", "c2", "c3", "c4"]
def test_new_chapter_before_everything_goes_first():
assert merge_chapter_lists(urls(1, 2), ["p", "c1", "c2"]) == ["p", "c1", "c2"]
def test_failed_chapter_later_lands_in_order():
# c61 failed last time, c62 was added. The next run adds c61 before c62.
assert merge_chapter_lists(urls(59, 60, 62), urls(59, 60, 61, 62)) == urls(59, 60, 61, 62)
def test_existing_order_wins_and_duplicates_are_dropped():
# The site moved b before a. Our order stays, and c is newer than every chapter we have.
assert merge_chapter_lists(["a", "b", "b"], ["b", "a", "a", "c"]) == ["a", "b", "c"]
def test_new_chapter_after_a_stubbed_tail_goes_last():
# The site kept 1-5, removed 6-10 and added 11 and 12.
assert merge_chapter_lists(urls(*range(1, 11)), urls(1, 2, 3, 4, 5, 11, 12)) == urls(*range(1, 13))
def test_new_chapters_after_every_old_chapter_is_gone_go_last():
assert merge_chapter_lists(urls(1, 2, 3), urls(7, 8)) == urls(1, 2, 3, 7, 8)
def test_empty_lists():
assert merge_chapter_lists([], urls(1, 2)) == urls(1, 2)
assert merge_chapter_lists(urls(1), []) == urls(1)
def test_older_copy_fills_the_gap_before_newer_chapters():
existing = urls(*range(1, 6), *range(50, 61))
epub = urls(*range(1, 46))
assert merge_chapter_lists(existing, epub, newest_last=False) == urls(*range(1, 46), *range(50, 61))
assert merge_chapter_lists(urls(1, 2), urls(1, 2, 3), newest_last=False) == urls(1, 2, 3)