Files
gf/contrib/drivers/pgsql/pgsql_z_unit_raw_test.go
Jack Ling 46cc4cef9e test(contrib/drivers/pgsql): add Union, DO and Raw Where test coverage (#4679)
## Summary
- Add `pgsql_z_unit_feature_union_test.go`: 4 tests for Union/UnionAll
on both db and model level
- Add `pgsql_z_unit_feature_model_do_test.go`: 10 tests for DO (Data
Object) pattern - insert, batch insert, update, pointer fields, WHERE,
DAO pattern, and field prefix handling
- Enhance `pgsql_z_unit_raw_test.go`: add `Test_Raw_Where` for subquery
NOT EXISTS and field comparison using `gdb.Raw()`, adapted for PgSQL
double-quote quoting
- Add `testdata/table_with_prefix.sql` for PgSQL-compatible FieldPrefix
test

All tests adapted from MySQL driver test suite with PgSQL-specific
adjustments:
- Nullable table schema for DO partial inserts (PgSQL NOT NULL is
stricter than MySQL)
- Double-quote identifier quoting instead of backticks
- Unquoted table aliases in generated SQL

## Test plan
- [x] All 15 new tests pass locally
- [x] Full pgsql test suite (107 tests) passes with zero regressions

ref #4689

---------

Co-authored-by: John Guo <claymore1986@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-02-11 14:41:42 +08:00

143 lines
3.7 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package pgsql_test
import (
"context"
"testing"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_Raw_Insert(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Model(table)
result, err := user.Data(g.Map{
"passport": "port_1",
"password": "pass_1",
"nickname": "name_1",
"create_time": gdb.Raw("now()"),
}).Insert()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
}
func Test_Raw_BatchInsert(t *testing.T) {
table := createTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Model(table)
result, err := user.Data(
g.List{
g.Map{
"passport": "port_2",
"password": "pass_2",
"nickname": "name_2",
"create_time": gdb.Raw("now()"),
},
g.Map{
"passport": "port_4",
"password": "pass_4",
"nickname": "name_4",
"create_time": gdb.Raw("now()"),
},
},
).Insert()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 2)
})
}
func Test_Raw_Delete(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Model(table)
result, err := user.Data(g.Map{
"id": gdb.Raw("id"),
}).Where("id", 1).Delete()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
}
func Test_Raw_Update(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
user := db.Model(table)
result, err := user.Data(g.Map{
"id": gdb.Raw("id+100"),
"create_time": gdb.Raw("now()"),
}).Where("id", 1).Update()
t.AssertNil(err)
n, _ := result.RowsAffected()
t.Assert(n, 1)
})
gtest.C(t, func(t *gtest.T) {
user := db.Model(table)
n, err := user.Where("id", 101).Count()
t.AssertNil(err)
t.Assert(n, int64(1))
})
}
func Test_Raw_Where(t *testing.T) {
table1 := createTable("test_raw_where_table1")
table2 := createTable("test_raw_where_table2")
defer dropTable(table1)
defer dropTable(table2)
// https://github.com/gogf/gf/issues/3922
gtest.C(t, func(t *gtest.T) {
expectSql := `SELECT * FROM "test_raw_where_table1" AS A WHERE NOT EXISTS (SELECT B.id FROM "test_raw_where_table2" AS B WHERE "B"."id"=A.id) LIMIT 1`
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where("B.id", gdb.Raw("A.id"))
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
_, err := m.All()
return err
})
t.AssertNil(err)
t.Assert(expectSql, sql)
})
gtest.C(t, func(t *gtest.T) {
expectSql := `SELECT * FROM "test_raw_where_table1" AS A WHERE NOT EXISTS (SELECT B.id FROM "test_raw_where_table2" AS B WHERE B.id=A.id) LIMIT 1`
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
s := db.Model(table2).As("B").Ctx(ctx).Fields("B.id").Where(gdb.Raw("B.id=A.id"))
m := db.Model(table1).As("A").Ctx(ctx).Where("NOT EXISTS ?", s).Limit(1)
_, err := m.All()
return err
})
t.AssertNil(err)
t.Assert(expectSql, sql)
})
// https://github.com/gogf/gf/issues/3915
gtest.C(t, func(t *gtest.T) {
expectSql := `SELECT * FROM "test_raw_where_table1" WHERE "passport" < "nickname"`
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
m := db.Model(table1).Ctx(ctx).WhereLT("passport", gdb.Raw(`"nickname"`))
_, err := m.All()
return err
})
t.AssertNil(err)
t.Assert(expectSql, sql)
})
}