Files
gf/contrib/drivers/pgsql/pgsql_z_unit_feature_union_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

147 lines
3.3 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 (
"testing"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_Union(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
r, err := db.Union(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").All()
t.AssertNil(err)
t.Assert(len(r), 3)
t.Assert(r[0]["id"], 3)
t.Assert(r[1]["id"], 2)
t.Assert(r[2]["id"], 1)
})
gtest.C(t, func(t *gtest.T) {
r, err := db.Union(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").One()
t.AssertNil(err)
t.Assert(r["id"], 3)
})
}
func Test_UnionAll(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
r, err := db.UnionAll(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").All()
t.AssertNil(err)
t.Assert(len(r), 5)
t.Assert(r[0]["id"], 3)
t.Assert(r[1]["id"], 2)
t.Assert(r[2]["id"], 2)
t.Assert(r[3]["id"], 1)
t.Assert(r[4]["id"], 1)
})
gtest.C(t, func(t *gtest.T) {
r, err := db.UnionAll(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").One()
t.AssertNil(err)
t.Assert(r["id"], 3)
})
}
func Test_Model_Union(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table).Union(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").All()
t.AssertNil(err)
t.Assert(len(r), 3)
t.Assert(r[0]["id"], 3)
t.Assert(r[1]["id"], 2)
t.Assert(r[2]["id"], 1)
})
gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table).Union(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").One()
t.AssertNil(err)
t.Assert(r["id"], 3)
})
}
func Test_Model_UnionAll(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table).UnionAll(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").All()
t.AssertNil(err)
t.Assert(len(r), 5)
t.Assert(r[0]["id"], 3)
t.Assert(r[1]["id"], 2)
t.Assert(r[2]["id"], 2)
t.Assert(r[3]["id"], 1)
t.Assert(r[4]["id"], 1)
})
gtest.C(t, func(t *gtest.T) {
r, err := db.Model(table).UnionAll(
db.Model(table).Where("id", 1),
db.Model(table).Where("id", 2),
db.Model(table).WhereIn("id", g.Slice{1, 2, 3}).OrderDesc("id"),
).OrderDesc("id").One()
t.AssertNil(err)
t.Assert(r["id"], 3)
})
}