|
16 | 16 | # under the License. |
17 | 17 | # pylint:disable=redefined-outer-name |
18 | 18 |
|
| 19 | +import time |
| 20 | + |
19 | 21 | import pytest |
20 | 22 | from pytest_lazy_fixtures import lf |
21 | 23 |
|
22 | 24 | from pyiceberg.catalog.rest import RestCatalog |
| 25 | +from pyiceberg.exceptions import NoSuchViewError |
| 26 | +from pyiceberg.schema import Schema |
| 27 | +from pyiceberg.view.metadata import SQLViewRepresentation, ViewVersion |
23 | 28 |
|
24 | 29 | TEST_NAMESPACE_IDENTIFIER = "TEST NS" |
25 | 30 |
|
@@ -62,3 +67,47 @@ def test_create_namespace_if_already_existing(catalog: RestCatalog) -> None: |
62 | 67 | catalog.create_namespace_if_not_exists(TEST_NAMESPACE_IDENTIFIER) |
63 | 68 |
|
64 | 69 | assert catalog.namespace_exists(TEST_NAMESPACE_IDENTIFIER) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.integration |
| 73 | +@pytest.mark.parametrize("catalog", [lf("session_catalog")]) |
| 74 | +def test_load_view(catalog: RestCatalog, table_schema_nested: Schema, database_name: str, view_name: str) -> None: |
| 75 | + identifier = (database_name, view_name) |
| 76 | + if not catalog.namespace_exists(database_name): |
| 77 | + catalog.create_namespace(database_name) |
| 78 | + |
| 79 | + view_version = ViewVersion( |
| 80 | + version_id=1, |
| 81 | + schema_id=1, |
| 82 | + timestamp_ms=int(time.time() * 1000), |
| 83 | + summary={}, |
| 84 | + representations=[ |
| 85 | + SQLViewRepresentation( |
| 86 | + type="sql", |
| 87 | + sql="SELECT 1 as some_col", |
| 88 | + dialect="spark", |
| 89 | + ) |
| 90 | + ], |
| 91 | + default_namespace=["default"], |
| 92 | + ) |
| 93 | + view = catalog.create_view(identifier, table_schema_nested, view_version=view_version) |
| 94 | + loaded_view = catalog.load_view(identifier) |
| 95 | + assert view.name() == loaded_view.name() |
| 96 | + assert view.metadata == loaded_view.metadata |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.integration |
| 100 | +@pytest.mark.parametrize("catalog", [lf("session_catalog")]) |
| 101 | +def test_load_view_with_table_ident( |
| 102 | + catalog: RestCatalog, table_name: str, table_schema_nested: Schema, database_name: str |
| 103 | +) -> None: |
| 104 | + table_identifier = (database_name, table_name) |
| 105 | + if not catalog.namespace_exists(database_name): |
| 106 | + catalog.create_namespace(database_name) |
| 107 | + |
| 108 | + if not catalog.table_exists(table_identifier): |
| 109 | + catalog.create_table(table_identifier, table_schema_nested) |
| 110 | + |
| 111 | + assert catalog.table_exists(table_identifier) |
| 112 | + with pytest.raises(NoSuchViewError): |
| 113 | + catalog.load_view(table_identifier) |
0 commit comments