|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/glennliao/apijson-go/consts" |
| 5 | + "github.com/gogf/gf/v2/frame/g" |
| 6 | + "github.com/gogf/gf/v2/util/gconv" |
| 7 | + . "github.com/smartystreets/goconvey/convey" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestFunctionsQuery(t *testing.T) { |
| 12 | + iAmWM() |
| 13 | + Convey("functions in query", t, func() { |
| 14 | + |
| 15 | + // =================================================================== |
| 16 | + Convey("sayHello", func() { |
| 17 | + |
| 18 | + req := ` |
| 19 | + { |
| 20 | + "User": {}, |
| 21 | + "hello()":"sayHello" |
| 22 | + } |
| 23 | + ` |
| 24 | + out, err := queryByJsonStr(req) |
| 25 | + |
| 26 | + So(err, ShouldBeNil) |
| 27 | + So(hasKey(out, "hello"), ShouldBeTrue) |
| 28 | + So(gconv.String(out["hello"]) == "world", ShouldBeTrue) |
| 29 | + }) |
| 30 | + |
| 31 | + Convey("sayHello()", func() { |
| 32 | + req := ` |
| 33 | + { |
| 34 | + "User": {}, |
| 35 | + "hello()":"sayHello()" |
| 36 | + } |
| 37 | + ` |
| 38 | + out, err := queryByJsonStr(req) |
| 39 | + |
| 40 | + So(err, ShouldBeNil) |
| 41 | + So(hasKey(out, "hello"), ShouldBeTrue) |
| 42 | + So(gconv.String(out["hello"]) == "world", ShouldBeTrue) |
| 43 | + }) |
| 44 | + |
| 45 | + Convey("sayHi(realname)", func() { |
| 46 | + req := ` |
| 47 | + { |
| 48 | + "User": { |
| 49 | + "hello()":"sayHi(realname)" |
| 50 | + } |
| 51 | + } |
| 52 | + ` |
| 53 | + out, err := queryByJsonStr(req) |
| 54 | + |
| 55 | + //g.Dump(out) |
| 56 | + |
| 57 | + So(err, ShouldBeNil) |
| 58 | + So(hasKey(out, "User"), ShouldBeTrue) |
| 59 | + |
| 60 | + user := gconv.Map(out["User"]) |
| 61 | + |
| 62 | + So(hasKey(user, "hello"), ShouldBeTrue) |
| 63 | + So(gconv.String(user["hello"]) == "你好:"+gconv.String(user["realname"]), ShouldBeTrue) |
| 64 | + }) |
| 65 | + |
| 66 | + Convey("sayHi(realname) in List", func() { |
| 67 | + req := ` |
| 68 | + { |
| 69 | + "User[]": { |
| 70 | + "hello()":"sayHi(realname)" |
| 71 | + } |
| 72 | + } |
| 73 | + ` |
| 74 | + out, err := queryByJsonStr(req) |
| 75 | + |
| 76 | + //g.Dump(out) |
| 77 | + |
| 78 | + So(err, ShouldBeNil) |
| 79 | + userList := gconv.Maps(out["User[]"]) |
| 80 | + So(hasKey(userList[0], "hello"), ShouldBeTrue) |
| 81 | + So(gconv.String(userList[0]["hello"]) == "你好:"+gconv.String(userList[0]["realname"]), ShouldBeTrue) |
| 82 | + }) |
| 83 | + |
| 84 | + }) |
| 85 | +} |
| 86 | + |
| 87 | +func TestFunctionsInAction(t *testing.T) { |
| 88 | + iAmSQ() |
| 89 | + Convey("functions in action", t, func() { |
| 90 | + // =================================================================== |
| 91 | + Convey("check", func() { |
| 92 | + |
| 93 | + req := ` |
| 94 | + { |
| 95 | + "Todo": { |
| 96 | + "title": "去找林云喝茶" |
| 97 | + }, |
| 98 | + "tag": "Todo", |
| 99 | + "version": 1 |
| 100 | + } |
| 101 | + ` |
| 102 | + |
| 103 | + _, err := actionByJsonStr(req, consts.MethodPost) |
| 104 | + |
| 105 | + So(err, ShouldNotBeNil) |
| 106 | + |
| 107 | + }) |
| 108 | + |
| 109 | + Convey("replace", func() { |
| 110 | + |
| 111 | + req := ` |
| 112 | + { |
| 113 | + "Todo": { |
| 114 | + "title": "去找林云逛街" |
| 115 | + }, |
| 116 | + "tag": "Todo", |
| 117 | + "version": 1 |
| 118 | + } |
| 119 | + ` |
| 120 | + |
| 121 | + out, err := actionByJsonStr(req, consts.MethodPost) |
| 122 | + //g.Dump(out) |
| 123 | + So(err, ShouldBeNil) |
| 124 | + |
| 125 | + // 删除测试数据 |
| 126 | + todo := out["Todo"].(g.Map) |
| 127 | + todoId := todo["todoId"].(string) |
| 128 | + |
| 129 | + g.DB().Model("t_todo").Unscoped().Delete(g.Map{"todo_id": todoId}) |
| 130 | + |
| 131 | + }) |
| 132 | + }) |
| 133 | +} |
0 commit comments