|
| 1 | +package example |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "github.com/gorm-plus/gorm-plus/gormplus" |
| 7 | + "gorm.io/gorm" |
| 8 | + "log" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSelectById(t *testing.T) { |
| 13 | + user, resultDb := gormplus.SelectById[User](1) |
| 14 | + if resultDb.Error != nil { |
| 15 | + if errors.Is(resultDb.Error, gorm.ErrRecordNotFound) { |
| 16 | + log.Fatalln("SelectById Data not found:", resultDb.Error) |
| 17 | + } |
| 18 | + log.Fatalln("SelectById error:", resultDb.Error) |
| 19 | + } |
| 20 | + log.Println("RowsAffected:", resultDb.RowsAffected) |
| 21 | + marshal, _ := json.Marshal(user) |
| 22 | + log.Println(string(marshal)) |
| 23 | +} |
| 24 | + |
| 25 | +func TestSelectByIds(t *testing.T) { |
| 26 | + var ids []int |
| 27 | + ids = append(ids, 1) |
| 28 | + ids = append(ids, 2) |
| 29 | + users, resultDb := gormplus.SelectByIds[User](ids) |
| 30 | + if resultDb.Error != nil { |
| 31 | + log.Fatalln(resultDb.Error) |
| 32 | + } |
| 33 | + log.Println("RowsAffected:", resultDb.RowsAffected) |
| 34 | + marshal, _ := json.Marshal(users) |
| 35 | + log.Println(string(marshal)) |
| 36 | +} |
| 37 | + |
| 38 | +func TestSelectOne1(t *testing.T) { |
| 39 | + q := &gormplus.Query[User]{} |
| 40 | + q.Eq(UserColumn.Username, "zhangsan1") |
| 41 | + user, resultDb := gormplus.SelectOne(q) |
| 42 | + |
| 43 | + if resultDb.Error != nil { |
| 44 | + if errors.Is(resultDb.Error, gorm.ErrRecordNotFound) { |
| 45 | + log.Fatalln("SelectOne Data not found:", resultDb.Error) |
| 46 | + } |
| 47 | + log.Fatalln("SelectOne error:", resultDb.Error) |
| 48 | + } |
| 49 | + |
| 50 | + log.Println("RowsAffected:", resultDb.RowsAffected) |
| 51 | + marshal, _ := json.Marshal(user) |
| 52 | + log.Println(string(marshal)) |
| 53 | +} |
| 54 | + |
| 55 | +func TestSelectOne2(t *testing.T) { |
| 56 | + q := &gormplus.Query[User]{} |
| 57 | + q.Eq(UserColumn.Username, "zhangsan"). |
| 58 | + Select(UserColumn.Username, UserColumn.Password) |
| 59 | + user, resultDb := gormplus.SelectOne(q) |
| 60 | + |
| 61 | + if resultDb.Error != nil { |
| 62 | + if errors.Is(resultDb.Error, gorm.ErrRecordNotFound) { |
| 63 | + log.Fatalln("SelectOne Data not found:", resultDb.Error) |
| 64 | + } |
| 65 | + log.Fatalln("SelectOne error:", resultDb.Error) |
| 66 | + } |
| 67 | + |
| 68 | + log.Println("RowsAffected:", resultDb.RowsAffected) |
| 69 | + marshal, _ := json.Marshal(user) |
| 70 | + log.Println(string(marshal)) |
| 71 | +} |
| 72 | + |
| 73 | +func TestSelectList1(t *testing.T) { |
| 74 | + q := &gormplus.Query[User]{} |
| 75 | + q.Eq(UserColumn.Username, "zhangsan") |
| 76 | + users, resultDb := gormplus.SelectList(q) |
| 77 | + if resultDb.Error != nil { |
| 78 | + log.Fatalln("error:", resultDb.Error) |
| 79 | + } |
| 80 | + for _, u := range users { |
| 81 | + marshal, _ := json.Marshal(u) |
| 82 | + log.Println(string(marshal)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestSelectTableList(t *testing.T) { |
| 87 | + type deptCount struct { |
| 88 | + Dept string |
| 89 | + Count string |
| 90 | + } |
| 91 | + q := &gormplus.Query[User]{} |
| 92 | + q.Group(UserColumn.Dept).Select(UserColumn.Dept, "count(*) as count") |
| 93 | + users, resultDb := gormplus.SelectModelList[User, deptCount](q) |
| 94 | + if resultDb.Error != nil { |
| 95 | + log.Fatalln("error:", resultDb.Error) |
| 96 | + } |
| 97 | + for _, u := range users { |
| 98 | + marshal, _ := json.Marshal(u) |
| 99 | + log.Println(string(marshal)) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestSelectPage(t *testing.T) { |
| 104 | + q := &gormplus.Query[User]{} |
| 105 | + q.Eq(UserColumn.Age, 18) |
| 106 | + page := &gormplus.Page[User]{Current: 1, Size: 10} |
| 107 | + pageResult, resultDb := gormplus.SelectPage(page, q) |
| 108 | + if resultDb.Error != nil { |
| 109 | + log.Fatalln("error:", resultDb.Error) |
| 110 | + } |
| 111 | + log.Println("total", pageResult.Total) |
| 112 | + for _, u := range pageResult.Records { |
| 113 | + marshal, _ := json.Marshal(u) |
| 114 | + log.Println(string(marshal)) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestSelectTablePage(t *testing.T) { |
| 119 | + type deptCount struct { |
| 120 | + Dept string |
| 121 | + Count string |
| 122 | + } |
| 123 | + q := gormplus.NewQuery[User]() |
| 124 | + q.Group(UserColumn.Dept).Select(UserColumn.Dept, "count(*) as count") |
| 125 | + page := gormplus.NewPage[deptCount](1, 2) |
| 126 | + pageResult, resultDb := gormplus.SelectModelPage[User, deptCount](page, q) |
| 127 | + if resultDb.Error != nil { |
| 128 | + log.Fatalln("error:", resultDb.Error) |
| 129 | + } |
| 130 | + log.Println("total:", pageResult.Total) |
| 131 | + for _, u := range pageResult.Records { |
| 132 | + marshal, _ := json.Marshal(u) |
| 133 | + log.Println(string(marshal)) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestSelectCount(t *testing.T) { |
| 138 | + q := gormplus.NewQuery[User]() |
| 139 | + q.Eq(UserColumn.Age, 18) |
| 140 | + count, resultDb := gormplus.SelectCount(q) |
| 141 | + if resultDb.Error != nil { |
| 142 | + log.Fatalln("error:", resultDb.Error) |
| 143 | + } |
| 144 | + log.Println("count:", count) |
| 145 | +} |
0 commit comments