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 tidb
import (
"io"
"os"
"path/filepath"
"testing"
metadatapb "github.com/bytebase/omni/metadata"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"
"gopkg.in/yaml.v3"
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/component/sheet"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
"github.com/bytebase/bytebase/backend/store/model"
)
type testData struct {
Statement string
// Use custom yaml tag to avoid generate field name `ignorecasesensitive`.
IgnoreCaseSensitive bool `yaml:"ignore_case_sensitive"`
Want string
Advice *storepb.Advice
}
func TestWalkThrough(t *testing.T) {
originDatabase := &metadatapb.DatabaseSchemaMetadata{
Name: "test",
}
tests := []testData{}
filepath := filepath.Join("testdata", "walk_through.yaml")
yamlFile, err := os.Open(filepath)
require.NoError(t, err)
defer yamlFile.Close()
byteValue, err := io.ReadAll(yamlFile)
require.NoError(t, err)
err = yaml.Unmarshal(byteValue, &tests)
require.NoError(t, err)
sm := sheet.NewManager()
for _, test := range tests {
// Make a deep copy to avoid mutation across tests
protoData, ok := proto.Clone(originDatabase).(*metadatapb.DatabaseSchemaMetadata)
require.True(t, ok)
// Create DatabaseMetadata for walk-through
state := model.NewDatabaseMetadata(protoData, nil, nil, storepb.Engine_TIDB, !test.IgnoreCaseSensitive)
stmts, _ := sm.GetStatementsForChecks(storepb.Engine_TIDB, test.Statement)
asts := base.ExtractASTs(stmts)
advice := WalkThrough(state, asts)
if advice != nil {
require.NotNil(t, test.Advice, "unexpected advice for statement %q: %+v", test.Statement, advice)
require.Equal(t, test.Advice.Code, advice.Code, "statement %q", test.Statement)
require.Equal(t, test.Advice.Content, advice.Content, "statement %q", test.Statement)
continue
}
// Skip comparison if want is empty (error cases)
if test.Want == "" {
continue
}
want := &metadatapb.DatabaseSchemaMetadata{}
err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)
require.NoError(t, err)
result := state.GetProto()
diff := cmp.Diff(want, result, protocmp.Transform(),
protocmp.SortRepeatedFields(&metadatapb.DatabaseSchemaMetadata{}, "schemas"),
protocmp.SortRepeatedFields(&metadatapb.SchemaMetadata{}, "tables", "views"),
protocmp.SortRepeatedFields(&metadatapb.TableMetadata{}, "indexes", "columns"),
)
require.Empty(t, diff)
}
}