|
| 1 | +import bz2 |
1 | 2 | import io |
| 3 | +import json |
2 | 4 | import zipfile |
3 | 5 | from unittest import mock |
4 | 6 |
|
|
7 | 9 | from mandible.metadata_mapper.format import ( |
8 | 10 | FORMAT_REGISTRY, |
9 | 11 | H5, |
| 12 | + Bzip2File, |
10 | 13 | Format, |
11 | 14 | FormatError, |
12 | 15 | Json, |
|
24 | 27 |
|
25 | 28 | def test_registry(): |
26 | 29 | assert FORMAT_REGISTRY == { |
| 30 | + "Bzip2File": Bzip2File, |
27 | 31 | "H5": H5, |
28 | 32 | "Json": Json, |
29 | 33 | "Xml": Xml, |
@@ -94,6 +98,47 @@ def test_h5_empty_key(): |
94 | 98 | format.get_value(file, Key("")) |
95 | 99 |
|
96 | 100 |
|
| 101 | +@pytest.mark.h5 |
| 102 | +def test_h5_attribute(): |
| 103 | + file = io.BytesIO() |
| 104 | + with h5py.File(file, "w") as f: |
| 105 | + f["foo"] = "foo value" |
| 106 | + f["bar"] = "bar value" |
| 107 | + f["list"] = ["list", "value"] |
| 108 | + f["foo@bar"] = "foo@bar value" |
| 109 | + new_group = f.create_group("foo_with_attribute") |
| 110 | + new_group.attrs["value"] = "foo_with_attribute value" |
| 111 | + new_group.attrs["foo@bar"] = "foo_with_attribute @bar value" |
| 112 | + new_group_with_at = f.create_group("bar@foo") |
| 113 | + new_group_with_at.attrs["attr@ibute"] = "testing_attribute@_group@" |
| 114 | + |
| 115 | + format = H5() |
| 116 | + |
| 117 | + assert format.get_values( |
| 118 | + file, |
| 119 | + [ |
| 120 | + Key("/foo"), |
| 121 | + Key("bar"), |
| 122 | + Key("list"), |
| 123 | + Key("foo@@bar"), |
| 124 | + Key("foo_with_attribute@value"), |
| 125 | + Key("foo_with_attribute@foo@@bar"), |
| 126 | + Key("bar@@foo@attr@@ibute"), |
| 127 | + ], |
| 128 | + ) == { |
| 129 | + Key("/foo"): "foo value", |
| 130 | + Key("bar"): "bar value", |
| 131 | + Key("list"): ["list", "value"], |
| 132 | + Key("foo@@bar"): "foo@bar value", |
| 133 | + Key("foo_with_attribute@value"): "foo_with_attribute value", |
| 134 | + Key("foo_with_attribute@foo@@bar"): "foo_with_attribute @bar value", |
| 135 | + Key("bar@@foo@attr@@ibute"): "testing_attribute@_group@", |
| 136 | + } |
| 137 | + |
| 138 | + with pytest.raises(FormatError, match="Invalid key: multiple '@'"): |
| 139 | + format.get_values(file, [Key("test@test@test")]) |
| 140 | + |
| 141 | + |
97 | 142 | @pytest.mark.h5 |
98 | 143 | def test_h5_key_error(): |
99 | 144 | file = io.BytesIO() |
@@ -430,3 +475,49 @@ def test_xml_key_error(): |
430 | 475 |
|
431 | 476 | with pytest.raises(FormatError, match="key not found 'foo'"): |
432 | 477 | format.get_values(file, [Key("foo")]) |
| 478 | + |
| 479 | + |
| 480 | +@pytest.mark.h5 |
| 481 | +def test_bzip2_h5py(): |
| 482 | + h5_buffer = io.BytesIO() |
| 483 | + with h5py.File(h5_buffer, "w") as f: |
| 484 | + f["foo"] = "foo value" |
| 485 | + f["bar"] = "bar value" |
| 486 | + f["list"] = ["list", "value"] |
| 487 | + |
| 488 | + bz2_compressed_file = io.BytesIO(bz2.compress(h5_buffer.getvalue())) |
| 489 | + format = Bzip2File(format=H5()) |
| 490 | + |
| 491 | + assert format.get_value(bz2_compressed_file, Key("foo")) == "foo value" |
| 492 | + bz2_compressed_file.seek(0) |
| 493 | + assert format.get_value(bz2_compressed_file, Key("bar")) == "bar value" |
| 494 | + bz2_compressed_file.seek(0) |
| 495 | + assert format.get_value(bz2_compressed_file, Key("list")) == ["list", "value"] |
| 496 | + bz2_compressed_file.seek(0) |
| 497 | + assert format.get_values(bz2_compressed_file, [Key("foo"), Key("bar")]) == { |
| 498 | + Key("foo"): "foo value", |
| 499 | + Key("bar"): "bar value", |
| 500 | + } |
| 501 | + |
| 502 | + |
| 503 | +def test_bzip2_json(): |
| 504 | + json_bytes = json.dumps( |
| 505 | + { |
| 506 | + "foo": "foo value", |
| 507 | + "bar": "bar value", |
| 508 | + }, |
| 509 | + ).encode("utf-8") |
| 510 | + |
| 511 | + bz2_compressed_file = io.BytesIO(bz2.compress(json_bytes)) |
| 512 | + format = Bzip2File(format=Json()) |
| 513 | + |
| 514 | + assert format.get_value(bz2_compressed_file, Key("$.foo")) == "foo value" |
| 515 | + bz2_compressed_file.seek(0) |
| 516 | + assert format.get_values(bz2_compressed_file, [Key("$.foo")]) == { |
| 517 | + Key("$.foo"): "foo value", |
| 518 | + } |
| 519 | + bz2_compressed_file.seek(0) |
| 520 | + assert format.get_value(bz2_compressed_file, Key("$")) == { |
| 521 | + "bar": "bar value", |
| 522 | + "foo": "foo value", |
| 523 | + } |
0 commit comments