diff --git a/pyiceberg/cli/output.py b/pyiceberg/cli/output.py index 4a508d5343..ac1d0ae58b 100644 --- a/pyiceberg/cli/output.py +++ b/pyiceberg/cli/output.py @@ -271,10 +271,4 @@ def version(self, version: str) -> None: self._out({"version": version}) def describe_refs(self, refs: list[tuple[str, SnapshotRefType, dict[str, str]]]) -> None: - self._out( - [ - {"name": name, "type": type, detail_key: detail_val} - for name, type, detail in refs - for detail_key, detail_val in detail.items() - ] - ) + self._out([{"name": name, "type": type, **detail} for name, type, detail in refs]) diff --git a/tests/cli/test_output.py b/tests/cli/test_output.py index a67d5ea255..a75b5c6e6e 100644 --- a/tests/cli/test_output.py +++ b/tests/cli/test_output.py @@ -14,3 +14,65 @@ # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. +import json + +import pytest + +from pyiceberg.cli.output import JsonOutput +from pyiceberg.table.refs import SnapshotRefType + + +def test_json_describe_refs_single_ref(capsys: pytest.CaptureFixture) -> None: + JsonOutput().describe_refs( + [ + ( + "main", + SnapshotRefType.BRANCH, + { + "min_snapshots_to_keep": "1", + "max_snapshot_age_ms": "432000000", + "max_ref_age_ms": "forever", + }, + ) + ] + ) + + assert json.loads(capsys.readouterr().out) == [ + { + "name": "main", + "type": "branch", + "min_snapshots_to_keep": "1", + "max_snapshot_age_ms": "432000000", + "max_ref_age_ms": "forever", + } + ] + + +def test_json_describe_refs_multiple_refs(capsys: pytest.CaptureFixture) -> None: + JsonOutput().describe_refs( + [ + ( + "main", + SnapshotRefType.BRANCH, + { + "min_snapshots_to_keep": "1", + "max_snapshot_age_ms": "432000000", + "max_ref_age_ms": "forever", + }, + ), + ("v1.0", SnapshotRefType.TAG, {"max_ref_age_ms": "forever"}), + ("no-retention", SnapshotRefType.BRANCH, {}), + ] + ) + + assert json.loads(capsys.readouterr().out) == [ + { + "name": "main", + "type": "branch", + "min_snapshots_to_keep": "1", + "max_snapshot_age_ms": "432000000", + "max_ref_age_ms": "forever", + }, + {"name": "v1.0", "type": "tag", "max_ref_age_ms": "forever"}, + {"name": "no-retention", "type": "branch"}, + ]