This is an automated email from the ASF dual-hosted git repository.
laskoviymishka pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-go.git
The following commit(s) were added to refs/heads/main by this push:
new f4139d3dd test(catalogtest): cover views (#2651)
f4139d3dd is described below
commit f4139d3ddcd932e802f196da03fbff67f22e2a56
Author: Andrei Tserakhau <[email protected]>
AuthorDate: Fri Nov 20 20:05:41 2026 +0100
test(catalogtest): cover views (#2651)
* test(catalogtest): cover views
Add a views slice to the catalog conformance suite. RunCatalogTests runs
it for catalogs that implement catalog.ViewCatalog, found with
catalog.As, which today means REST, SQL and Hive. Other catalogs skip it
and Config gets no new flag.
The tests cover create then load (identifier, schema, SQL and dialect),
a duplicate create returning ErrViewAlreadyExists and leaving the
original view alone, load and drop of a missing view returning
ErrNoSuchView, ListViews before a create, after it and after a drop,
CheckViewExists before and after, and DropNamespace on a namespace that
still holds a view returning ErrNamespaceNotEmpty.
Views are created with TableSchema (field IDs 1 and 2), so catalogs that
assign fresh IDs on create and catalogs that keep the given IDs report
the same schema. ID assignment is out of scope here, as are UpdateView
and RenameView.
Always register the view subtests and skip them when the catalog does not
implement ViewCatalog or its view calls return iceberg.ErrNotImplemented.
---
catalog/catalogtest/catalogtest.go | 12 +-
catalog/catalogtest/views.go | 243 +++++++++++++++++++++++++++++++++++++
2 files changed, 254 insertions(+), 1 deletion(-)
diff --git a/catalog/catalogtest/catalogtest.go
b/catalog/catalogtest/catalogtest.go
index 257344b91..0d97e6b1a 100644
--- a/catalog/catalogtest/catalogtest.go
+++ b/catalog/catalogtest/catalogtest.go
@@ -30,7 +30,9 @@
// }
//
// Each test gets its own catalog from Config.NewCatalog, so tests never share
-// state and may run in any order.
+// state and may run in any order. The view tests are skipped for catalogs that
+// do not implement [catalog.ViewCatalog] or whose view calls return
+// [iceberg.ErrNotImplemented].
package catalogtest
import (
@@ -161,6 +163,14 @@ func RunCatalogTests(t *testing.T, cfg Config) {
} else {
t.Run("NamespacePropertiesNotSupported", func(t *testing.T) {
testNamespacePropertiesNotSupported(t, cfg) })
}
+
+ t.Run("BasicCreateView", func(t *testing.T) { testBasicCreateView(t,
cfg) })
+ t.Run("CreateViewThatAlreadyExists", func(t *testing.T) {
testCreateViewThatAlreadyExists(t, cfg) })
+ t.Run("LoadMissingView", func(t *testing.T) { testLoadMissingView(t,
cfg) })
+ t.Run("ListViews", func(t *testing.T) { testListViews(t, cfg) })
+ t.Run("DropView", func(t *testing.T) { testDropView(t, cfg) })
+ t.Run("DropMissingView", func(t *testing.T) { testDropMissingView(t,
cfg) })
+ t.Run("DropNamespaceWithView", func(t *testing.T) {
testDropNamespaceWithView(t, cfg) })
}
// testBasicCreateTable asserts that a newly created table is visible to the
diff --git a/catalog/catalogtest/views.go b/catalog/catalogtest/views.go
new file mode 100644
index 000000000..d4e07e6f8
--- /dev/null
+++ b/catalog/catalogtest/views.go
@@ -0,0 +1,243 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package catalogtest
+
+import (
+ "context"
+ "errors"
+ "testing"
+
+ "github.com/apache/iceberg-go"
+ "github.com/apache/iceberg-go/catalog"
+ "github.com/apache/iceberg-go/table"
+ "github.com/apache/iceberg-go/view"
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// viewSQL is the SQL the conformance tests create views with.
+const viewSQL = "SELECT id, data FROM tbl"
+
+// viewDialect is the dialect of viewSQL.
+const viewDialect = "spark"
+
+// newViewVersion returns the version the conformance tests create a view in
+// namespace with.
+func newViewVersion(t *testing.T, namespace table.Identifier) *view.Version {
+ t.Helper()
+
+ version, err := view.NewVersionFromSQL(1, 0,
+ view.Representation{SQL: viewSQL, Dialect: viewDialect},
namespace)
+ require.NoError(t, err)
+
+ return version
+}
+
+// createView creates the conformance view at ident and drops it on cleanup.
+// It uses [TableSchema] rather than [Schema] so that catalogs that assign
+// fresh field IDs on create and catalogs that keep the given IDs agree.
+func createView(t *testing.T, views catalog.ViewCatalog, ident
table.Identifier) *view.View {
+ t.Helper()
+ ctx := context.Background()
+
+ v, err := views.CreateView(ctx, ident, newViewVersion(t,
ident[:len(ident)-1]), TableSchema)
+ require.NoError(t, err)
+ t.Cleanup(func() { _ = views.DropView(ctx, ident) })
+
+ return v
+}
+
+// listViews collects every view identifier the catalog reports for namespace.
+func listViews(t *testing.T, views catalog.ViewCatalog, namespace
table.Identifier) []table.Identifier {
+ t.Helper()
+
+ var out []table.Identifier
+ for ident, err := range views.ListViews(context.Background(),
namespace) {
+ require.NoError(t, err)
+ out = append(out, ident)
+ }
+
+ return out
+}
+
+// newViewCatalog returns a fresh catalog from cfg and its view capability. It
+// skips the test when the catalog has no view support, such as a REST server
+// that does not advertise the view endpoints.
+func newViewCatalog(t *testing.T, cfg Config) (catalog.Catalog,
catalog.ViewCatalog) {
+ t.Helper()
+
+ cat := cfg.NewCatalog(t)
+ views, ok := catalog.As[catalog.ViewCatalog](cat)
+ if !ok {
+ t.Skip("catalog does not implement catalog.ViewCatalog")
+ }
+
+ _, ident := newIdentifiers()
+ if _, err := views.CheckViewExists(context.Background(), ident);
errors.Is(err, iceberg.ErrNotImplemented) {
+ t.Skipf("catalog does not support views: %v", err)
+ }
+
+ return cat, views
+}
+
+// assertConformanceView asserts that v is the conformance view at ident.
+func assertConformanceView(t *testing.T, ident table.Identifier, v *view.View)
{
+ t.Helper()
+
+ assert.Equal(t, ident, v.Identifier(), "view should report the
identifier it was created with")
+ assert.Equal(t, TableSchema.AsStruct(), v.CurrentSchema().AsStruct(),
"view should keep its schema")
+
+ version := v.CurrentVersion()
+ require.NotNil(t, version, "view should have a current version")
+ require.Len(t, version.Representations, 1)
+ assert.Equal(t, viewSQL, version.Representations[0].SQL, "view should
keep its SQL")
+ assert.Equal(t, viewDialect, version.Representations[0].Dialect, "view
should keep its dialect")
+}
+
+// testBasicCreateView asserts that a newly created view is visible to the
+// catalog and reports the schema and SQL it was created with.
+func testBasicCreateView(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ exists, err := views.CheckViewExists(ctx, ident)
+ require.NoError(t, err)
+ assert.False(t, exists, "view should not exist before create")
+
+ createView(t, views, ident)
+
+ exists, err = views.CheckViewExists(ctx, ident)
+ require.NoError(t, err)
+ assert.True(t, exists, "view should exist after create")
+
+ v, err := views.LoadView(ctx, ident)
+ require.NoError(t, err)
+ assertConformanceView(t, ident, v)
+}
+
+// testCreateViewThatAlreadyExists asserts that creating a view over an
+// existing one is rejected and leaves the original view intact.
+func testCreateViewThatAlreadyExists(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ createView(t, views, ident)
+
+ other, err := view.NewVersionFromSQL(1, 0,
+ view.Representation{SQL: "SELECT some_id FROM other", Dialect:
viewDialect}, namespace)
+ require.NoError(t, err)
+
+ _, err = views.CreateView(ctx, ident, other, OtherSchema)
+ assert.ErrorIs(t, err, catalog.ErrViewAlreadyExists)
+
+ v, err := views.LoadView(ctx, ident)
+ require.NoError(t, err)
+ assertConformanceView(t, ident, v)
+}
+
+// testLoadMissingView asserts that loading a view that was never created
+// reports that the view does not exist.
+func testLoadMissingView(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ _, err := views.LoadView(ctx, ident)
+ assert.ErrorIs(t, err, catalog.ErrNoSuchView)
+}
+
+// testListViews asserts that ListViews reflects creates and drops.
+func testListViews(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ assert.Empty(t, listViews(t, views, namespace), "new namespace should
have no views")
+
+ createView(t, views, ident)
+ assert.Equal(t, []table.Identifier{ident}, listViews(t, views,
namespace))
+
+ require.NoError(t, views.DropView(ctx, ident))
+ assert.Empty(t, listViews(t, views, namespace), "dropped view should
not be listed")
+}
+
+// testDropView asserts that a dropped view is no longer visible.
+func testDropView(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ createView(t, views, ident)
+ require.NoError(t, views.DropView(ctx, ident))
+
+ exists, err := views.CheckViewExists(ctx, ident)
+ require.NoError(t, err)
+ assert.False(t, exists, "view should not exist after drop")
+
+ _, err = views.LoadView(ctx, ident)
+ assert.ErrorIs(t, err, catalog.ErrNoSuchView)
+}
+
+// testDropMissingView asserts that dropping a view that was never created
+// reports that the view does not exist.
+func testDropMissingView(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ assert.ErrorIs(t, views.DropView(ctx, ident), catalog.ErrNoSuchView)
+}
+
+// testDropNamespaceWithView asserts that a namespace holding a view cannot be
+// dropped and that the view survives the attempt.
+func testDropNamespaceWithView(t *testing.T, cfg Config) {
+ ctx := context.Background()
+ cat, views := newViewCatalog(t, cfg)
+ namespace, ident := newIdentifiers()
+
+ require.NoError(t, cat.CreateNamespace(ctx, namespace, nil))
+ t.Cleanup(func() { _ = cat.DropNamespace(ctx, namespace) })
+
+ createView(t, views, ident)
+
+ assert.ErrorIs(t, cat.DropNamespace(ctx, namespace),
catalog.ErrNamespaceNotEmpty)
+
+ exists, err := views.CheckViewExists(ctx, ident)
+ require.NoError(t, err)
+ assert.True(t, exists, "view should still exist after a rejected
namespace drop")
+}