Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
test: refactor the testkit for parser (#3196)
rebelice6f5083b6048e
diff --git a/plugin/parser/engine/pg/deparse_test.go b/plugin/parser/engine/pg/deparse_test.goindex b78648b6a1..1b7d9d4055 100644--- a/plugin/parser/engine/pg/deparse_test.go+++ b/plugin/parser/engine/pg/deparse_test.go@@ -1,103 +1,65 @@package pgimport (+ "io"+ "os"+ "path/filepath""testing""github.com/stretchr/testify/require"+ "gopkg.in/yaml.v3""github.com/bytebase/bytebase/plugin/parser")-type testDeparseData struct {- stmt string- want string+// TestDeparseData is the test data struct.+type TestDeparseData struct {+ Stmt string `yaml:"stmt"`+ Want string `yaml:"want"`}-func runDeparseTest(t *testing.T, tests []testDeparseData) {+func runDeparseTest(t *testing.T, file string, record bool) {p := &PostgreSQLParser{}- for _, test := range tests {- nodeList, err := p.Parse(parser.ParseContext{}, test.stmt)+ var tests []TestDeparseData+ filepath := filepath.Join("test-data", file)+ 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)++ for i, test := range tests {+ nodeList, err := p.Parse(parser.ParseContext{}, test.Stmt)require.NoError(t, err)require.Len(t, nodeList, 1)res, err := p.Deparse(parser.DeparseContext{}, nodeList[0])require.NoError(t, err)- require.Equal(t, test.want, res, test.stmt)+ if record {+ tests[i].Want = res+ } else {+ require.Equal(t, test.Want, res, test.Stmt)+ }}-}-func TestCreateTable(t *testing.T) {- tests := []testDeparseData{- {- stmt: `CREATE TABLE tech_book(- a smallint,- b integer,- c bigint,- d decimal(10, 2),- e numeric(4),- f real,- g double precision,- h smallserial,- i serial,- j bigserial,- k int8,- l serial8,- m float8,- n int,- o int4,- p float4,- q int2,- r serial2,- s serial4,- t decimal)`,- want: `CREATE TABLE "tech_book" (- "a" smallint,- "b" integer,- "c" bigint,- "d" numeric(10, 2),- "e" numeric(4),- "f" real,- "g" double precision,- "h" smallserial,- "i" serial,- "j" bigserial,- "k" bigint,- "l" bigserial,- "m" double precision,- "n" integer,- "o" integer,- "p" real,- "q" smallint,- "r" smallserial,- "s" serial,- "t" numeric-)`,- },- {- stmt: `create table "TechBook"(a "user defined data type")`,- want: `CREATE TABLE "TechBook" (- "a" "user defined data type"-)`,- },- {- stmt: `- CREATE TABLE tech_book(- a char(20),- b character(30),- c varchar(330),- d character varying(400),- e text- )- `,- want: `CREATE TABLE "tech_book" (- "a" character(20),- "b" character(30),- "c" character varying(330),- "d" character varying(400),- "e" text-)`,- },+ if record {+ err := yamlFile.Close()+ require.NoError(t, err)+ byteValue, err = yaml.Marshal(tests)+ require.NoError(t, err)+ err = os.WriteFile(filepath, byteValue, 0644)+ require.NoError(t, err)}+}- runDeparseTest(t, tests)+func TestDeparse(t *testing.T) {+ testFileList := []string{+ "test_create_table_data.yaml",+ }+ for _, test := range testFileList {+ runDeparseTest(t, test, false /* record */)+ }}diff --git a/plugin/parser/engine/pg/test-data/test_create_table_data.yaml b/plugin/parser/engine/pg/test-data/test_create_table_data.yamlnew file mode 100644index 0000000000..f7c284a677--- /dev/null+++ b/plugin/parser/engine/pg/test-data/test_create_table_data.yaml@@ -0,0 +1,66 @@+- stmt: create table "TechBook"(a "user defined data type")+ want: |-+ CREATE TABLE "TechBook" (+ "a" "user defined data type"+ )+- stmt: |-+ CREATE TABLE tech_book(+ a char(20),+ b character(30),+ c varchar(330),+ d character varying(400),+ e text+ )+ want: |-+ CREATE TABLE "tech_book" (+ "a" character(20),+ "b" character(30),+ "c" character varying(330),+ "d" character varying(400),+ "e" text+ )+- stmt: |-+ CREATE TABLE tech_book(+ a smallint,+ b integer,+ c bigint,+ d decimal(10, 2),+ e numeric(4),+ f real,+ g double precision,+ h smallserial,+ i serial,+ j bigserial,+ k int8,+ l serial8,+ m float8,+ n int,+ o int4,+ p float4,+ q int2,+ r serial2,+ s serial4,+ t decimal)+ want: |-+ CREATE TABLE "tech_book" (+ "a" smallint,+ "b" integer,+ "c" bigint,+ "d" numeric(10, 2),+ "e" numeric(4),+ "f" real,+ "g" double precision,+ "h" smallserial,+ "i" serial,+ "j" bigserial,+ "k" bigint,+ "l" bigserial,+ "m" double precision,+ "n" integer,+ "o" integer,+ "p" real,+ "q" smallint,+ "r" smallserial,+ "s" serial,+ "t" numeric+ )