MCPcopy Create free account
hub / github.com/cdgriffith/Box / TestConverterPaths

Class TestConverterPaths

test/test_coverage_paths.py:265–318  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

263
264
265class TestConverterPaths:
266 @pytest.fixture
267 def pyyaml_only(self, monkeypatch):
268 if not converters.pyyaml_available:
269 pytest.skip("PyYAML is not installed")
270 monkeypatch.setattr(converters, "ruamel_available", False)
271
272 @pytest.fixture
273 def no_yaml(self, monkeypatch):
274 monkeypatch.setattr(converters, "ruamel_available", False)
275 monkeypatch.setattr(converters, "pyyaml_available", False)
276
277 def test_no_yaml_library(self, no_yaml, tmp_path):
278 path = tmp_path / "a.yaml"
279 path.write_text("a: 1\n")
280 with pytest.raises(BoxError, match="No YAML Parser"):
281 converters._to_yaml({"a": 1})
282 with pytest.raises(BoxError, match="No YAML Parser"):
283 converters._from_yaml("a: 1")
284 with pytest.raises(BoxError, match="No YAML Parser"):
285 converters._from_yaml(filename=path)
286
287 def test_pyyaml_file_and_loader(self, pyyaml_only, tmp_path):
288 import yaml
289
290 path = tmp_path / "a.yaml"
291 path.write_text("a: 1\nb: [1, 2]\n")
292 assert Box.from_yaml(filename=path).to_dict() == {"a": 1, "b": [1, 2]}
293 assert Box.from_yaml(filename=path, Loader=yaml.SafeLoader).a == 1
294 assert Box.from_yaml("a: 1\n", Loader=yaml.SafeLoader).a == 1
295
296 def test_ruamel_attrs_from_file(self, tmp_path):
297 if not converters.ruamel_available:
298 pytest.skip("ruamel.yaml is not installed")
299 path = tmp_path / "a.yaml"
300 path.write_text("a: 'x'\n")
301 bx = Box.from_yaml(filename=path, ruamel_attrs={"preserve_quotes": True})
302 assert bx.to_yaml() == "a: 'x'\n"
303
304 def test_ruamel_dumper_without_sort_attribute(self):
305 if not converters.ruamel_available:
306 pytest.skip("ruamel.yaml is not installed")
307 assert "a" in converters._to_yaml({"a": 1}, ruamel_typ="base")
308
309 def test_toml_package_writer(self, monkeypatch, tmp_path):
310 written = []
311 fake_toml = types.ModuleType("toml")
312 fake_toml.dump = lambda obj, f: written.append(obj) or f.write("a = 1\n")
313 fake_toml.dumps = lambda obj: "a = 1\n"
314 monkeypatch.setattr(converters, "toml_write_library", fake_toml)
315 path = tmp_path / "out.toml"
316 converters._to_toml({"a": 1}, filename=path)
317 assert path.read_text() == "a = 1\n"
318 assert written == [{"a": 1}]
319
320
321class TestFromFilePaths:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected