Skip to content

Commit ff23f33

Browse files
committed
Merge branch 'pref_add_uint_test' into dev
# Conflicts: # demo/todo/tests/todo_test.go
2 parents 9023bd2 + 7d0352a commit ff23f33

File tree

9 files changed

+401
-211
lines changed

9 files changed

+401
-211
lines changed

demo/todo/go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ require (
99
github.com/iancoleman/orderedmap v0.2.0
1010
github.com/samber/lo v1.33.0
1111
github.com/smartystreets/goconvey v1.7.2
12-
github.com/stretchr/testify v1.8.0
1312
)
1413

1514
require (
1615
github.com/BurntSushi/toml v1.1.0 // indirect
1716
github.com/cespare/xxhash/v2 v2.1.2 // indirect
1817
github.com/clbanning/mxj/v2 v2.5.5 // indirect
19-
github.com/davecgh/go-spew v1.1.1 // indirect
2018
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
2119
github.com/fatih/color v1.13.0 // indirect
2220
github.com/fsnotify/fsnotify v1.5.4 // indirect
@@ -33,7 +31,6 @@ require (
3331
github.com/mattn/go-isatty v0.0.14 // indirect
3432
github.com/mattn/go-runewidth v0.0.9 // indirect
3533
github.com/olekukonko/tablewriter v0.0.5 // indirect
36-
github.com/pmezard/go-difflib v1.0.0 // indirect
3734
github.com/smartystreets/assertions v1.2.0 // indirect
3835
go.opentelemetry.io/otel v1.7.0 // indirect
3936
go.opentelemetry.io/otel/sdk v1.7.0 // indirect

demo/todo/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ github.com/smartystreets/assertions v1.2.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYl
103103
github.com/smartystreets/goconvey v1.7.2 h1:9RBaZCeXEQ3UselpuwUQHltGVXvdwm6cv1hgR6gDIPg=
104104
github.com/smartystreets/goconvey v1.7.2/go.mod h1:Vw0tHAZW6lzCRk3xgdin6fKYcG+G3Pg9vgXWeJpQFMM=
105105
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
106-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
107106
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
108107
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
109108
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
110109
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
111-
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
112110
github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M=
113111
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
114112
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=

demo/todo/tests/access_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"github.com/gogf/gf/v2/frame/g"
6+
. "github.com/smartystreets/goconvey/convey"
7+
"testing"
8+
"time"
9+
)
10+
11+
// TestAccessExt 字段RightLike查询
12+
func TestAccessExtFiledRightLike(t *testing.T) {
13+
Convey("AccessExt", t, func() {
14+
15+
rows, err := g.DB().Model("t_todo").All()
16+
So(err, ShouldBeNil)
17+
18+
const layout = "2006-01-02"
19+
20+
var createdAtList = map[string]int{}
21+
for _, row := range rows {
22+
createdAt := row["created_at"]
23+
if createdAt.IsNil() {
24+
continue
25+
}
26+
27+
createdAtList[createdAt.Time().Format(layout)]++
28+
}
29+
30+
outByDate := func(date string) g.Map {
31+
req := fmt.Sprintf(`
32+
{
33+
"[]":{
34+
"User":{
35+
"userId@":"/Todo/userId"
36+
},
37+
"Todo":{
38+
"@role":"PARTNER",
39+
"createdAt$":"%s%%"
40+
}
41+
}
42+
}
43+
`, date)
44+
45+
out, err := queryByJsonStr(req)
46+
So(err, ShouldBeNil)
47+
return out
48+
}
49+
50+
// 制造无效数据
51+
now := time.Now()
52+
for i := 100; i < 1000; i++ {
53+
date := now.AddDate(0, 0, -i).Format(layout)
54+
if _, ok := createdAtList[date]; !ok {
55+
createdAtList[date] = 0
56+
break
57+
}
58+
}
59+
60+
for date, cnt := range createdAtList {
61+
out := outByDate(date)
62+
list, ok := out["[]"].([]g.Map)
63+
64+
// 默认分页:limit 0,10
65+
if cnt > 10 {
66+
cnt = 10
67+
}
68+
69+
if ok {
70+
So(len(list), ShouldEqual, cnt)
71+
} else {
72+
So(out["[]"], ShouldBeEmpty)
73+
}
74+
}
75+
})
76+
}

demo/todo/tests/config_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ func init() {
2525
config.AccessConditionFunc = app.AccessCondition
2626
config.AccessVerify = false // 全局配置验证权限开关
2727

28-
g.DB().SetLogger(g.Log())
29-
3028
g.Log().SetLevelStr("all")
3129
//g.Log().SetLevelStr("info") // 需要显示debug时将本句注释即可
3230

31+
logger := g.Log("db") // 使用独立的Logger控制sql日志
32+
logger.SetLevelStr("info") // 不打印db.Init初始化的日志
33+
g.DB().SetLogger(logger)
34+
3335
db.Init()
3436

37+
logger.SetLevelStr("all")
38+
g.DB().SetLogger(logger)
39+
3540
config.SetDbFieldStyle(config.CaseSnake)
3641
config.SetJsonFieldStyle(config.CaseCamel)
3742
}

demo/todo/tests/field_style_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@ func hasKey(m g.Map, k string) bool {
1515
}
1616

1717
func TestFieldStyle(t *testing.T) {
18-
1918
oriDbStyle := config.GetDbFieldStyle()
2019
oriJsonStyle := config.GetJsonFieldStyle()
20+
defer func() {
21+
config.SetDbFieldStyle(oriDbStyle)
22+
config.SetJsonFieldStyle(oriJsonStyle)
23+
}()
2124

22-
Convey("TestFieldStyle", t, func() {
25+
// 手动临时启用
26+
SkipConvey("TestFieldStyle", t, func() {
2327
// ============================================================
2428
Convey("json use CaseCamel, db use CaseSnake", func() {
25-
config.SetJsonFieldStyle(config.CaseCamel)
26-
config.SetDbFieldStyle(config.CaseSnake)
2729

2830
req := `
2931
{
@@ -77,7 +79,4 @@ func TestFieldStyle(t *testing.T) {
7779
So(todo["user_id"] == "10001", ShouldBeTrue)
7880
})
7981
})
80-
81-
config.SetDbFieldStyle(oriDbStyle)
82-
config.SetDbFieldStyle(oriJsonStyle)
8382
}

demo/todo/tests/query_ref_test.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"github.com/gogf/gf/v2/frame/g"
6+
. "github.com/smartystreets/goconvey/convey"
7+
"testing"
8+
)
9+
10+
// TestTodoWithUser 两表关联查询
11+
func TestTodoWithUser(t *testing.T) {
12+
Convey("TodoWithUser", t, func() {
13+
req := `
14+
{
15+
"Todo": {
16+
17+
},
18+
"User":{
19+
"userId@":"Todo/userId"
20+
}
21+
}
22+
`
23+
out, err := queryByJsonStr(req)
24+
So(err, ShouldBeNil)
25+
26+
todo, user := out["Todo"].(g.Map), out["User"].(g.Map)
27+
So(todo, ShouldNotBeNil)
28+
So(user, ShouldNotBeNil)
29+
30+
So(user["userId"], ShouldNotBeEmpty)
31+
32+
fmt.Println()
33+
row, err := g.DB().Model("t_user").Where("user_id", user["userId"]).One()
34+
So(err, ShouldBeNil)
35+
36+
//field := config.GetDbFieldStyle()(ctx, "t_todo", "userId")
37+
So(todo["userId"], ShouldEqual, row["user_id"].String())
38+
})
39+
}
40+
41+
// TestTodoListWithUser 两表关联查询
42+
func TestTodoListWithUser(t *testing.T) {
43+
Convey("", t, func() {
44+
req := `
45+
{
46+
"[]": {
47+
"Todo": {
48+
49+
},
50+
"User": {
51+
"userId@": "[]/Todo/userId"
52+
}
53+
}
54+
}
55+
`
56+
out, err := queryByJsonStr(req)
57+
So(err, ShouldBeNil)
58+
59+
data := out["[]"]
60+
So(data, ShouldNotBeNil)
61+
list := data.([]g.Map)
62+
63+
for _, item := range list {
64+
todo, user := item["Todo"].(g.Map), item["User"].(g.Map)
65+
So(todo, ShouldNotBeNil)
66+
So(user, ShouldNotBeNil)
67+
68+
So(user["userId"], ShouldNotBeEmpty)
69+
So(user["userId"], ShouldEqual, todo["userId"])
70+
}
71+
fmt.Println()
72+
73+
})
74+
}
75+
76+
// TestTodoListByUser 两表关联查询
77+
func TestTodoListByUser(t *testing.T) {
78+
Convey("TodoListByUser", t, func() {
79+
req := `
80+
{
81+
"User": {
82+
83+
},
84+
"[]": {
85+
"Todo": {
86+
"userId@": "User/userId"
87+
}
88+
}
89+
}
90+
`
91+
out, err := queryByJsonStr(req)
92+
So(err, ShouldBeNil)
93+
94+
data, user := out["[]"], out["User"]
95+
So(data, ShouldNotBeNil)
96+
So(user, ShouldNotBeNil)
97+
98+
userId := user.(g.Map)["userId"]
99+
list := data.([]g.Map)
100+
So(len(list), ShouldBeGreaterThan, 0)
101+
102+
for _, item := range list {
103+
row := item["Todo"].(g.Map)
104+
So(row["userId"], ShouldEqual, userId)
105+
}
106+
})
107+
}
108+
109+
// TestTodoRef 两表关联查询
110+
func TestTodoRef(t *testing.T) {
111+
Convey("TodoRef", t, func() {
112+
req := `
113+
{
114+
"Todo": {
115+
"@column": "id,userId",
116+
"userId@":"User/userId"
117+
},
118+
"User": {
119+
"@column": "userId"
120+
},
121+
"[]": {
122+
"Todo": {
123+
"userId@": "Todo/userId",
124+
"@column": "id,userId"
125+
},
126+
"User": {
127+
"user_id@": "/Todo/userId",
128+
"@column": "userId"
129+
}
130+
}
131+
}
132+
`
133+
out, err := queryByJsonStr(req)
134+
So(err, ShouldBeNil)
135+
136+
{
137+
todo := out["Todo"]
138+
So(todo, ShouldNotBeNil)
139+
140+
row := todo.(g.Map)
141+
So(len(row), ShouldEqual, 2)
142+
So(hasKey(row, "id"), ShouldBeTrue)
143+
So(hasKey(row, "userId"), ShouldBeTrue)
144+
}
145+
146+
{
147+
user := out["User"]
148+
So(user, ShouldNotBeNil)
149+
150+
row := user.(g.Map)
151+
So(len(row), ShouldEqual, 1)
152+
So(hasKey(row, "userId"), ShouldBeTrue)
153+
}
154+
155+
{
156+
data := out["[]"]
157+
list := data.([]g.Map)
158+
So(len(list), ShouldBeGreaterThan, 0)
159+
160+
for _, item := range list {
161+
todo := item["Todo"]
162+
So(hasKey(item, "Todo"), ShouldBeTrue)
163+
So(len(item), ShouldEqual, 1)
164+
165+
row := todo.(g.Map)
166+
167+
So(len(row), ShouldEqual, 2)
168+
So(hasKey(row, "id"), ShouldBeTrue)
169+
So(hasKey(row, "userId"), ShouldBeTrue)
170+
}
171+
}
172+
})
173+
174+
}
175+
176+
// TestTodoOneMany 列表中一对多
177+
func TestTodoOneMany(t *testing.T) {
178+
Convey("TodoOneMany", t, func() {
179+
180+
req := `
181+
{
182+
"[]":{
183+
"User":{
184+
185+
},
186+
"Todo[]":{
187+
"userId@":"/User/userId"
188+
}
189+
}
190+
}
191+
`
192+
out, err := queryByJsonStr(req)
193+
So(err, ShouldBeNil)
194+
So(len(out), ShouldEqual, 1)
195+
196+
So(hasKey(out, "[]"), ShouldBeTrue)
197+
list := out["[]"].([]g.Map)
198+
So(len(list), ShouldBeGreaterThan, 0)
199+
200+
for _, item := range list {
201+
So(len(item), ShouldBeGreaterThan, 0)
202+
203+
So(hasKey(item, "User"), ShouldBeTrue)
204+
user := item["User"].(g.Map)
205+
userId := user["userId"]
206+
So(userId, ShouldNotBeNil)
207+
208+
if hasKey(item, "Todo[]") {
209+
todoList := item["Todo[]"].([]g.Map)
210+
So(len(todoList), ShouldBeGreaterThan, 0)
211+
212+
for _, todo := range todoList {
213+
So(todo["userId"], ShouldEqual, userId)
214+
}
215+
}
216+
}
217+
})
218+
}

0 commit comments

Comments
 (0)