yayaha commented on code in PR #4321:
URL: https://github.com/apache/iceberg-python/pull/4321#discussion_r4529876888


##########
tests/catalog/test_rest.py:
##########
@@ -3713,3 +3716,52 @@ def test_load_table_without_storage_credentials(
     )
     assert actual.metadata.model_dump() == expected.metadata.model_dump()
     assert actual == expected
+
+
+def test_load_responses_parse_labels(
+    example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any], 
example_view_metadata_rest_json: dict[str, Any]
+) -> None:
+    labels_json = {"object-labels": {"owner": "team-a"}, "fields": 
[{"field-id": 1, "labels": {"classification": "pii"}}]}
+    expected = Labels(object_labels={"owner": "team-a"}, 
fields=[FieldLabel(field_id=1, labels={"classification": "pii"})])
+    cases: list[tuple[type[TableResponse | ViewResponse], dict[str, Any]]] = [
+        (TableResponse, example_table_metadata_with_snapshot_v1_rest_json),
+        (ViewResponse, example_view_metadata_rest_json),
+    ]
+    for response_type, payload in cases:
+        assert response_type.model_validate({**payload, "labels": 
labels_json}).labels == expected
+        assert response_type.model_validate({**payload, "labels": 
None}).labels == Labels()
+        assert response_type.model_validate(payload).labels == Labels()
+
+
+def test_load_table_with_labels(rest_mock: Mocker, 
example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any]) -> None:
+    rest_mock.get(
+        f"{TEST_URI}v1/namespaces/fokko/tables/table",
+        json={
+            **example_table_metadata_with_snapshot_v1_rest_json,
+            "labels": {"object-labels": {"owner": "team-a"}, "fields": 
[{"field-id": 1, "labels": {"classification": "pii"}}]},
+        },
+        status_code=200,
+        request_headers=TEST_HEADERS,
+    )
+    catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
+    table = catalog.load_table(("fokko", "table"))

Review Comment:
   Done in f1d11c08. The test now mocks load, create, and register, and it 
checks `labels` on all three tables. I renamed it to 
`test_table_labels_from_rest_responses`.
   



##########
tests/catalog/test_rest.py:
##########
@@ -3713,3 +3716,52 @@ def test_load_table_without_storage_credentials(
     )
     assert actual.metadata.model_dump() == expected.metadata.model_dump()
     assert actual == expected
+
+
+def test_load_responses_parse_labels(
+    example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any], 
example_view_metadata_rest_json: dict[str, Any]
+) -> None:
+    labels_json = {"object-labels": {"owner": "team-a"}, "fields": 
[{"field-id": 1, "labels": {"classification": "pii"}}]}
+    expected = Labels(object_labels={"owner": "team-a"}, 
fields=[FieldLabel(field_id=1, labels={"classification": "pii"})])
+    cases: list[tuple[type[TableResponse | ViewResponse], dict[str, Any]]] = [
+        (TableResponse, example_table_metadata_with_snapshot_v1_rest_json),
+        (ViewResponse, example_view_metadata_rest_json),
+    ]
+    for response_type, payload in cases:
+        assert response_type.model_validate({**payload, "labels": 
labels_json}).labels == expected
+        assert response_type.model_validate({**payload, "labels": 
None}).labels == Labels()
+        assert response_type.model_validate(payload).labels == Labels()
+
+
+def test_load_table_with_labels(rest_mock: Mocker, 
example_table_metadata_with_snapshot_v1_rest_json: dict[str, Any]) -> None:
+    rest_mock.get(
+        f"{TEST_URI}v1/namespaces/fokko/tables/table",
+        json={
+            **example_table_metadata_with_snapshot_v1_rest_json,
+            "labels": {"object-labels": {"owner": "team-a"}, "fields": 
[{"field-id": 1, "labels": {"classification": "pii"}}]},
+        },
+        status_code=200,
+        request_headers=TEST_HEADERS,
+    )
+    catalog = RestCatalog("rest", uri=TEST_URI, token=TEST_TOKEN)
+    table = catalog.load_table(("fokko", "table"))
+
+    expected = Labels(object_labels={"owner": "team-a"}, 
fields=[FieldLabel(field_id=1, labels={"classification": "pii"})])
+    assert table.labels == expected
+
+    # Labels are not table state, so equality ignores them and refresh() keeps 
the labels from the first load.
+    assert table == Table(
+        identifier=("fokko", "table"),
+        
metadata_location=example_table_metadata_with_snapshot_v1_rest_json["metadata-location"],
+        
metadata=TableMetadataV1(**example_table_metadata_with_snapshot_v1_rest_json["metadata"]),
+        io=load_file_io(),
+        catalog=catalog,
+    )
+    rest_mock.get(
+        f"{TEST_URI}v1/namespaces/fokko/tables/table",
+        json={**example_table_metadata_with_snapshot_v1_rest_json, "labels": 
{"object-labels": {"owner": "team-b"}}},
+        status_code=200,
+        request_headers=TEST_HEADERS,
+    )
+    table.refresh()
+    assert table.labels == expected

Review Comment:
   Done in f1d11c08, with your suggestion and `import pickle`.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to