1- """Tests for common functionality."""
1+ """Tests for configuration functionality."""
22
33from pathlib import Path
44
55import pytest
6+ import yaml
67
7- from pdfbaker .config import PDFBakerConfiguration , deep_merge
8+ from pdfbaker .config import PDFBakerConfiguration , deep_merge , render_config
9+ from pdfbaker .errors import ConfigurationError
810
911
10- def test_deep_merge_basic ():
12+ # Dictionary merging tests
13+ def test_deep_merge_basic () -> None :
1114 """Test basic dictionary merging."""
1215 base = {
1316 "title" : "Document" ,
@@ -34,8 +37,8 @@ def test_deep_merge_basic():
3437 assert deep_merge (base , update ) == expected
3538
3639
37- def test_deep_merge_nested ():
38- """Test merging of nested dictionaries ."""
40+ def test_deep_merge_nested () -> None :
41+ """Test nested dictionary merging ."""
3942 base = {
4043 "document" : {
4144 "title" : "Main Document" ,
@@ -85,7 +88,7 @@ def test_deep_merge_nested():
8588 assert deep_merge (base , update ) == expected
8689
8790
88- def test_deep_merge_empty ():
91+ def test_deep_merge_empty () -> None :
8992 """Test merging with empty dictionaries."""
9093 base = {
9194 "title" : "Document" ,
@@ -101,44 +104,116 @@ def test_deep_merge_empty():
101104 assert deep_merge (update , base ) == base
102105
103106
104- def test_configuration_init_with_dict ():
107+ # Configuration initialization tests
108+ def test_configuration_init_with_dict (tmp_path : Path ) -> None :
105109 """Test initializing Configuration with a dictionary."""
106- config = PDFBakerConfiguration ({}, {"title" : "Document" })
110+ config_file = tmp_path / "test.yaml"
111+ config_file .write_text (yaml .dump ({"title" : "Document" }))
112+
113+ config = PDFBakerConfiguration ({}, config_file )
107114 assert config ["title" ] == "Document"
108115
109116
110- def test_configuration_init_with_path (tmp_path ) :
117+ def test_configuration_init_with_path (tmp_path : Path ) -> None :
111118 """Test initializing Configuration with a file path."""
112119 config_file = tmp_path / "test.yaml"
113- config_file .write_text ("title: Document" )
120+ config_file .write_text (yaml . dump ({ "title" : " Document"}) )
114121
115122 config = PDFBakerConfiguration ({}, config_file )
116123 assert config ["title" ] == "Document"
117- assert config . directory == tmp_path
124+ assert config [ "directories" ][ "config" ] == tmp_path
118125
119126
120- def test_configuration_init_with_directory (tmp_path ) :
127+ def test_configuration_init_with_directory (tmp_path : Path ) -> None :
121128 """Test initializing Configuration with custom directory."""
122129 config_file = tmp_path / "test.yaml"
123- config_file .write_text ('{"title": "Document"}' )
130+ config_file .write_text (yaml .dump ({"title" : "Document" }))
131+
124132 config = PDFBakerConfiguration ({}, config_file )
125133 assert config ["title" ] == "Document"
126- assert config . directory == tmp_path
134+ assert config [ "directories" ][ "config" ] == tmp_path
127135
128136
129- def test_configuration_resolve_path ():
137+ def test_configuration_init_invalid_yaml (tmp_path : Path ) -> None :
138+ """Test configuration with invalid YAML."""
139+ config_file = tmp_path / "invalid.yaml"
140+ config_file .write_text ("invalid: [yaml: content" )
141+
142+ with pytest .raises (ConfigurationError , match = "Failed to load config file" ):
143+ PDFBakerConfiguration ({}, config_file )
144+
145+
146+ # Path resolution tests
147+ def test_configuration_resolve_path (tmp_path : Path ) -> None :
130148 """Test path resolution."""
131- config = PDFBakerConfiguration ({}, {"template" : "test.yaml" })
132- config .directory = Path ("/base" ) # Set directory explicitly for testing
133- assert config .resolve_path ("test.yaml" ) == Path ("/base/test.yaml" )
149+ config_file = tmp_path / "test.yaml"
150+ config_file .write_text (yaml .dump ({"template" : "test.yaml" }))
151+
152+ config = PDFBakerConfiguration ({}, config_file )
153+
154+ # Test relative path
155+ assert config .resolve_path ("test.yaml" ) == tmp_path / "test.yaml"
156+
157+ # Test absolute path
134158 assert config .resolve_path ({"path" : "/absolute/path.yaml" }) == Path (
135159 "/absolute/path.yaml"
136160 )
137- assert config .resolve_path ({"name" : "test.yaml" }) == Path ("/base/test.yaml" )
138161
162+ # Test named path
163+ assert config .resolve_path ({"name" : "test.yaml" }) == tmp_path / "test.yaml"
139164
140- def test_configuration_resolve_path_invalid ():
165+
166+ def test_configuration_resolve_path_invalid (tmp_path : Path ) -> None :
141167 """Test invalid path specification."""
142- config = PDFBakerConfiguration ({}, {})
143- with pytest .raises (ValueError , match = "Invalid path specification" ):
168+ config_file = tmp_path / "test.yaml"
169+ config_file .write_text (yaml .dump ({}))
170+
171+ config = PDFBakerConfiguration ({}, config_file )
172+ with pytest .raises (ConfigurationError , match = "Invalid path specification" ):
144173 config .resolve_path ({})
174+
175+
176+ # Configuration rendering tests
177+ def test_render_config_basic () -> None :
178+ """Test basic template rendering in configuration."""
179+ config = {
180+ "name" : "test" ,
181+ "title" : "{{ name }} document" ,
182+ "nested" : {
183+ "value" : "{{ title }}" ,
184+ },
185+ }
186+
187+ rendered = render_config (config )
188+ assert rendered ["title" ] == "test document"
189+ assert rendered ["nested" ]["value" ] == "test document"
190+
191+
192+ def test_render_config_circular () -> None :
193+ """Test detection of circular references in config rendering."""
194+ config = {
195+ "a" : "{{ b }}" ,
196+ "b" : "{{ a }}" ,
197+ }
198+
199+ with pytest .raises (ConfigurationError , match = "(?i).*circular.*" ):
200+ render_config (config )
201+
202+
203+ # Utility method tests
204+ def test_configuration_pretty (tmp_path : Path ) -> None :
205+ """Test configuration pretty printing."""
206+ config_file = tmp_path / "test.yaml"
207+ config_file .write_text (
208+ yaml .dump (
209+ {
210+ "title" : "Test" ,
211+ "content" : "A" * 100 , # Long string that should be truncated
212+ }
213+ )
214+ )
215+
216+ config = PDFBakerConfiguration ({}, config_file )
217+ pretty = config .pretty (max_chars = 20 )
218+ assert "…" in pretty # Should show truncation
219+ assert "Test" in pretty
0 commit comments