Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

test: add tests for transformSchemaToMetadata (#7074)

rebelicebe4f48afc825
diff --git a/backend/api/v1/schema_design_service.go b/backend/api/v1/schema_design_service.go
index 515479b508..a46f00e455 100644
--- a/backend/api/v1/schema_design_service.go
+++ b/backend/api/v1/schema_design_service.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
+ "sort"
"strconv"
"golang.org/x/exp/slices"
@@ -419,9 +420,14 @@ func (s *databaseState) convertToDatabaseMetadata() *v1pb.DatabaseMetadata {
for _, schema := range s.schemas {
schemas = append(schemas, schema.convertToSchemaMetadata())
}
+ sort.Slice(schemas, func(i, j int) bool {
+ return schemas[i].Name < schemas[j].Name
+ })
return &v1pb.DatabaseMetadata{
Name: s.name,
Schemas: schemas,
+ // Unsupported, for tests only.
+ Extensions: []*v1pb.ExtensionMetadata{},
}
}
@@ -441,9 +447,17 @@ func (s *schemaState) convertToSchemaMetadata() *v1pb.SchemaMetadata {
for _, table := range s.tables {
tables = append(tables, table.convertToTableMetadata())
}
+ sort.Slice(tables, func(i, j int) bool {
+ return tables[i].Name < tables[j].Name
+ })
return &v1pb.SchemaMetadata{
Name: s.name,
Tables: tables,
+ // Unsupported, for tests only.
+ Views: []*v1pb.ViewMetadata{},
+ Functions: []*v1pb.FunctionMetadata{},
+ Streams: []*v1pb.StreamMetadata{},
+ Tasks: []*v1pb.TaskMetadata{},
}
}
@@ -452,8 +466,9 @@ type tableState struct {
columns map[string]*columnState
}
-func newTableState() *tableState {
+func newTableState(name string) *tableState {
return &tableState{
+ name: name,
columns: make(map[string]*columnState),
}
}
@@ -463,9 +478,15 @@ func (t *tableState) convertToTableMetadata() *v1pb.TableMetadata {
for _, column := range t.columns {
columns = append(columns, column.convertToColumnMetadata())
}
+ sort.Slice(columns, func(i, j int) bool {
+ return columns[i].Name < columns[j].Name
+ })
return &v1pb.TableMetadata{
Name: t.name,
Columns: columns,
+ // Unsupported, for tests only.
+ Indexes: []*v1pb.IndexMetadata{},
+ ForeignKeys: []*v1pb.ForeignKeyMetadata{},
}
}
@@ -502,7 +523,7 @@ func (t *mysqlTransformer) EnterCreateTable(ctx *mysql.CreateTableContext) {
return
}
- schema.tables[tableName] = newTableState()
+ schema.tables[tableName] = newTableState(tableName)
t.currentTable = tableName
}
diff --git a/backend/api/v1/schema_design_service_test.go b/backend/api/v1/schema_design_service_test.go
new file mode 100644
index 0000000000..979379f708
--- /dev/null
+++ b/backend/api/v1/schema_design_service_test.go
@@ -0,0 +1,54 @@
+package v1
+
+import (
+ "io"
+ "os"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
+
+ v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
+)
+
+type test struct {
+ Engine v1pb.Engine
+ Schema string
+ Metadata *v1pb.DatabaseMetadata
+}
+
+func TestTransformSchemaString(t *testing.T) {
+ const (
+ record = false
+ )
+ var (
+ filepath = "testdata/schema.yaml"
+ )
+
+ a := require.New(t)
+ yamlFile, err := os.Open(filepath)
+ a.NoError(err)
+
+ tests := []test{}
+ byteValue, err := io.ReadAll(yamlFile)
+ a.NoError(yamlFile.Close())
+ a.NoError(err)
+ a.NoError(yaml.Unmarshal(byteValue, &tests))
+
+ for i, t := range tests {
+ result, err := transformSchemaStringToDatabaseMetadata(t.Engine, t.Schema)
+ a.NoError(err)
+ if record {
+ tests[i].Metadata = result
+ } else {
+ a.Equal(t.Metadata, result)
+ }
+ }
+
+ if record {
+ byteValue, err := yaml.Marshal(tests)
+ a.NoError(err)
+ err = os.WriteFile(filepath, byteValue, 0644)
+ a.NoError(err)
+ }
+}
diff --git a/backend/api/v1/testdata/schema.yaml b/backend/api/v1/testdata/schema.yaml
new file mode 100644
index 0000000000..048900c9e6
--- /dev/null
+++ b/backend/api/v1/testdata/schema.yaml
@@ -0,0 +1,54 @@
+- engine: 2
+ schema: create table t(a int); create table t2(b int);
+ metadata:
+ name: ""
+ schemas:
+ - name: ""
+ tables:
+ - name: t
+ columns:
+ - name: a
+ position: 0
+ default: null
+ nullable: false
+ type: int
+ characterset: ""
+ collation: ""
+ comment: ""
+ indexes: []
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ - name: t2
+ columns:
+ - name: b
+ position: 0
+ default: null
+ nullable: false
+ type: int
+ characterset: ""
+ collation: ""
+ comment: ""
+ indexes: []
+ engine: ""
+ collation: ""
+ rowcount: 0
+ datasize: 0
+ indexsize: 0
+ datafree: 0
+ createoptions: ""
+ comment: ""
+ foreignkeys: []
+ views: []
+ functions: []
+ streams: []
+ tasks: []
+ characterset: ""
+ collation: ""
+ extensions: []