Skip to content

Commit 20dad7a

Browse files
committed
tests
1 parent 4dbb967 commit 20dad7a

2 files changed

Lines changed: 157 additions & 0 deletions

File tree

‎tests/conftest.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,43 @@ def example_view_metadata_v1() -> dict[str, Any]:
11741174
}
11751175

11761176

1177+
@pytest.fixture
1178+
def example_view_metadata_v1_multiple_versions() -> dict[str, Any]:
1179+
return {
1180+
"view-uuid": "a20125c8-7284-442c-9aea-15fee620737c",
1181+
"format-version": 1,
1182+
"location": "s3://bucket/test/location/test_view",
1183+
"current-version-id": 2,
1184+
"versions": [
1185+
{
1186+
"version-id": 1,
1187+
"timestamp-ms": 1602638573874,
1188+
"schema-id": 1,
1189+
"summary": {},
1190+
"representations": [{"type": "sql", "sql": "SELECT 1", "dialect": "spark"}],
1191+
"default-namespace": ["default"],
1192+
},
1193+
{
1194+
"version-id": 2,
1195+
"timestamp-ms": 1602638573875,
1196+
"schema-id": 2,
1197+
"summary": {},
1198+
"representations": [{"type": "sql", "sql": "SELECT 2", "dialect": "spark"}],
1199+
"default-namespace": ["default"],
1200+
},
1201+
],
1202+
"schemas": [
1203+
{"type": "struct", "schema-id": 1, "fields": [{"id": 1, "name": "a", "required": True, "type": "long"}]},
1204+
{"type": "struct", "schema-id": 2, "fields": [{"id": 2, "name": "b", "required": True, "type": "string"}]},
1205+
],
1206+
"version-log": [
1207+
{"timestamp-ms": 1602638573874, "version-id": 1},
1208+
{"timestamp-ms": 1602638573875, "version-id": 2},
1209+
],
1210+
"properties": {},
1211+
}
1212+
1213+
11771214
@pytest.fixture
11781215
def example_table_metadata_v3() -> dict[str, Any]:
11791216
return EXAMPLE_TABLE_METADATA_V3

‎tests/test_view.py‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
from typing import Any
18+
19+
import pytest
20+
21+
from pyiceberg.schema import Schema
22+
from pyiceberg.view import View
23+
from pyiceberg.view.metadata import SQLViewRepresentation, ViewHistoryEntry, ViewMetadata, ViewVersion
24+
25+
26+
@pytest.fixture
27+
def view(example_view_metadata_v1: dict[str, Any]) -> View:
28+
metadata = ViewMetadata.model_validate(example_view_metadata_v1)
29+
return View(("default", "test_view"), metadata)
30+
31+
32+
def test_view_schema(view: View) -> None:
33+
schema = view.schema()
34+
assert isinstance(schema, Schema)
35+
assert schema.schema_id == 1
36+
assert len(schema.fields) == 3
37+
assert schema.find_field("x") is not None
38+
assert schema.find_field("y") is not None
39+
assert schema.find_field("z") is not None
40+
41+
42+
def test_view_schemas(view: View) -> None:
43+
schemas = view.schemas()
44+
assert isinstance(schemas, dict)
45+
assert len(schemas) == 1
46+
assert 1 in schemas
47+
assert isinstance(schemas[1], Schema)
48+
49+
50+
def test_view_current_version(view: View) -> None:
51+
version = view.current_version()
52+
assert isinstance(version, ViewVersion)
53+
assert version.version_id == 1
54+
assert version.schema_id == 1
55+
56+
57+
def test_view_versions(view: View) -> None:
58+
versions = view.versions()
59+
assert len(versions) == 1
60+
assert isinstance(versions[0], ViewVersion)
61+
assert versions[0].version_id == 1
62+
63+
64+
def test_view_version_by_id(view: View) -> None:
65+
version = view.version(1)
66+
assert isinstance(version, ViewVersion)
67+
assert version.version_id == 1
68+
assert version == view.current_version()
69+
70+
71+
def test_view_history(view: View) -> None:
72+
history = view.history()
73+
assert len(history) == 1
74+
assert isinstance(history[0], ViewHistoryEntry)
75+
assert history[0].version_id == 1
76+
assert history[0].timestamp_ms == 1602638573874
77+
78+
79+
def test_view_properties(view: View) -> None:
80+
assert view.properties() == {"comment": "this is a test view"}
81+
82+
83+
def test_view_location(view: View) -> None:
84+
assert view.location() == "s3://bucket/test/location/test_view"
85+
86+
87+
def test_view_uuid(view: View) -> None:
88+
assert view.uuid() == "a20125c8-7284-442c-9aea-15fee620737c"
89+
90+
91+
def test_view_sql_for_dialect(view: View) -> None:
92+
repr = view.sql_for("spark")
93+
assert isinstance(repr, SQLViewRepresentation)
94+
assert repr.dialect == "spark"
95+
assert repr.sql == "SELECT * FROM prod.db.table"
96+
97+
98+
def test_view_schemas_multiple(example_view_metadata_v1_multiple_versions: dict[str, Any]) -> None:
99+
view = View(("default", "test_view"), ViewMetadata.model_validate(example_view_metadata_v1_multiple_versions))
100+
schemas = view.schemas()
101+
assert len(schemas) == 2
102+
assert 1 in schemas
103+
assert 2 in schemas
104+
assert view.schema().schema_id == 2
105+
106+
107+
def test_view_versions_multiple(example_view_metadata_v1_multiple_versions: dict[str, Any]) -> None:
108+
view = View(("default", "test_view"), ViewMetadata.model_validate(example_view_metadata_v1_multiple_versions))
109+
assert len(view.versions()) == 2
110+
assert view.current_version().version_id == 2
111+
112+
113+
def test_view_version_unknown_id(view: View) -> None:
114+
with pytest.raises(StopIteration):
115+
view.version(999)
116+
117+
118+
def test_view_sql_for_unknown_dialect(view: View) -> None:
119+
with pytest.raises(StopIteration):
120+
view.sql_for("trino")

0 commit comments

Comments
 (0)