|
| 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 | +} |
0 commit comments