|
| 1 | +import builtins |
| 2 | +import pytest |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import gardenlinux.oci.layer as gl_layer |
| 6 | + |
| 7 | + |
| 8 | +class DummyLayer: |
| 9 | + """Minimal stub for oras.oci.Layer""" |
| 10 | + |
| 11 | + def __init__(self, blob_path, media_type=None, is_dir=False): |
| 12 | + self._init_args = (blob_path, media_type, is_dir) |
| 13 | + |
| 14 | + def to_dict(self): |
| 15 | + return {"dummy": True} |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture(autouse=True) |
| 19 | +def patch__Layer(monkeypatch): |
| 20 | + """Replace oras.oci.Layer with DummyLayer in Layer's module.""" |
| 21 | + monkeypatch.setattr(gl_layer, "_Layer", DummyLayer) |
| 22 | + yield |
| 23 | + |
| 24 | + |
| 25 | +def test_dict_property_returns_with_annotations(tmp_path): |
| 26 | + """dict property should merge _Layer.to_dict() with annotations.""" |
| 27 | + # Arrange |
| 28 | + blob = tmp_path / "blob.txt" |
| 29 | + blob.write_text("data") |
| 30 | + |
| 31 | + # Act |
| 32 | + l = gl_layer.Layer(blob) |
| 33 | + result = l.dict |
| 34 | + |
| 35 | + # Assert |
| 36 | + assert result["dummy"] is True |
| 37 | + assert "annotations" in result |
| 38 | + assert result["annotations"]["org.opencontainers.image.title"] == "blob.txt" |
| 39 | + |
| 40 | + |
| 41 | +def test_getitem_and_delitem_annotations(tmp_path): |
| 42 | + """getitem should return annotations, delitem should clear them.""" |
| 43 | + # Arrange |
| 44 | + blob = tmp_path / "blob.txt" |
| 45 | + blob.write_text("data") |
| 46 | + l = gl_layer.Layer(blob) |
| 47 | + |
| 48 | + # Act / Assert (__getitem__) |
| 49 | + ann = l["annotations"] |
| 50 | + assert isinstance(ann, dict) |
| 51 | + assert "org.opencontainers.image.title" in ann |
| 52 | + |
| 53 | + # Act / Assert (__delitem__) |
| 54 | + l.__delitem__("annotations") |
| 55 | + assert l._annotations == {} |
| 56 | + |
| 57 | + |
| 58 | +def test_getitem_invalid_key_raises(tmp_path): |
| 59 | + """getitem with unsupported key should raise KeyError.""" |
| 60 | + # Arrange |
| 61 | + blob = tmp_path / "blob.txt" |
| 62 | + blob.write_text("data") |
| 63 | + l = gl_layer.Layer(blob) |
| 64 | + |
| 65 | + # Act / Assert |
| 66 | + with pytest.raises(KeyError): |
| 67 | + _ = l["invalid"] |
| 68 | + |
| 69 | + |
| 70 | +def test_setitem_annotations(tmp_path): |
| 71 | + pass |
0 commit comments