Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
package v1
import (
"testing"
metadatapb "github.com/bytebase/omni/metadata"
"github.com/stretchr/testify/require"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
func TestValidateCatalogSchemaNames(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config *storepb.DatabaseConfig
metadata *metadatapb.DatabaseSchemaMetadata
wantErr bool
}{
{
name: "named schemas pass",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{
{Name: "public", Tables: []*storepb.TableCatalog{{Name: "users"}}},
},
},
metadata: &metadatapb.DatabaseSchemaMetadata{
Schemas: []*metadatapb.SchemaMetadata{
{Name: "public", Tables: []*metadatapb.TableMetadata{{Name: "users"}}},
},
},
wantErr: false,
},
{
name: "empty schema rejected for named-schema engine",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{
{Name: "", Tables: []*storepb.TableCatalog{{Name: "users"}}},
},
},
metadata: &metadatapb.DatabaseSchemaMetadata{
Schemas: []*metadatapb.SchemaMetadata{
{Name: "public", Tables: []*metadatapb.TableMetadata{{Name: "users"}}},
},
},
wantErr: true,
},
{
name: "empty schema allowed when metadata has empty schema",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{
{Name: "", Tables: []*storepb.TableCatalog{{Name: "users"}}},
},
},
metadata: &metadatapb.DatabaseSchemaMetadata{
Schemas: []*metadatapb.SchemaMetadata{
{Name: "", Tables: []*metadatapb.TableMetadata{{Name: "users"}}},
},
},
wantErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := validateCatalogSchemaNames(tc.config, tc.metadata)
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
func TestValidateCatalogSemanticTypeIDs(t *testing.T) {
t.Parallel()
tests := []struct {
name string
config *storepb.DatabaseConfig
setting *storepb.SemanticTypeSetting
wantErr string
}{
{
name: "empty semantic type allowed",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{{
Name: "public",
Tables: []*storepb.TableCatalog{{
Name: "users",
Columns: []*storepb.ColumnCatalog{{Name: "email"}},
}},
}},
},
setting: &storepb.SemanticTypeSetting{},
},
{
name: "known column semantic type passes",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{{
Name: "public",
Tables: []*storepb.TableCatalog{{
Name: "users",
Columns: []*storepb.ColumnCatalog{{Name: "email", SemanticType: "email"}},
}},
}},
},
setting: &storepb.SemanticTypeSetting{
Types: []*storepb.SemanticTypeSetting_SemanticType{{Id: "email"}},
},
},
{
name: "built-in semantic types pass without workspace configuration",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{{
Name: "public",
Tables: []*storepb.TableCatalog{{
Name: "users",
Columns: []*storepb.ColumnCatalog{
{Name: "email", SemanticType: defaultSemanticTypeID},
{Name: "phone", SemanticType: defaultPartialSemanticTypeID},
},
}},
}},
},
setting: &storepb.SemanticTypeSetting{Types: getBuiltinSemanticTypes()},
},
{
name: "unknown column semantic type rejected",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{{
Name: "public",
Tables: []*storepb.TableCatalog{{
Name: "users",
Columns: []*storepb.ColumnCatalog{{Name: "email", SemanticType: "missing"}},
}},
}},
},
setting: &storepb.SemanticTypeSetting{
Types: []*storepb.SemanticTypeSetting_SemanticType{{Id: "email"}},
},
wantErr: "invalid semantic type for column \"email\": semantic type id \"missing\" not found",
},
{
name: "nested object semantic type rejected",
config: &storepb.DatabaseConfig{
Schemas: []*storepb.SchemaCatalog{{
Name: "public",
Tables: []*storepb.TableCatalog{{
Name: "events",
Columns: []*storepb.ColumnCatalog{{
Name: "payload",
ObjectSchema: &storepb.ObjectSchema{
Type: storepb.ObjectSchema_OBJECT,
Kind: &storepb.ObjectSchema_StructKind_{
StructKind: &storepb.ObjectSchema_StructKind{
Properties: map[string]*storepb.ObjectSchema{
"email": {Type: storepb.ObjectSchema_STRING, SemanticType: "missing"},
},
},
},
},
}},
}},
}},
},
setting: &storepb.SemanticTypeSetting{
Types: []*storepb.SemanticTypeSetting_SemanticType{{Id: "email"}},
},
wantErr: "invalid object schema for column \"payload\": invalid semantic type for object property \"email\": semantic type id \"missing\" not found",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
err := validateCatalogSemanticTypeIDs(tc.config, tc.setting)
if tc.wantErr != "" {
require.EqualError(t, err, tc.wantErr)
return
}
require.NoError(t, err)
})
}
}