Skip to content

Commit 68b0221

Browse files
test(contrib/drivers/mariadb): add infrastructure, core and model tests (gogf#4719)
## Summary - Add full test infrastructure (`mariadb_unit_init_test.go`) with MariaDB-specific helpers (createTable, createInitTable, dropTable) matching the MySQL test baseline - Port 4 basic tests from MySQL: `Test_New`, `Test_DB_Ping`, `Test_DB_Query`, `Test_DB_Exec` - Port 47 core tests covering CRUD operations, raw SQL, schema switching, and DB/TX method parity - Port 55 model tests covering Model API: Fields, Where, Scan, Save, Replace, InsertIgnore, InsertGetId, OmitEmpty, Distinct, Count/Min/Max/Avg/Sum, HasField, chained operations, testdata SQL-based scenarios and more - Add 5 testdata SQL files required by model tests (copied from MySQL baseline) All tests are structurally identical to the MySQL driver baseline. SQL syntax is standard and shared. Package and import references are adapted for MariaDB. ref gogf#4689 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 766579d commit 68b0221

8 files changed

Lines changed: 4413 additions & 15 deletions

contrib/drivers/mariadb/mariadb_unit_init_test.go

Lines changed: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package mariadb_test
99
import (
1010
"context"
1111
"fmt"
12+
"testing"
1213
"time"
1314

1415
_ "github.com/gogf/gf/contrib/drivers/mariadb/v2"
@@ -21,17 +22,22 @@ import (
2122
)
2223

2324
const (
24-
TableSize = 10
25-
TableName = "user"
26-
TestSchema1 = "test1"
27-
TestSchema2 = "test2"
28-
TestDbPass = "12345678"
29-
CreateTime = "2018-10-24 10:00:00"
25+
TableSize = 10
26+
TableName = "user"
27+
TestSchema1 = "test1"
28+
TestSchema2 = "test2"
29+
TestPartitionDB = "test3"
30+
TableNamePrefix1 = "gf_"
31+
TestDbUser = "root"
32+
TestDbPass = "12345678"
33+
CreateTime = "2018-10-24 10:00:00"
3034
)
3135

3236
var (
3337
db gdb.DB
3438
db2 gdb.DB
39+
db3 gdb.DB
40+
dbPrefix gdb.DB
3541
dbInvalid gdb.DB
3642
ctx = context.TODO()
3743
)
@@ -42,10 +48,26 @@ func init() {
4248
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
4349
TranTimeout: time.Second * 3,
4450
}
45-
err := gdb.AddConfigNode(gdb.DefaultGroupName, nodeDefault)
46-
if err != nil {
47-
panic(err)
51+
partitionDefault := gdb.ConfigNode{
52+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
53+
Debug: true,
54+
TranTimeout: time.Second * 3,
55+
}
56+
nodePrefix := gdb.ConfigNode{
57+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
58+
TranTimeout: time.Second * 3,
59+
}
60+
nodePrefix.Prefix = TableNamePrefix1
61+
62+
nodeInvalid := gdb.ConfigNode{
63+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3317)/?loc=Local&parseTime=true", TestDbPass),
64+
TranTimeout: time.Second * 3,
4865
}
66+
gdb.AddConfigNode("test", nodeDefault)
67+
gdb.AddConfigNode("prefix", nodePrefix)
68+
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
69+
gdb.AddConfigNode("partition", partitionDefault)
70+
gdb.AddConfigNode(gdb.DefaultGroupName, nodeDefault)
4971

5072
// Default db.
5173
if r, err := gdb.NewByGroup(); err != nil {
@@ -60,15 +82,27 @@ func init() {
6082
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
6183
gtest.Error(err)
6284
}
85+
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestPartitionDB)); err != nil {
86+
gtest.Error(err)
87+
}
6388
db = db.Schema(TestSchema1)
6489
db2 = db.Schema(TestSchema2)
65-
66-
// Invalid db (wrong port for testing error handling).
67-
nodeInvalid := gdb.ConfigNode{
68-
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3317)/?loc=Local&parseTime=true", TestDbPass),
69-
TranTimeout: time.Second * 3,
90+
db3 = db.Schema(TestPartitionDB)
91+
// Prefix db.
92+
if r, err := gdb.NewByGroup("prefix"); err != nil {
93+
gtest.Error(err)
94+
} else {
95+
dbPrefix = r
7096
}
71-
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
97+
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
98+
gtest.Error(err)
99+
}
100+
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
101+
gtest.Error(err)
102+
}
103+
dbPrefix = dbPrefix.Schema(TestSchema1)
104+
105+
// Invalid db.
72106
if r, err := gdb.NewByGroup("nodeinvalid"); err != nil {
73107
gtest.Error(err)
74108
} else {
@@ -140,3 +174,61 @@ func dropTableWithDb(db gdb.DB, table string) {
140174
gtest.Error(err)
141175
}
142176
}
177+
178+
func Test_PartitionTable(t *testing.T) {
179+
dropShopDBTable()
180+
createShopDBTable()
181+
insertShopDBData()
182+
183+
// defer dropShopDBTable()
184+
gtest.C(t, func(t *gtest.T) {
185+
data, err := db3.Ctx(ctx).Model("dbx_order").Partition("p3", "p4").All()
186+
t.AssertNil(err)
187+
dataLen := len(data)
188+
t.Assert(dataLen, 5)
189+
data, err = db3.Ctx(ctx).Model("dbx_order").Partition("p3").All()
190+
t.AssertNil(err)
191+
dataLen = len(data)
192+
t.Assert(dataLen, 5)
193+
})
194+
}
195+
196+
func createShopDBTable() {
197+
sql := `CREATE TABLE dbx_order (
198+
id int(11) NOT NULL,
199+
sales_date date DEFAULT NULL,
200+
amount decimal(10,2) DEFAULT NULL
201+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
202+
PARTITION BY RANGE (YEAR(sales_date))
203+
(PARTITION p1 VALUES LESS THAN (2020) ENGINE = InnoDB,
204+
PARTITION p2 VALUES LESS THAN (2021) ENGINE = InnoDB,
205+
PARTITION p3 VALUES LESS THAN (2022) ENGINE = InnoDB,
206+
PARTITION p4 VALUES LESS THAN MAXVALUE ENGINE = InnoDB);`
207+
_, err := db3.Exec(ctx, sql)
208+
if err != nil {
209+
gtest.Fatal(err.Error())
210+
}
211+
}
212+
213+
func insertShopDBData() {
214+
data := g.Slice{}
215+
year := 2020
216+
for i := 1; i <= 5; i++ {
217+
year++
218+
data = append(data, g.Map{
219+
"id": i,
220+
"sales_date": fmt.Sprintf("%d-09-21", year),
221+
"amount": fmt.Sprintf("1%d.21", i),
222+
})
223+
}
224+
_, err := db3.Model("dbx_order").Ctx(ctx).Data(data).Insert()
225+
if err != nil {
226+
gtest.Error(err)
227+
}
228+
}
229+
230+
func dropShopDBTable() {
231+
if _, err := db3.Exec(ctx, "DROP TABLE IF EXISTS `dbx_order`"); err != nil {
232+
gtest.Error(err)
233+
}
234+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2+
//
3+
// This Source Code Form is subject to the terms of the MIT License.
4+
// If a copy of the MIT was not distributed with this file,
5+
// You can obtain one at https://github.com/gogf/gf.
6+
7+
package mariadb_test
8+
9+
import (
10+
"context"
11+
"testing"
12+
13+
"github.com/gogf/gf/v2/database/gdb"
14+
"github.com/gogf/gf/v2/test/gtest"
15+
)
16+
17+
func Test_Instance(t *testing.T) {
18+
gtest.C(t, func(t *gtest.T) {
19+
_, err := gdb.Instance("none")
20+
t.AssertNE(err, nil)
21+
22+
db, err := gdb.Instance()
23+
t.AssertNil(err)
24+
25+
err1 := db.PingMaster()
26+
err2 := db.PingSlave()
27+
t.Assert(err1, nil)
28+
t.Assert(err2, nil)
29+
})
30+
}
31+
32+
func Test_Func_FormatSqlWithArgs(t *testing.T) {
33+
// mysql
34+
gtest.C(t, func(t *gtest.T) {
35+
var s string
36+
s = gdb.FormatSqlWithArgs("select * from table where id>=? and sex=?", []any{100, 1})
37+
t.Assert(s, "select * from table where id>=100 and sex=1")
38+
})
39+
// mssql
40+
gtest.C(t, func(t *gtest.T) {
41+
var s string
42+
s = gdb.FormatSqlWithArgs("select * from table where id>=@p1 and sex=@p2", []any{100, 1})
43+
t.Assert(s, "select * from table where id>=100 and sex=1")
44+
})
45+
// pgsql
46+
gtest.C(t, func(t *gtest.T) {
47+
var s string
48+
s = gdb.FormatSqlWithArgs("select * from table where id>=$1 and sex=$2", []any{100, 1})
49+
t.Assert(s, "select * from table where id>=100 and sex=1")
50+
})
51+
// oracle
52+
gtest.C(t, func(t *gtest.T) {
53+
var s string
54+
s = gdb.FormatSqlWithArgs("select * from table where id>=:v1 and sex=:v2", []any{100, 1})
55+
t.Assert(s, "select * from table where id>=100 and sex=1")
56+
})
57+
}
58+
59+
func Test_Func_ToSQL(t *testing.T) {
60+
gtest.C(t, func(t *gtest.T) {
61+
sql, err := gdb.ToSQL(ctx, func(ctx context.Context) error {
62+
value, err := db.Ctx(ctx).Model(TableName).Fields("nickname").Where("id", 1).Value()
63+
t.Assert(value, nil)
64+
return err
65+
})
66+
t.AssertNil(err)
67+
t.Assert(sql, "SELECT `nickname` FROM `user` WHERE `id`=1 LIMIT 1")
68+
})
69+
}
70+
71+
func Test_Func_CatchSQL(t *testing.T) {
72+
table := createInitTable()
73+
defer dropTable(table)
74+
gtest.C(t, func(t *gtest.T) {
75+
array, err := gdb.CatchSQL(ctx, func(ctx context.Context) error {
76+
value, err := db.Ctx(ctx).Model(table).Fields("nickname").Where("id", 1).Value()
77+
t.Assert(value, "name_1")
78+
return err
79+
})
80+
t.AssertNil(err)
81+
t.AssertGE(len(array), 1)
82+
})
83+
}

0 commit comments

Comments
 (0)