|
| 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