Skip to content

Commit

Permalink
chore: all warehouses support views in ListTables (#169)
Browse files Browse the repository at this point in the history
# Description

- Removed unnecessary `IncludesViewsInListTables` flag from integration
tests
- Added a scenario for verifying that view's columns are returned during
`ListColumns`

## Security

- [x] The code changed/added as part of this pull request won't create
any security issues with how the software is being used.
  • Loading branch information
atzoum authored Aug 29, 2024
1 parent 5f799ab commit 098a378
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ type Options struct {
// LegacySupport enables the use of legacy column mappings
LegacySupport bool

IncludesViewsInListTables bool

SpecialCharactersInQuotedTable string // special characters to test in quoted table identifiers (default: <space>,",',`")
SpecialCharactersInQuotedTable string // special characters to test in quoted table identifiers (default: <space>,",',``)

ExtraTests func(t *testing.T, db sqlconnect.DB)
}
Expand Down Expand Up @@ -202,6 +200,7 @@ func TestDatabaseScenarios(t *testing.T, warehouse string, configJSON json.RawMe

t.Run("table admin", func(t *testing.T) {
table := sqlconnect.NewRelationRef(formatfn("test_table"), sqlconnect.WithSchema(schema.Name))
view := sqlconnect.NewRelationRef(formatfn("test_view"), sqlconnect.WithSchema(schema.Name))

t.Run("table doesn't exist", func(t *testing.T) {
t.Run("with context cancelled", func(t *testing.T) {
Expand All @@ -227,6 +226,11 @@ func TestDatabaseScenarios(t *testing.T, warehouse string, configJSON json.RawMe
require.True(t, exists, "it should return true for a table that was just created")
})

t.Run("create view", func(t *testing.T) {
_, err := db.ExecContext(ctx, fmt.Sprintf("CREATE VIEW %s AS SELECT * FROM %s", db.QuoteTable(view), db.QuoteTable(table)))
require.NoError(t, err, "it should be able to create a view")
})

t.Run("list tables", func(t *testing.T) {
t.Run("with context cancelled", func(t *testing.T) {
_, err := db.ListTables(cancelledCtx, schema)
Expand All @@ -238,18 +242,12 @@ func TestDatabaseScenarios(t *testing.T, warehouse string, configJSON json.RawMe
require.Contains(t, tables, table, "it should contain the created table")
})

if opts.IncludesViewsInListTables {
t.Run("list tables with views", func(t *testing.T) {
view := table
view.Name = formatfn(table.Name + "_view")
_, err := db.Exec(fmt.Sprintf("CREATE VIEW %s AS SELECT * FROM %s", db.QuoteTable(view), db.QuoteTable(table)))
require.NoError(t, err, "it should be able to create a view")
tables, err := db.ListTables(ctx, schema)
require.NoError(t, err, "it should be able to list tables")
require.Contains(t, tables, view, "it should contain the created view")
require.Contains(t, tables, view, "it should contain the table as well")
})
}
t.Run("list tables with views", func(t *testing.T) {
tables, err := db.ListTables(ctx, schema)
require.NoError(t, err, "it should be able to list tables")
require.Contains(t, tables, view, "it should contain the created view")
require.Contains(t, tables, table, "it should contain the table as well")
})

t.Run("list tables with prefix", func(t *testing.T) {
t.Run("with context cancelled", func(t *testing.T) {
Expand Down Expand Up @@ -312,6 +310,21 @@ func TestDatabaseScenarios(t *testing.T, warehouse string, configJSON json.RawMe
cols, _ := db.ListColumns(ctx, tableWithInvalidCatalog)
require.Empty(t, cols, "it should return an empty list of columns for an invalid catalog")
})

t.Run("list columns for view", func(t *testing.T) {
columns, err := db.ListColumns(ctx, view)
columns = lo.Map(columns, func(col sqlconnect.ColumnRef, _ int) sqlconnect.ColumnRef {
require.NotEmptyf(t, col.RawType, "it should return the raw type for column %q", col.Name)
col.RawType = ""
return col
})
require.NoError(t, err, "it should be able to list columns for a view")
require.Len(t, columns, 2, "it should return the correct number of columns")
require.ElementsMatch(t, columns, []sqlconnect.ColumnRef{
{Name: formatfn("c1"), Type: "int"},
{Name: formatfn("c2"), Type: "string"},
}, "it should return the correct columns")
})
})

t.Run("list columns for sql query", func(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions sqlconnect/internal/snowflake/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ func TestSnowflakeDB(t *testing.T) {
[]byte(configJSON),
strings.ToUpper,
integrationtest.Options{
LegacySupport: true,
IncludesViewsInListTables: true,
LegacySupport: true,
},
)
}

0 comments on commit 098a378

Please sign in to comment.