Skip to content

Commit bd951bb

Browse files
committed
contrib/drivers/mariadb: add infrastructure, core, basic, and model tests
Align MariaDB test coverage with MySQL baseline (PR 1/6). - Extend init_test.go with db3, dbPrefix, dbInvalid, partition setup - Add basic_test.go (instance and connection tests) - Add core_test.go (core database operation tests) - Add model_test.go (non-conflicting model tests) ref gogf#4689
1 parent 6204c13 commit bd951bb

4 files changed

Lines changed: 4384 additions & 13 deletions

File tree

contrib/drivers/mariadb/mariadb_unit_init_test.go

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

1415
_ "github.com/gogf/gf/contrib/drivers/mariadb/v2"
15-
1616
"github.com/gogf/gf/v2/container/garray"
1717
"github.com/gogf/gf/v2/database/gdb"
1818
"github.com/gogf/gf/v2/frame/g"
@@ -21,18 +21,24 @@ import (
2121
)
2222

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

3235
var (
33-
db gdb.DB
34-
db2 gdb.DB
35-
ctx = context.TODO()
36+
db gdb.DB
37+
db2 gdb.DB
38+
db3 gdb.DB
39+
dbPrefix gdb.DB
40+
dbInvalid gdb.DB
41+
ctx = context.TODO()
3642
)
3743

3844
func init() {
@@ -41,10 +47,26 @@ func init() {
4147
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
4248
TranTimeout: time.Second * 3,
4349
}
44-
err := gdb.AddConfigNode(gdb.DefaultGroupName, nodeDefault)
45-
if err != nil {
46-
panic(err)
50+
partitionDefault := gdb.ConfigNode{
51+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
52+
Debug: true,
53+
TranTimeout: time.Second * 3,
54+
}
55+
nodePrefix := gdb.ConfigNode{
56+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
57+
TranTimeout: time.Second * 3,
58+
}
59+
nodePrefix.Prefix = TableNamePrefix1
60+
61+
nodeInvalid := gdb.ConfigNode{
62+
Link: fmt.Sprintf("mariadb:root:%s@tcp(127.0.0.1:3307)/?loc=Local&parseTime=true", TestDbPass),
63+
TranTimeout: time.Second * 3,
4764
}
65+
gdb.AddConfigNode("test", nodeDefault)
66+
gdb.AddConfigNode("prefix", nodePrefix)
67+
gdb.AddConfigNode("nodeinvalid", nodeInvalid)
68+
gdb.AddConfigNode("partition", partitionDefault)
69+
gdb.AddConfigNode(gdb.DefaultGroupName, nodeDefault)
4870

4971
// Default db.
5072
if r, err := gdb.NewByGroup(); err != nil {
@@ -59,8 +81,33 @@ func init() {
5981
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
6082
gtest.Error(err)
6183
}
84+
if _, err := db.Exec(ctx, fmt.Sprintf(schemaTemplate, TestPartitionDB)); err != nil {
85+
gtest.Error(err)
86+
}
6287
db = db.Schema(TestSchema1)
6388
db2 = db.Schema(TestSchema2)
89+
db3 = db.Schema(TestPartitionDB)
90+
// Prefix db.
91+
if r, err := gdb.NewByGroup("prefix"); err != nil {
92+
gtest.Error(err)
93+
} else {
94+
dbPrefix = r
95+
}
96+
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema1)); err != nil {
97+
gtest.Error(err)
98+
}
99+
if _, err := dbPrefix.Exec(ctx, fmt.Sprintf(schemaTemplate, TestSchema2)); err != nil {
100+
gtest.Error(err)
101+
}
102+
dbPrefix = dbPrefix.Schema(TestSchema1)
103+
104+
// Invalid db.
105+
if r, err := gdb.NewByGroup("nodeinvalid"); err != nil {
106+
gtest.Error(err)
107+
} else {
108+
dbInvalid = r
109+
}
110+
dbInvalid = dbInvalid.Schema(TestSchema1)
64111
}
65112

66113
func createTable(table ...string) string {
@@ -126,3 +173,61 @@ func dropTableWithDb(db gdb.DB, table string) {
126173
gtest.Error(err)
127174
}
128175
}
176+
177+
func Test_PartitionTable(t *testing.T) {
178+
dropShopDBTable()
179+
createShopDBTable()
180+
insertShopDBData()
181+
182+
// defer dropShopDBTable()
183+
gtest.C(t, func(t *gtest.T) {
184+
data, err := db3.Ctx(ctx).Model("dbx_order").Partition("p3", "p4").All()
185+
t.AssertNil(err)
186+
dataLen := len(data)
187+
t.Assert(dataLen, 5)
188+
data, err = db3.Ctx(ctx).Model("dbx_order").Partition("p3").All()
189+
t.AssertNil(err)
190+
dataLen = len(data)
191+
t.Assert(dataLen, 5)
192+
})
193+
}
194+
195+
func createShopDBTable() {
196+
sql := `CREATE TABLE dbx_order (
197+
id int(11) NOT NULL,
198+
sales_date date DEFAULT NULL,
199+
amount decimal(10,2) DEFAULT NULL
200+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
201+
PARTITION BY RANGE (YEAR(sales_date))
202+
(PARTITION p1 VALUES LESS THAN (2020) ENGINE = InnoDB,
203+
PARTITION p2 VALUES LESS THAN (2021) ENGINE = InnoDB,
204+
PARTITION p3 VALUES LESS THAN (2022) ENGINE = InnoDB,
205+
PARTITION p4 VALUES LESS THAN MAXVALUE ENGINE = InnoDB);`
206+
_, err := db3.Exec(ctx, sql)
207+
if err != nil {
208+
gtest.Fatal(err.Error())
209+
}
210+
}
211+
212+
func insertShopDBData() {
213+
data := g.Slice{}
214+
year := 2020
215+
for i := 1; i <= 5; i++ {
216+
year++
217+
data = append(data, g.Map{
218+
"id": i,
219+
"sales_date": fmt.Sprintf("%d-09-21", year),
220+
"amount": fmt.Sprintf("1%d.21", i),
221+
})
222+
}
223+
_, err := db3.Model("dbx_order").Ctx(ctx).Data(data).Insert()
224+
if err != nil {
225+
gtest.Error(err)
226+
}
227+
}
228+
229+
func dropShopDBTable() {
230+
if _, err := db3.Exec(ctx, "DROP TABLE IF EXISTS `dbx_order`"); err != nil {
231+
gtest.Error(err)
232+
}
233+
}
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)