|
| 1 | +# Copyright (c) DataLab Platform Developers, BSD 3-Clause License |
| 2 | +# See LICENSE file for details |
| 3 | +"""Tests for the HDF5 browser backend (``dlw_h5browser``). |
| 4 | +
|
| 5 | +Builds a small real HDF5 file with h5py (a 1-D signal, a 2-D image and a |
| 6 | +nested group), feeds its raw bytes through ``open_file`` and exercises |
| 7 | +the public surface (tree walk, attrs, preview, array data, import, |
| 8 | +close). Malformed input and the encoding helper are covered too. |
| 9 | +""" |
| 10 | + |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import dlw_h5browser as h5b |
| 14 | +import h5py |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | + |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def h5_bytes(tmp_path): |
| 21 | + """Return the raw bytes of a small multi-node HDF5 file.""" |
| 22 | + path = tmp_path / "sample.h5" |
| 23 | + with h5py.File(path, "w") as f: |
| 24 | + sig = f.create_dataset("sig", data=np.linspace(0.0, 1.0, 32)) |
| 25 | + sig.attrs["units"] = "V" |
| 26 | + f.create_dataset("img", data=np.arange(64, dtype=float).reshape(8, 8)) |
| 27 | + grp = f.create_group("grp") |
| 28 | + grp.create_dataset("inner", data=np.ones(10)) |
| 29 | + return path.read_bytes() |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def opened(h5_bytes): |
| 34 | + """Open the sample file and guarantee cleanup of all browser state.""" |
| 35 | + result = h5b.open_file("sample.h5", h5_bytes) |
| 36 | + try: |
| 37 | + yield result |
| 38 | + finally: |
| 39 | + h5b.close_all() |
| 40 | + |
| 41 | + |
| 42 | +# --------------------------------------------------------------------------- |
| 43 | +# Encoding helper |
| 44 | +# --------------------------------------------------------------------------- |
| 45 | + |
| 46 | + |
| 47 | +def test_safe_decode_bytes(): |
| 48 | + assert h5b._safe_decode_bytes(b"hello") == "hello" |
| 49 | + assert h5b._safe_decode_bytes("already str") == "already str" |
| 50 | + assert h5b._safe_decode_bytes(123) == "123" |
| 51 | + |
| 52 | + |
| 53 | +# --------------------------------------------------------------------------- |
| 54 | +# open / close |
| 55 | +# --------------------------------------------------------------------------- |
| 56 | + |
| 57 | + |
| 58 | +def test_open_file_returns_tree(opened): |
| 59 | + assert opened["filename"] == "sample.h5" |
| 60 | + root = opened["root"] |
| 61 | + child_ids = {c["id"] for c in root["children"]} |
| 62 | + assert {"/sig", "/img", "/grp"} <= child_ids |
| 63 | + |
| 64 | + |
| 65 | +def test_open_invalid_file_raises(): |
| 66 | + with pytest.raises(ValueError): |
| 67 | + h5b.open_file("bogus.h5", b"definitely not an HDF5 file") |
| 68 | + |
| 69 | + |
| 70 | +def test_close_file_clears_state(opened): |
| 71 | + file_id = opened["file_id"] |
| 72 | + assert file_id in h5b.STATE |
| 73 | + h5b.close_file(file_id) |
| 74 | + assert file_id not in h5b.STATE |
| 75 | + |
| 76 | + |
| 77 | +def test_close_file_unknown_id_is_noop(): |
| 78 | + # Must not raise even for an id that was never opened. |
| 79 | + h5b.close_file("never-opened") |
| 80 | + |
| 81 | + |
| 82 | +# --------------------------------------------------------------------------- |
| 83 | +# Node inspection |
| 84 | +# --------------------------------------------------------------------------- |
| 85 | + |
| 86 | + |
| 87 | +def test_node_attrs_exposes_attributes(opened): |
| 88 | + file_id = opened["file_id"] |
| 89 | + attrs = h5b.node_attrs(file_id, "/sig") |
| 90 | + assert attrs["path"] == "/sig" |
| 91 | + assert attrs["attributes"].get("units") == "V" |
| 92 | + |
| 93 | + |
| 94 | +def test_preview_signal(opened): |
| 95 | + file_id = opened["file_id"] |
| 96 | + prev = h5b.preview(file_id, "/sig") |
| 97 | + assert prev["kind"] == "signal" |
| 98 | + assert len(prev["x"]) == len(prev["y"]) == 32 |
| 99 | + |
| 100 | + |
| 101 | +def test_preview_image(opened): |
| 102 | + file_id = opened["file_id"] |
| 103 | + prev = h5b.preview(file_id, "/img") |
| 104 | + assert prev["kind"] == "image" |
| 105 | + assert prev["width"] == 8 |
| 106 | + assert prev["height"] == 8 |
| 107 | + |
| 108 | + |
| 109 | +def test_array_data_returns_shape_and_values(opened): |
| 110 | + file_id = opened["file_id"] |
| 111 | + data = h5b.array_data(file_id, "/img") |
| 112 | + assert data["shape"] == [8, 8] |
| 113 | + assert "float" in data["dtype"] |
| 114 | + assert len(data["data"]) == 8 |
| 115 | + |
| 116 | + |
| 117 | +def test_require_node_rejects_unknown(opened): |
| 118 | + file_id = opened["file_id"] |
| 119 | + with pytest.raises(KeyError): |
| 120 | + h5b.node_attrs(file_id, "/does-not-exist") |
| 121 | + |
| 122 | + |
| 123 | +# --------------------------------------------------------------------------- |
| 124 | +# Import |
| 125 | +# --------------------------------------------------------------------------- |
| 126 | + |
| 127 | + |
| 128 | +def test_import_nodes_builds_objects(opened): |
| 129 | + file_id = opened["file_id"] |
| 130 | + result = h5b.import_nodes(file_id, ["/sig", "/img"]) |
| 131 | + kinds = sorted(kind for kind, _obj in result["objects"]) |
| 132 | + assert kinds == ["image", "signal"] |
| 133 | + assert result["uint32_clipped"] is False |
| 134 | + |
| 135 | + |
| 136 | +def test_import_nodes_unknown_id_raises(opened): |
| 137 | + file_id = opened["file_id"] |
| 138 | + with pytest.raises(KeyError): |
| 139 | + h5b.import_nodes(file_id, ["/nope"]) |
| 140 | + |
| 141 | + |
| 142 | +def test_import_nodes_unknown_file_raises(): |
| 143 | + with pytest.raises(KeyError): |
| 144 | + h5b.import_nodes("never-opened", ["/sig"]) |
0 commit comments