Skip to content

Commit 52fb81b

Browse files
committed
Add test for a document where pages are only defined in variants
1 parent 1d05a3a commit 52fb81b

1 file changed

Lines changed: 81 additions & 1 deletion

File tree

tests/test_document.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ def test_document_init_with_dir(
7979
config_path=doc_dir, # this will default to config.yaml in the directory
8080
)
8181
assert doc.config.name == "test_doc"
82+
assert doc.config["pages"] == ["page1.yaml"]
83+
84+
# We need to manually determine pages now
85+
doc.config.determine_pages(doc.config)
8286
assert len(doc.config.pages) > 0
8387
assert doc.config.pages[0].name == "page1.yaml"
8488

@@ -125,7 +129,7 @@ def test_document_init_missing_pages(tmp_path: Path, baker_config: Path) -> None
125129
""")
126130

127131
baker = PDFBaker(baker_config)
128-
with pytest.raises(ConfigurationError, match='missing key "pages"'):
132+
with pytest.raises(ConfigurationError, match="Cannot determine pages"):
129133
PDFBakerDocument(baker, baker.config, config_file)
130134

131135

@@ -213,3 +217,79 @@ def test_document_teardown(
213217
assert "Tearing down build directory" in caplog.text
214218
assert "Removing files in build directory" in caplog.text
215219
assert "Removing build directory" in caplog.text
220+
221+
222+
def test_document_variants_with_different_pages(
223+
tmp_path: Path, baker_config: Path, baker_options: PDFBakerOptions
224+
) -> None:
225+
"""Test document with variants where each variant has different pages."""
226+
# Create document config with variants but no pages
227+
config_file = tmp_path / "test_doc.yaml"
228+
config_file.write_text("""
229+
filename: "{{ variant.name }}_doc"
230+
directories:
231+
build: build
232+
dist: dist
233+
variants:
234+
- name: variant1
235+
filename: variant1
236+
pages: [page1.yaml]
237+
- name: variant2
238+
filename: variant2
239+
pages: [page2.yaml]
240+
""")
241+
242+
# Create page configs
243+
pages_dir = tmp_path / "pages"
244+
pages_dir.mkdir()
245+
246+
page_file1 = pages_dir / "page1.yaml"
247+
page_file1.write_text("""
248+
template: template.svg
249+
content: "Variant 1 content"
250+
""")
251+
252+
page_file2 = pages_dir / "page2.yaml"
253+
page_file2.write_text("""
254+
template: template.svg
255+
content: "Variant 2 content"
256+
""")
257+
258+
# Create template
259+
templates_dir = tmp_path / "templates"
260+
templates_dir.mkdir()
261+
template_file = templates_dir / "template.svg"
262+
template_file.write_text(
263+
'<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"></svg>'
264+
)
265+
266+
baker = PDFBaker(baker_config, options=baker_options)
267+
doc = PDFBakerDocument(baker, baker.config, config_file)
268+
269+
# Check that document initialization works without pages at doc level
270+
assert doc.config.name == "test_doc"
271+
assert "pages" not in doc.config
272+
assert len(doc.config["variants"]) == 2
273+
274+
# Check that each variant has its own pages definition
275+
assert doc.config["variants"][0]["pages"] == ["page1.yaml"]
276+
assert doc.config["variants"][1]["pages"] == ["page2.yaml"]
277+
278+
# Test that processing works with per-variant pages
279+
# We don't need to call process for this basic functionality test
280+
# as that would require inkapscape/cairosvg and be more integration testing
281+
282+
# Instead, test that we can determine pages from variant configs
283+
variant1_config = doc.config.copy()
284+
variant1_config.update(doc.config["variants"][0])
285+
assert "pages" in variant1_config
286+
doc.config.determine_pages(variant1_config)
287+
assert len(doc.config.pages) > 0
288+
assert doc.config.pages[0].name == "page1.yaml"
289+
290+
variant2_config = doc.config.copy()
291+
variant2_config.update(doc.config["variants"][1])
292+
assert "pages" in variant2_config
293+
doc.config.determine_pages(variant2_config)
294+
assert len(doc.config.pages) > 0
295+
assert doc.config.pages[0].name == "page2.yaml"

0 commit comments

Comments
 (0)