Skip to content

Commit 821cefb

Browse files
committed
Update tests - test build of all examples
1 parent 93c59de commit 821cefb

3 files changed

Lines changed: 75 additions & 76 deletions

File tree

tests/examples.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
documents:
2+
- minimal
3+
- regular
4+
- variants
5+
- "./custom_locations/your_directory"
6+
- custom_processing

tests/test_baker.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import shutil
2+
from pathlib import Path
3+
from pdfbaker.baker import PDFBaker
4+
5+
6+
def test_examples() -> None:
7+
"""Test all examples."""
8+
examples_dir = Path(__file__).parent.parent / "examples"
9+
test_dir = Path(__file__).parent
10+
11+
# Create build and dist directories
12+
build_dir = test_dir / "build"
13+
dist_dir = test_dir / "dist"
14+
build_dir.mkdir(exist_ok=True)
15+
dist_dir.mkdir(exist_ok=True)
16+
17+
# Copy and modify examples config
18+
config = examples_dir / "examples.yaml"
19+
test_config = test_dir / "examples.yaml"
20+
shutil.copy(config, test_config)
21+
22+
# Modify paths in config
23+
with open(test_config) as f:
24+
content = f.read()
25+
content = content.replace("build_dir: build", f"build_dir: {build_dir}")
26+
content = content.replace("dist_dir: dist", f"dist_dir: {dist_dir}")
27+
with open(test_config, "w") as f:
28+
f.write(content)
29+
30+
# Run baker
31+
baker = PDFBaker(test_config, quiet=True, keep_build=True)
32+
baker.bake()
Lines changed: 37 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
"""Tests for common functionality."""
22

3+
from pathlib import Path
4+
35
import pytest
46

5-
from pdfbaker.common import deep_merge, resolve_config
7+
from pdfbaker.config import PDFBakerConfiguration, deep_merge
68

79

810
def test_deep_merge_basic():
@@ -99,84 +101,43 @@ def test_deep_merge_empty():
99101
assert deep_merge(update, base) == base
100102

101103

102-
def test_resolve_config_basic():
103-
"""Test basic template resolution."""
104-
config = {
105-
"name": "test",
106-
"title": "{{ name }} document",
107-
}
108-
expected = {
109-
"name": "test",
110-
"title": "test document",
111-
}
112-
assert resolve_config(config) == expected
104+
def test_configuration_init_with_dict():
105+
"""Test initializing Configuration with a dictionary."""
106+
config = PDFBakerConfiguration({}, {"title": "Document"})
107+
assert config["title"] == "Document"
113108

114109

115-
def test_resolve_config_multiple_passes():
116-
"""Test config that needs multiple passes to resolve."""
117-
config = {
118-
"name": "test",
119-
"title": "{{ name }} document",
120-
"filename": "{{ title }}.pdf",
121-
}
122-
expected = {
123-
"name": "test",
124-
"title": "test document",
125-
"filename": "test document.pdf",
126-
}
127-
assert resolve_config(config) == expected
110+
def test_configuration_init_with_path(tmp_path):
111+
"""Test initializing Configuration with a file path."""
112+
config_file = tmp_path / "test.yaml"
113+
config_file.write_text("title: Document")
128114

115+
config = PDFBakerConfiguration({}, config_file)
116+
assert config["title"] == "Document"
117+
assert config.directory == tmp_path
129118

130-
def test_resolve_config_diamond_reference():
131-
"""Test diamond-shaped reference pattern."""
132-
config = {
133-
"name": "test",
134-
"title": "{{ name }} document",
135-
"subtitle": "{{ name }} details",
136-
"header": "{{ title }} - {{ subtitle }}",
137-
}
138-
expected = {
139-
"name": "test",
140-
"title": "test document",
141-
"subtitle": "test details",
142-
"header": "test document - test details",
143-
}
144-
assert resolve_config(config) == expected
145119

120+
def test_configuration_init_with_directory(tmp_path):
121+
"""Test initializing Configuration with custom directory."""
122+
config = PDFBakerConfiguration({}, {"title": "Document"}, directory=tmp_path)
123+
assert config["title"] == "Document"
124+
assert config.directory == tmp_path
146125

147-
def test_resolve_config_circular():
148-
"""Test circular reference handling."""
149-
config = {
150-
"a": "{{ b }}",
151-
"b": "{{ c }}",
152-
"c": "{{ a }}",
153-
}
154-
with pytest.raises(ValueError, match="Maximum number of iterations reached"):
155-
resolve_config(config)
156-
157-
158-
def test_resolve_config_nested():
159-
"""Test resolution in nested structures."""
160-
config = {
161-
"name": "test",
162-
"sections": [
163-
{"title": "{{ name }} section 1"},
164-
{"title": "{{ name }} section 2"},
165-
],
166-
"meta": {
167-
"title": "{{ name }} document",
168-
"description": "About {{ meta.title }}",
169-
},
170-
}
171-
expected = {
172-
"name": "test",
173-
"sections": [
174-
{"title": "test section 1"},
175-
{"title": "test section 2"},
176-
],
177-
"meta": {
178-
"title": "test document",
179-
"description": "About test document",
180-
},
181-
}
182-
assert resolve_config(config) == expected
126+
127+
def test_configuration_resolve_path():
128+
"""Test path resolution."""
129+
config = PDFBakerConfiguration(
130+
{}, {"template": "test.yaml"}, directory=Path("/base")
131+
)
132+
assert config.resolve_path("test.yaml") == Path("/base/test.yaml")
133+
assert config.resolve_path({"path": "/absolute/path.yaml"}) == Path(
134+
"/absolute/path.yaml"
135+
)
136+
assert config.resolve_path({"name": "test.yaml"}) == Path("/base/test.yaml")
137+
138+
139+
def test_configuration_resolve_path_invalid():
140+
"""Test invalid path specification."""
141+
config = PDFBakerConfiguration({}, {})
142+
with pytest.raises(ValueError, match="Invalid path specification"):
143+
config.resolve_path({})

0 commit comments

Comments
 (0)