|
1 | 1 | """Tests for common functionality.""" |
2 | 2 |
|
| 3 | +from pathlib import Path |
| 4 | + |
3 | 5 | import pytest |
4 | 6 |
|
5 | | -from pdfbaker.common import deep_merge, resolve_config |
| 7 | +from pdfbaker.config import PDFBakerConfiguration, deep_merge |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def test_deep_merge_basic(): |
@@ -99,84 +101,43 @@ def test_deep_merge_empty(): |
99 | 101 | assert deep_merge(update, base) == base |
100 | 102 |
|
101 | 103 |
|
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" |
113 | 108 |
|
114 | 109 |
|
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") |
128 | 114 |
|
| 115 | + config = PDFBakerConfiguration({}, config_file) |
| 116 | + assert config["title"] == "Document" |
| 117 | + assert config.directory == tmp_path |
129 | 118 |
|
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 |
145 | 119 |
|
| 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 |
146 | 125 |
|
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