forked from Ompluscator/gorm-generics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository_test.go
187 lines (166 loc) · 3.83 KB
/
repository_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package gorm_generics_test
import (
"context"
gorm_generics "github.com/ompluscator/gorm-generics"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"testing"
)
// Product is a domain entity
type Product struct {
ID uint
Name string
Weight uint
IsAvailable bool
}
// ProductGorm is DTO used to map Product entity to database
type ProductGorm struct {
ID uint `gorm:"primaryKey;column:id"`
Name string `gorm:"column:name"`
Weight uint `gorm:"column:weight"`
IsAvailable bool `gorm:"column:is_available"`
}
// ToEntity respects the gorm_generics.GormModel interface
// Creates new Entity from GORM model.
func (g ProductGorm) ToEntity() Product {
return Product{
ID: g.ID,
Name: g.Name,
Weight: g.Weight,
IsAvailable: g.IsAvailable,
}
}
// FromEntity respects the gorm_generics.GormModel interface
// Creates new GORM model from Entity.
func (g ProductGorm) FromEntity(product Product) interface{} {
return ProductGorm{
ID: product.ID,
Name: product.Name,
Weight: product.Weight,
IsAvailable: product.IsAvailable,
}
}
func getDB() (*gorm.DB, error) {
return gorm.Open(sqlite.Open("file:test?mode=memory&cache=shared&_fk=1"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
}
func TestMain(m *testing.M) {
db, err := getDB()
if err != nil {
log.Fatal(err)
}
err = db.AutoMigrate(ProductGorm{})
if err != nil {
log.Fatal(err)
}
ret := m.Run()
os.Exit(ret)
}
func TestGormRepository_Insert(t *testing.T) {
db, _ := getDB()
repository := gorm_generics.NewRepository[ProductGorm, Product](db)
ctx := context.Background()
product := Product{
ID: 8,
Name: "product1",
Weight: 100,
IsAvailable: true,
}
err := repository.Insert(ctx, &product)
if err != nil {
panic(err)
}
}
func TestGormRepository_FindByID(t *testing.T) {
db, _ := getDB()
repository := gorm_generics.NewRepository[ProductGorm, Product](db)
ctx := context.Background()
_, err := repository.FindByID(ctx, 8)
if err != nil {
panic(err)
}
}
func TestGormRepository_Count(t *testing.T) {
db, _ := getDB()
repository := gorm_generics.NewRepository[ProductGorm, Product](db)
ctx := context.Background()
nb, err := repository.Count(ctx)
if err != nil {
panic(err)
}
if nb != 1 {
panic("not good count")
}
}
func TestGormRepository_DeleteByID(t *testing.T) {
db, _ := getDB()
repository := gorm_generics.NewRepository[ProductGorm, Product](db)
ctx := context.Background()
err := repository.DeleteById(ctx, 8)
if err != nil {
panic(err)
}
_, err = repository.FindByID(ctx, 8)
if err == nil {
panic("supposed to be deleted")
}
}
func TestGormRepository_Find(t *testing.T) {
db, _ := getDB()
repository := gorm_generics.NewRepository[ProductGorm, Product](db)
ctx := context.Background()
product := Product{
ID: 1,
Name: "product1",
Weight: 100,
IsAvailable: true,
}
repository.Insert(ctx, &product)
product2 := Product{
ID: 2,
Name: "product2",
Weight: 50,
IsAvailable: true,
}
repository.Insert(ctx, &product2)
many, err := repository.Find(ctx, gorm_generics.GreaterOrEqual("weight", 50))
if err != nil {
panic(err)
}
if len(many) != 2 {
panic("should be 2")
}
repository.Insert(ctx, &Product{
ID: 3,
Name: "product3",
Weight: 250,
IsAvailable: false,
})
many, err = repository.Find(ctx, gorm_generics.GreaterOrEqual("weight", 90))
if err != nil {
panic(err)
}
if len(many) != 2 {
panic("should be 2")
}
many, err = repository.Find(ctx, gorm_generics.And(
gorm_generics.GreaterOrEqual("weight", 90),
gorm_generics.Equal("is_available", true)),
)
if err != nil {
panic(err)
}
if len(many) != 1 {
panic("should be 1")
}
}
/*
TODO
Delete (by item)
Update
Find (with sql cond)
*/