Skip to content

Commit f2e8832

Browse files
committed
test(gendao): add PostgreSQL and GaussDB schema integration tests
Add integration tests for issue gogf#4495 to verify that gf gen dao works correctly with non-public schemas using search_path in connection string: - Test_Gen_Dao_Issue4495_PgSchema: PostgreSQL schema test - Test_Gen_Dao_Issue4495_GaussDBSchema: GaussDB schema test - Add testGaussDB connection in cmd_z_init_test.go Tests create tables in a custom schema and verify gen dao can generate correct dao/entity files when search_path is configured. Also update .gitignore to ignore *.out files (coverage output).
1 parent 46ef166 commit f2e8832

3 files changed

Lines changed: 189 additions & 6 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ node_modules
2525
output
2626
.example/
2727
.golangci.bck.yml
28-
*.exe
28+
*.exe
29+
*.out

cmd/gf/internal/cmd/cmd_z_init_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ import (
1515
)
1616

1717
var (
18-
ctx = context.Background()
19-
testDB gdb.DB
20-
testPgDB gdb.DB
21-
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
22-
linkPg = "pgsql:postgres:12345678@tcp(127.0.0.1:5432)/test"
18+
ctx = context.Background()
19+
testDB gdb.DB
20+
testPgDB gdb.DB
21+
testGaussDB gdb.DB
22+
link = "mysql:root:12345678@tcp(127.0.0.1:3306)/test?loc=Local&parseTime=true"
23+
linkPg = "pgsql:postgres:12345678@tcp(127.0.0.1:5432)/test"
24+
linkGaussDB = "gaussdb:gaussdb:UTpass@1234@tcp(127.0.0.1:9950)/postgres"
2325
)
2426

2527
func init() {
@@ -34,6 +36,10 @@ func init() {
3436
testPgDB, _ = gdb.New(gdb.ConfigNode{
3537
Link: linkPg,
3638
})
39+
// GaussDB connection (optional, may not be available in all environments)
40+
testGaussDB, _ = gdb.New(gdb.ConfigNode{
41+
Link: linkGaussDB,
42+
})
3743
}
3844

3945
func dropTableWithDb(db gdb.DB, table string) {

cmd/gf/internal/cmd/cmd_z_unit_gen_dao_issue_test.go

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,3 +855,179 @@ func Test_Gen_Dao_Issue4629_TablesPattern_PgSql(t *testing.T) {
855855
t.Assert(gfile.Exists(gfile.Join(path, "dao", "user_log.go")), false)
856856
})
857857
}
858+
859+
// https://github.com/gogf/gf/issues/4495
860+
// Test that gen dao works correctly with PostgreSQL non-public schemas
861+
// using search_path in connection string.
862+
func Test_Gen_Dao_Issue4495_PgSchema(t *testing.T) {
863+
if testPgDB == nil {
864+
t.Skip("PostgreSQL database not available, skipping test")
865+
return
866+
}
867+
gtest.C(t, func(t *gtest.T) {
868+
var (
869+
err error
870+
db = testPgDB
871+
schema = "test_gendao_schema"
872+
table1 = "schema_user"
873+
table2 = "schema_order"
874+
linkPgSP = fmt.Sprintf("pgsql:postgres:12345678@tcp(127.0.0.1:5432)/test?search_path=%s,public", schema)
875+
)
876+
877+
// Create schema
878+
if _, err = db.Exec(ctx, fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schema)); err != nil {
879+
t.Fatal(err)
880+
}
881+
defer db.Exec(ctx, fmt.Sprintf(`DROP SCHEMA %s CASCADE`, schema))
882+
883+
// Create tables in the schema (not in public)
884+
if _, err = db.Exec(ctx, fmt.Sprintf(`
885+
CREATE TABLE %s.%s (
886+
id bigserial PRIMARY KEY,
887+
name varchar(100)
888+
)`, schema, table1)); err != nil {
889+
t.Fatal(err)
890+
}
891+
892+
if _, err = db.Exec(ctx, fmt.Sprintf(`
893+
CREATE TABLE %s.%s (
894+
id bigserial PRIMARY KEY,
895+
user_id bigint,
896+
amount decimal(10,2)
897+
)`, schema, table2)); err != nil {
898+
t.Fatal(err)
899+
}
900+
901+
var (
902+
path = gfile.Temp(guid.S())
903+
group = "test"
904+
in = gendao.CGenDaoInput{
905+
Path: path,
906+
Link: linkPgSP, // Use connection string with search_path
907+
Group: group,
908+
Tables: fmt.Sprintf("%s,%s", table1, table2), // Specify exact tables
909+
}
910+
)
911+
err = gutil.FillStructWithDefault(&in)
912+
t.AssertNil(err)
913+
914+
err = gfile.Mkdir(path)
915+
t.AssertNil(err)
916+
917+
pwd := gfile.Pwd()
918+
err = gfile.Chdir(path)
919+
t.AssertNil(err)
920+
defer gfile.Chdir(pwd)
921+
defer gfile.RemoveAll(path)
922+
923+
_, err = gendao.CGenDao{}.Dao(ctx, in)
924+
t.AssertNil(err)
925+
926+
// Should generate 2 dao files from the schema
927+
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
928+
t.AssertNil(err)
929+
t.Assert(len(generatedFiles), 2)
930+
931+
// Verify the correct files are generated
932+
t.Assert(gfile.Exists(gfile.Join(path, "dao", "schema_user.go")), true)
933+
t.Assert(gfile.Exists(gfile.Join(path, "dao", "schema_order.go")), true)
934+
935+
// Verify entity files contain correct field types
936+
entityUserContent := gfile.GetContents(gfile.Join(path, "model", "entity", "schema_user.go"))
937+
t.Assert(gstr.Contains(entityUserContent, "Id"), true)
938+
t.Assert(gstr.Contains(entityUserContent, "Name"), true)
939+
940+
entityOrderContent := gfile.GetContents(gfile.Join(path, "model", "entity", "schema_order.go"))
941+
t.Assert(gstr.Contains(entityOrderContent, "Id"), true)
942+
t.Assert(gstr.Contains(entityOrderContent, "UserId"), true)
943+
t.Assert(gstr.Contains(entityOrderContent, "Amount"), true)
944+
})
945+
}
946+
947+
// https://github.com/gogf/gf/issues/4495
948+
// Test that gen dao works correctly with GaussDB non-public schemas
949+
// using search_path in connection string.
950+
func Test_Gen_Dao_Issue4495_GaussDBSchema(t *testing.T) {
951+
if testGaussDB == nil {
952+
t.Skip("GaussDB database not available, skipping test")
953+
return
954+
}
955+
gtest.C(t, func(t *gtest.T) {
956+
var (
957+
err error
958+
db = testGaussDB
959+
schema = "test_gendao_schema"
960+
table1 = "schema_user"
961+
table2 = "schema_order"
962+
linkGaussDBSP = fmt.Sprintf("gaussdb:gaussdb:UTpass@1234@tcp(127.0.0.1:9950)/postgres?search_path=%s,public", schema)
963+
)
964+
965+
// Create schema
966+
if _, err = db.Exec(ctx, fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schema)); err != nil {
967+
t.Fatal(err)
968+
}
969+
defer db.Exec(ctx, fmt.Sprintf(`DROP SCHEMA %s CASCADE`, schema))
970+
971+
// Create tables in the schema (not in public)
972+
if _, err = db.Exec(ctx, fmt.Sprintf(`
973+
CREATE TABLE %s.%s (
974+
id bigserial PRIMARY KEY,
975+
name varchar(100)
976+
)`, schema, table1)); err != nil {
977+
t.Fatal(err)
978+
}
979+
980+
if _, err = db.Exec(ctx, fmt.Sprintf(`
981+
CREATE TABLE %s.%s (
982+
id bigserial PRIMARY KEY,
983+
user_id bigint,
984+
amount decimal(10,2)
985+
)`, schema, table2)); err != nil {
986+
t.Fatal(err)
987+
}
988+
989+
var (
990+
path = gfile.Temp(guid.S())
991+
group = "test"
992+
in = gendao.CGenDaoInput{
993+
Path: path,
994+
Link: linkGaussDBSP, // Use connection string with search_path
995+
Group: group,
996+
Tables: fmt.Sprintf("%s,%s", table1, table2), // Specify exact tables
997+
}
998+
)
999+
err = gutil.FillStructWithDefault(&in)
1000+
t.AssertNil(err)
1001+
1002+
err = gfile.Mkdir(path)
1003+
t.AssertNil(err)
1004+
1005+
pwd := gfile.Pwd()
1006+
err = gfile.Chdir(path)
1007+
t.AssertNil(err)
1008+
defer gfile.Chdir(pwd)
1009+
defer gfile.RemoveAll(path)
1010+
1011+
_, err = gendao.CGenDao{}.Dao(ctx, in)
1012+
t.AssertNil(err)
1013+
1014+
// Should generate 2 dao files from the schema
1015+
generatedFiles, err := gfile.ScanDir(gfile.Join(path, "dao"), "*.go", false)
1016+
t.AssertNil(err)
1017+
t.Assert(len(generatedFiles), 2)
1018+
1019+
// Verify the correct files are generated
1020+
t.Assert(gfile.Exists(gfile.Join(path, "dao", "schema_user.go")), true)
1021+
t.Assert(gfile.Exists(gfile.Join(path, "dao", "schema_order.go")), true)
1022+
1023+
// Verify entity files contain correct field types
1024+
entityUserContent := gfile.GetContents(gfile.Join(path, "model", "entity", "schema_user.go"))
1025+
t.Assert(gstr.Contains(entityUserContent, "Id"), true)
1026+
t.Assert(gstr.Contains(entityUserContent, "Name"), true)
1027+
1028+
entityOrderContent := gfile.GetContents(gfile.Join(path, "model", "entity", "schema_order.go"))
1029+
t.Assert(gstr.Contains(entityOrderContent, "Id"), true)
1030+
t.Assert(gstr.Contains(entityOrderContent, "UserId"), true)
1031+
t.Assert(gstr.Contains(entityOrderContent, "Amount"), true)
1032+
})
1033+
}

0 commit comments

Comments
 (0)