| 319 | |
| 320 | |
| 321 | class TestFromFilePaths: |
| 322 | def test_toon_files(self, tmp_path): |
| 323 | if not from_file.toon_available: |
| 324 | pytest.skip("toon_format is not installed") |
| 325 | good = tmp_path / "data.toon" |
| 326 | Box(a=1, b="x").to_toon(filename=good) |
| 327 | assert box_from_file(good).to_dict() == {"a": 1, "b": "x"} |
| 328 | rows = tmp_path / "rows.toon" |
| 329 | BoxList([{"a": 1}, {"a": 2}]).to_toon(filename=rows) |
| 330 | assert box_from_file(rows).to_list() == [{"a": 1}, {"a": 2}] |
| 331 | bad = tmp_path / "bad.toon" |
| 332 | bad.write_text('a: "unterminated\n') |
| 333 | with pytest.raises(BoxError, match="not TOON"): |
| 334 | box_from_file(bad) |
| 335 | |
| 336 | def test_toon_unavailable(self, tmp_path, monkeypatch): |
| 337 | monkeypatch.setattr(from_file, "toon_available", False) |
| 338 | path = tmp_path / "data.toon" |
| 339 | path.write_text("a: 1\n") |
| 340 | with pytest.raises(BoxError, match="toon"): |
| 341 | box_from_file(path) |
| 342 | with pytest.raises(BoxError, match="toon"): |
| 343 | box_from_string("a: 1", string_type="toon") |
| 344 | |
| 345 | def test_box_from_string_yaml_unavailable(self, monkeypatch): |
| 346 | # What from_file and Box look like without ruamel.yaml or PyYAML installed |
| 347 | def unavailable(cls, *args, **kwargs): |
| 348 | raise BoxError("yaml is unavailable on this system") |
| 349 | |
| 350 | monkeypatch.setattr(from_file, "yaml_available", False) |
| 351 | monkeypatch.setattr(from_file, "YAMLError", False) |
| 352 | monkeypatch.setattr(Box, "from_yaml", classmethod(unavailable)) |
| 353 | monkeypatch.setattr(BoxList, "from_yaml", classmethod(unavailable)) |
| 354 | with pytest.raises(BoxError, match="yaml is unavailable"): |
| 355 | box_from_string("a: 1", string_type="yaml") |
| 356 | with pytest.raises(BoxError, match="yaml is unavailable"): |
| 357 | box_from_string("- 1\n- 2\n", string_type="yaml") |
| 358 | |
| 359 | def test_box_from_string_json(self): |
| 360 | assert box_from_string('{"a": 1}').to_dict() == {"a": 1} |
| 361 | assert box_from_string("[1, 2]").to_list() == [1, 2] |
| 362 | with pytest.raises(BoxError, match="not JSON"): |
| 363 | box_from_string("{nope") |
| 364 | |
| 365 | def test_box_from_string_toml(self): |
| 366 | assert box_from_string("a = 1", string_type="toml").to_dict() == {"a": 1} |
| 367 | with pytest.raises(BoxError, match="not TOML"): |
| 368 | box_from_string("= nope", string_type="toml") |
| 369 | |
| 370 | def test_box_from_string_yaml(self): |
| 371 | assert box_from_string("a: 1", string_type="yaml").to_dict() == {"a": 1} |
| 372 | assert box_from_string("- 1\n- 2\n", string_type="yaml").to_list() == [1, 2] |
| 373 | with pytest.raises(BoxError, match="not YAML"): |
| 374 | box_from_string("a: [1", string_type="yaml") |
| 375 | |
| 376 | def test_box_from_string_toon(self): |
| 377 | if not from_file.toon_available: |
| 378 | pytest.skip("toon_format is not installed") |
nothing calls this directly
no outgoing calls
no test coverage detected