Skip to content

Commit 9023bd2

Browse files
committed
feat(action): 增加 version支持 + action的tests
1 parent 2be58f2 commit 9023bd2

File tree

8 files changed

+321
-10
lines changed

8 files changed

+321
-10
lines changed

action/action.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func New(ctx context.Context, method string, req g.Map) *Action {
4848
}
4949

5050
delete(req, "tag")
51+
delete(req, "version")
5152

5253
a := &Action{
5354
ctx: ctx,
@@ -92,7 +93,7 @@ func (a *Action) parse() error {
9293
if ok { // 将所有node都假设成列表, 如果单个则看成一个元素的批量
9394
list = []g.Map{_v}
9495
} else {
95-
list = v.([]g.Map)
96+
list = gconv.SliceMap(v)
9697
}
9798

9899
node := newNode(key, list, structure)

action/node.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,11 @@ func (n *Node) do(ctx context.Context, method string, i int) (ret g.Map, err err
285285
}
286286

287287
if len(n.Data) > 0 { //多条插入时返回值已经应该无意义了
288+
289+
jsonStyle := config.GetJsonFieldStyle()
288290
if rowKeyVal != nil {
289291
for k, v := range rowKeyVal {
290-
ret[k] = v
292+
ret[jsonStyle(ctx, n.TableName, k)] = v
291293
}
292294
}
293295
}

action/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ func checkTag(req g.Map, method string) (*db.Request, error) {
1515
}
1616

1717
tag := gconv.String(_tag)
18+
version := req["version"]
1819

19-
request, err := db.GetRequest(tag, method, -1)
20+
request, err := db.GetRequest(tag, method, gconv.String(version))
2021
if err != nil {
2122
return nil, err
2223
}

db/request.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/gogf/gf/v2/errors/gerror"
66
"github.com/gogf/gf/v2/frame/g"
77
"github.com/gogf/gf/v2/os/gtime"
8+
"github.com/gogf/gf/v2/util/gconv"
89
"strings"
910
)
1011

@@ -64,16 +65,21 @@ func loadRequestMap() {
6465
item.ExecQueue = strings.Split(tag, ",")
6566
}
6667

67-
_requestMap[item.Method+"@"+item.Tag] = item
68+
_requestMap[item.Method+"@"+item.Tag+"@"+gconv.String(item.Version)] = item
69+
// todo 暂按照列表获取, 最后一个是最新, 这里需要调整
70+
_requestMap[item.Method+"@"+item.Tag+"@"+"latest"] = item
6871
}
6972

7073
requestMap = _requestMap
7174
}
7275

73-
func GetRequest(tag string, method string, version int16) (*Request, error) {
74-
// 暂未使用version
75-
// 读取配置时将最新的版本额外增加一个@latest的版本, 传入为-1时候, 读取最新版本
76-
key := method + "@" + tag
76+
func GetRequest(tag string, method string, version string) (*Request, error) {
77+
78+
if version == "" || version == "-1" || version == "0" {
79+
version = "latest"
80+
}
81+
82+
key := method + "@" + tag + "@" + version
7783
request, ok := requestMap[key]
7884

7985
if !ok {

demo/todo/tests/action_test.go

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
"github.com/glennliao/apijson-go/consts"
6+
"github.com/gogf/gf/v2/encoding/gjson"
7+
"github.com/gogf/gf/v2/frame/g"
8+
. "github.com/smartystreets/goconvey/convey"
9+
"testing"
10+
)
11+
12+
func TestActionOneTableOneLine(t *testing.T) {
13+
iAmWM()
14+
todoId := ""
15+
Convey("单表单条数据操作", t, func() {
16+
17+
m := g.DB().Model("t_todo").Ctx(ctx)
18+
19+
// ===================================================================
20+
Convey("新增", func() {
21+
22+
cnt1, err := m.Clone().Count(g.Map{
23+
"user_id": UserIdWM,
24+
})
25+
So(err, ShouldBeNil)
26+
27+
req := `
28+
{
29+
"Todo": {
30+
"title": "去找林云喝茶"
31+
},
32+
"tag": "Todo",
33+
"version": 1
34+
}
35+
`
36+
37+
out, err := actionByJsonStr(req, consts.MethodPost)
38+
So(err, ShouldBeNil)
39+
40+
//g.Dump(out)
41+
todo := out["Todo"].(g.Map)
42+
todoId = todo["todoId"].(string)
43+
44+
cnt2, err := m.Clone().Count(g.Map{
45+
"user_id": UserIdWM,
46+
})
47+
So(err, ShouldBeNil)
48+
49+
So(cnt2-cnt1, ShouldEqual, 1)
50+
51+
})
52+
53+
// ===================================================================
54+
Convey("修改", func() {
55+
56+
req := `
57+
{
58+
"tag":"Todo",
59+
"Todo":{
60+
"todoId":"%s",
61+
"title":"去找林云喝茶, 把史强的预约先取消"
62+
}
63+
}
64+
`
65+
66+
_, err := actionByJsonStr(fmt.Sprintf(req, todoId), consts.MethodPut)
67+
So(err, ShouldBeNil)
68+
69+
one, err := m.Clone().One(g.Map{
70+
"todo_id": todoId,
71+
})
72+
So(err, ShouldBeNil)
73+
So(one.Map()["title"], ShouldEqual, "去找林云喝茶, 把史强的预约先取消")
74+
75+
})
76+
77+
// ===================================================================
78+
Convey("删除", func() {
79+
req := `
80+
{
81+
"tag":"Todo",
82+
"Todo":{
83+
"todoId":"%s"
84+
}
85+
}
86+
`
87+
88+
_, err := actionByJsonStr(fmt.Sprintf(req, todoId), consts.MethodDelete)
89+
So(err, ShouldBeNil)
90+
91+
one, err := m.Clone().One(g.Map{
92+
"todo_id": todoId,
93+
})
94+
So(err, ShouldBeNil)
95+
So(one.IsEmpty(), ShouldBeTrue)
96+
})
97+
})
98+
}
99+
100+
func TestActionMoreTableMoreLine(t *testing.T) {
101+
iAmWM()
102+
todoId := ""
103+
Convey("多表多数据操作", t, func() {
104+
105+
m := g.DB().Model("t_todo").Ctx(ctx)
106+
107+
// ===================================================================
108+
Convey("新增", func() {
109+
110+
cnt1, err := m.Clone().Count(g.Map{
111+
"user_id": UserIdWM,
112+
})
113+
So(err, ShouldBeNil)
114+
115+
req := `
116+
{
117+
"Todo": {
118+
"title": "去找林云喝茶 ♪(^∇^*)"
119+
},
120+
"TodoLog":{
121+
"log":"created by one"
122+
},
123+
"TodoLog[]":[
124+
{"log":"created by list[0]"},
125+
{"log":"created by list[1]"}
126+
],
127+
"tag": "Todo",
128+
"version": 2
129+
}
130+
`
131+
132+
out, err := actionByJsonStr(req, consts.MethodPost)
133+
So(err, ShouldBeNil)
134+
135+
//g.Dump(out)
136+
todo := out["Todo"].(g.Map)
137+
todoId = todo["todoId"].(string)
138+
139+
cnt2, err := m.Clone().Count(g.Map{
140+
"user_id": UserIdWM,
141+
})
142+
So(err, ShouldBeNil)
143+
So(cnt2-cnt1, ShouldEqual, 1)
144+
145+
cnt, err := g.DB().Model("t_todo_log").Ctx(ctx).Count(g.Map{
146+
"todo_id": todoId,
147+
"log": "created by one",
148+
})
149+
So(err, ShouldBeNil)
150+
So(cnt, ShouldEqual, 1)
151+
152+
cnt, err = g.DB().Model("t_todo_log").Ctx(ctx).WhereLike("log", "created by list%").Count(g.Map{
153+
"todo_id": todoId,
154+
})
155+
So(err, ShouldBeNil)
156+
So(cnt, ShouldEqual, 2)
157+
158+
})
159+
160+
// ===================================================================
161+
Convey("修改", func() {
162+
oneId, err := g.DB().Model("t_todo_log").Ctx(ctx).Value("id", g.Map{
163+
"todo_id": todoId,
164+
"log": "created by one",
165+
})
166+
So(err, ShouldBeNil)
167+
168+
manyId, err := g.DB().Model("t_todo_log").Ctx(ctx).WhereLike("log", "created by list%").Array("id", g.Map{
169+
"todo_id": todoId,
170+
})
171+
So(err, ShouldBeNil)
172+
many0 := manyId[0]
173+
many1 := manyId[1]
174+
175+
allIdStr := gjson.New([]int{oneId.Int(), many0.Int(), many1.Int()}).MustToJsonString()
176+
177+
req := `
178+
{
179+
"tag":"TodoLog[]",
180+
"TodoLog":{
181+
"id{}":%s,
182+
"remark":"update all"
183+
},
184+
"TodoLog[]":[
185+
{"log":"update by one","id":"%d"},
186+
{"log":"update by list[0]","id":"%d"},
187+
{"log":"update by list[1]","id":"%d"}
188+
]
189+
}
190+
`
191+
192+
_, err = actionByJsonStr(fmt.Sprintf(req, allIdStr, oneId.Int(), many0.Int(), many1.Int()), consts.MethodPut)
193+
So(err, ShouldBeNil)
194+
195+
cnt, err := g.DB().Model("t_todo_log").Ctx(ctx).Count(g.Map{
196+
"todo_id": todoId,
197+
"remark": "update all",
198+
})
199+
So(err, ShouldBeNil)
200+
So(cnt, ShouldEqual, 3)
201+
202+
cnt, err = g.DB().Model("t_todo_log").Ctx(ctx).Count(g.Map{
203+
"todo_id": todoId,
204+
"log": "update by one",
205+
})
206+
So(err, ShouldBeNil)
207+
So(cnt, ShouldEqual, 1)
208+
209+
cnt, err = g.DB().Model("t_todo_log").Ctx(ctx).WhereLike("log", "update by list%").Count(g.Map{
210+
"todo_id": todoId,
211+
})
212+
So(err, ShouldBeNil)
213+
So(cnt, ShouldEqual, 2)
214+
215+
})
216+
217+
// ===================================================================
218+
Convey("删除", func() {
219+
220+
oneId, err := g.DB().Model("t_todo_log").Ctx(ctx).Value("id", g.Map{
221+
"todo_id": todoId,
222+
"log": "update by one",
223+
})
224+
So(err, ShouldBeNil)
225+
226+
manyId, err := g.DB().Model("t_todo_log").Ctx(ctx).WhereLike("log", "update by list%").Array("id", g.Map{
227+
"todo_id": todoId,
228+
})
229+
So(err, ShouldBeNil)
230+
many0 := manyId[0]
231+
many1 := manyId[1]
232+
233+
allIdStr := gjson.New([]int{oneId.Int(), many0.Int(), many1.Int()}).MustToJsonString()
234+
235+
req := `
236+
{
237+
"tag":"TodoLog",
238+
"TodoLog":{
239+
"id{}":%s
240+
}
241+
}
242+
`
243+
244+
_, err = actionByJsonStr(fmt.Sprintf(req, allIdStr), consts.MethodDelete)
245+
So(err, ShouldBeNil)
246+
247+
cnt, err := g.DB().Model("t_todo_log").Ctx(ctx).Count(g.Map{
248+
"todo_id": todoId,
249+
})
250+
So(err, ShouldBeNil)
251+
So(cnt, ShouldEqual, 0)
252+
})
253+
})
254+
}
255+
256+
func TestActionDEmptyRowKey(t *testing.T) {
257+
iAmWM()
258+
Convey("条件为空的情况", t, func() {
259+
260+
// ===================================================================
261+
Convey("修改", func() {
262+
263+
req := `
264+
{
265+
"tag":"Todo",
266+
"Todo":{
267+
"todoId":"",
268+
"title":"去找林云喝茶, 把史强的预约先取消"
269+
}
270+
}
271+
`
272+
273+
_, err := actionByJsonStr(req, consts.MethodPut)
274+
So(err, ShouldNotBeNil)
275+
276+
})
277+
278+
// ===================================================================
279+
Convey("删除", func() {
280+
req := `
281+
{
282+
"tag":"Todo",
283+
"Todo":{
284+
"todoId":""
285+
}
286+
}
287+
`
288+
289+
_, err := actionByJsonStr(req, consts.MethodDelete)
290+
So(err, ShouldNotBeNil)
291+
292+
})
293+
})
294+
}

demo/todo/tests/config_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tests
22

33
import (
44
"context"
5+
"github.com/glennliao/apijson-go/action"
56
"github.com/glennliao/apijson-go/config"
67
"github.com/glennliao/apijson-go/db"
78
"github.com/glennliao/apijson-go/handlers"
@@ -27,7 +28,7 @@ func init() {
2728
g.DB().SetLogger(g.Log())
2829

2930
g.Log().SetLevelStr("all")
30-
g.Log().SetLevelStr("info") // 需要显示debug时将本句注释即可
31+
//g.Log().SetLevelStr("info") // 需要显示debug时将本句注释即可
3132

3233
db.Init()
3334

@@ -55,6 +56,11 @@ func queryByJsonStr(req string) (res g.Map, err error) {
5556
return handlers.Get(ctx, reqMap)
5657
}
5758

59+
func actionByJsonStr(req string, method string) (res g.Map, err error) {
60+
reqMap := gjson.New(req).Map()
61+
return action.New(ctx, method, reqMap).Result()
62+
}
63+
5864
func countTodoByUser(userId string) int {
5965
m := g.Model("todo").Ctx(ctx)
6066
if userId != "" {

0 commit comments

Comments
 (0)