updated with functional changes, testing

This commit is contained in:
Rory Hinnen
2026-07-10 21:02:34 -05:00
parent 48b9dee6d6
commit c14a97331c
11 changed files with 931 additions and 381 deletions
+249
View File
@@ -0,0 +1,249 @@
import pytest
from epubmaker.models import BuildContext
from epubmaker.parser import extract, identify, parse_book
@pytest.fixture
def ctx():
return BuildContext()
def test_extract_returns_inner_content():
assert extract("<!-- Title: Foo Bar -->") == "Title: Foo Bar"
def test_extract_with_newline():
assert extract("<!-- Title: Foo Bar -->\n") == "Title: Foo Bar"
def test_extract_returns_none_for_non_comment():
assert extract("<p>Not a comment</p>") is None
def test_identify_directory(ctx):
identify("<!-- Directory: mybook -->", ctx)
assert ctx.book.directory == "mybook"
assert ctx.in_section is False
def test_identify_title_plain(ctx):
identify("<!-- Title: My Book -->", ctx)
assert ctx.book.title == "My Book"
assert ctx.book.titlesort == ""
def test_identify_title_the(ctx):
identify("<!-- Title: The Great Book -->", ctx)
assert ctx.book.title == "The Great Book"
assert ctx.book.titlesort == "Great Book, The"
def test_identify_title_a(ctx):
identify("<!-- Title: A Good Story -->", ctx)
assert ctx.book.title == "A Good Story"
assert ctx.book.titlesort == "Good Story, A"
def test_identify_title_an(ctx):
identify("<!-- Title: An Old Tale -->", ctx)
assert ctx.book.title == "An Old Tale"
assert ctx.book.titlesort == "Old Tale, An"
def test_identify_author(ctx):
identify("<!-- Author: Smith, John -->", ctx)
assert len(ctx.book.author) == 1
assert ctx.book.author[0].last == "Smith"
assert ctx.book.author[0].first == "John"
def test_identify_multiple_authors(ctx):
identify("<!-- Author: Smith, John -->", ctx)
identify("<!-- Author: Jones, Jane -->", ctx)
assert len(ctx.book.author) == 2
assert ctx.book.author[1].last == "Jones"
def test_identify_editor(ctx):
identify("<!-- Editor: Brown, Alice -->", ctx)
assert ctx.book.editor.last == "Brown"
assert ctx.book.editor.first == "Alice"
def test_identify_publisher(ctx):
identify("<!-- Publisher: Acme Press -->", ctx)
assert ctx.book.publisher == "Acme Press"
def test_identify_copyright(ctx):
identify("<!-- Copyright: 2024 Author -->", ctx)
assert ctx.book.copyright == "2024 Author"
def test_identify_isbn(ctx):
identify("<!-- ISBN: 978-0-000-00000-0 -->", ctx)
assert ctx.book.isbn == "978-0-000-00000-0"
def test_identify_uuid_explicit(ctx):
identify("<!-- UUID: abc-123 -->", ctx)
assert ctx.book.uuid == "abc-123"
def test_identify_uuid_empty_generates_uuid(ctx):
identify("<!-- UUID: -->", ctx)
assert ctx.book.uuid != ""
assert ctx.book.uuid is not None
def test_identify_source(ctx):
identify("<!-- Source: Some Original Work -->", ctx)
assert ctx.book.source == "Some Original Work"
def test_identify_subjects(ctx):
identify("<!-- Subjects: Fiction, Mystery -->", ctx)
assert ctx.book.subjects == "Fiction, Mystery"
def test_identify_series(ctx):
identify("<!-- Series: 1 My Series Name -->", ctx)
assert ctx.book.series.number == "1"
assert ctx.book.series.name == "My Series Name"
def test_identify_cover(ctx):
identify("<!-- Cover: cover.jpeg -->", ctx)
assert ctx.book.cover.name == "cover"
assert ctx.book.cover.type == "jpeg"
def test_identify_text_section(ctx):
identify("<!-- Text: chapter1 Chapter One -->", ctx)
assert ctx.in_section is True
assert len(ctx.book.sections) == 1
assert ctx.book.sections[0].type == "text"
assert ctx.book.sections[0].name == "chapter1"
assert ctx.book.sections[0].title == "Chapter One"
def test_identify_toc_section(ctx):
identify("<!-- TOC: toc Table of Contents -->", ctx)
assert ctx.in_section is True
assert ctx.book.sections[0].type == "toc"
def test_identify_title_page(ctx):
identify("<!-- TitlePage: tp Title Page -->", ctx)
assert ctx.book.sections[0].type == "title"
def test_identify_copyright_page(ctx):
identify("<!-- CopyrightPage: cp Copyright -->", ctx)
assert ctx.book.sections[0].type == "copyright-page"
def test_identify_dedication(ctx):
identify("<!-- Dedication: ded For someone -->", ctx)
assert ctx.book.sections[0].type == "dedication"
def test_identify_foreward(ctx):
identify("<!-- Foreward: fw Foreword -->", ctx)
assert ctx.book.sections[0].type == "forward"
def test_identify_notes(ctx):
identify("<!-- Notes: notes Notes -->", ctx)
assert ctx.book.sections[0].type == "notes"
def test_identify_acknowledgement(ctx):
identify("<!-- Acknowledgement: ack Acknowledgements -->", ctx)
assert ctx.book.sections[0].type == "acknowledgement"
def test_identify_metadata_clears_in_section(ctx):
identify("<!-- Text: ch1 Chapter One -->", ctx)
assert ctx.in_section is True
identify("<!-- Title: My Book -->", ctx)
assert ctx.in_section is False
def test_identify_image_jpeg(ctx):
identify("<!-- Text: ch1 Chapter One -->", ctx)
identify("<!-- Image: photo.jpg Alt text::Caption text -->", ctx)
assert len(ctx.book.images) == 1
assert ctx.book.images[0].name == "photo.jpg"
assert ctx.book.images[0].type == "jpeg"
assert ctx.book.images[0].alt == "Alt text"
assert ctx.book.images[0].caption == "Caption text"
def test_identify_image_png(ctx):
identify("<!-- Text: ch1 Chapter One -->", ctx)
identify("<!-- Image: photo.png Alt text::Caption -->", ctx)
assert ctx.book.images[0].type == "png"
def test_identify_image_appends_to_section_body(ctx):
identify("<!-- Text: ch1 Chapter One -->", ctx)
identify("<!-- Image: photo.jpg Alt::Caption -->", ctx)
assert "photo.jpg" in ctx.book.sections[-1].body
def test_identify_image_with_caption_uses_div(ctx):
identify("<!-- Text: ch1 Chapter One -->", ctx)
identify("<!-- Image: photo.jpg Alt::My Caption -->", ctx)
assert '<div class="center group">' in ctx.book.sections[-1].body
assert "My Caption" in ctx.book.sections[-1].body
def test_identify_image_without_caption_uses_img(ctx):
identify("<!-- Text: ch1 Chapter One -->", ctx)
identify("<!-- Image: photo.jpg Alt:: -->", ctx)
body = ctx.book.sections[-1].body
assert "<img" in body
assert '<div class="center group">' not in body
def test_identify_unknown_key_ignored(ctx):
identify("<!-- UnknownKey: some value -->", ctx)
assert ctx.book.title == ""
assert ctx.book.directory == ""
def test_parse_book_populates_book(tmp_path):
source = tmp_path / "book.html"
source.write_text(
"<!-- Directory: mybook -->\n"
"<!-- Title: Test Book -->\n"
"<!-- Author: Doe, Jane -->\n"
"<!-- Text: ch1 Chapter One -->\n"
"<p>Body content</p>\n"
)
ctx = BuildContext()
ctx.book.filename = str(source)
parse_book(ctx)
assert ctx.book.title == "Test Book"
assert ctx.book.directory == "mybook"
assert len(ctx.book.author) == 1
assert ctx.book.author[0].last == "Doe"
assert len(ctx.book.sections) == 1
assert "<p>Body content</p>\n" in ctx.book.sections[0].body
def test_parse_book_body_not_captured_outside_section(tmp_path):
source = tmp_path / "book.html"
source.write_text(
"<!-- Title: Test Book -->\n"
"<p>This is outside any section</p>\n"
"<!-- Text: ch1 Chapter One -->\n"
"<p>Inside section</p>\n"
)
ctx = BuildContext()
ctx.book.filename = str(source)
parse_book(ctx)
assert len(ctx.book.sections) == 1
assert "outside any section" not in ctx.book.sections[0].body
assert "Inside section" in ctx.book.sections[0].body