| 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_json(self): |
| 346 | assert box_from_string('{"a": 1}').to_dict() == {"a": 1} |
| 347 | assert box_from_string("[1, 2]").to_list() == [1, 2] |
| 348 | with pytest.raises(BoxError, match="not JSON"): |
| 349 | box_from_string("{nope") |
| 350 | |
| 351 | def test_box_from_string_toml(self): |
| 352 | assert box_from_string("a = 1", string_type="toml").to_dict() == {"a": 1} |
| 353 | with pytest.raises(BoxError, match="not TOML"): |
| 354 | box_from_string("= nope", string_type="toml") |
| 355 | |
| 356 | def test_box_from_string_yaml(self): |
| 357 | assert box_from_string("a: 1", string_type="yaml").to_dict() == {"a": 1} |
| 358 | assert box_from_string("- 1\n- 2\n", string_type="yaml").to_list() == [1, 2] |
| 359 | with pytest.raises(BoxError, match="not YAML"): |
| 360 | box_from_string("a: [1", string_type="yaml") |
| 361 | |
| 362 | def test_box_from_string_toon(self): |
| 363 | if not from_file.toon_available: |
| 364 | pytest.skip("toon_format is not installed") |
| 365 | assert box_from_string(Box(a=1).to_toon(), string_type="toon").to_dict() == {"a": 1} |
| 366 | rows = BoxList([{"a": 1}, {"a": 2}]).to_toon() |
| 367 | assert box_from_string(rows, string_type="toon").to_list() == [{"a": 1}, {"a": 2}] |
| 368 | with pytest.raises(BoxError, match="not TOON"): |
| 369 | box_from_string('a: "unterminated', string_type="toon") |
| 370 | |
| 371 | def test_box_from_string_unknown_type(self): |
| 372 | with pytest.raises(BoxError, match="Unsupported"): |
| 373 | box_from_string("a", string_type="xml") |
nothing calls this directly
no outgoing calls
no test coverage detected