forked from uadmin/uadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_test.go
251 lines (220 loc) · 6.28 KB
/
db_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package uadmin
import (
"testing"
"time"
)
type TestStruct struct {
Model
Name string
Children []TestStruct
Parent *TestStruct
ParentID uint
OtherModel TestStruct1
OtherModelID uint
}
type TestStruct1 struct {
Model
Name string `uadmin:"search"`
Value int
}
type TestType int
func (TestType) Active() TestType {
return 1
}
func (TestType) Inactive() TestType {
return 2
}
type TestStruct2 struct {
Model
Name string
Count int
Value float64
Start time.Time
End *time.Time
Type TestType
OtherModel TestStruct1
OtherModelID uint
AnotherModel *TestStruct1
AnotherModelID uint
Active bool
Hidden string `uadmin:"list_exclude"`
}
// TestInitializeDB is a unit testing function for initializeDB() function
func TestInitializeDB(t *testing.T) {
examples := []struct {
m interface{}
name string
m2m []string
}{
{TestStruct{}, "test_structs", []string{"teststruct_teststruct"}},
{TestStruct2{}, "test_struct2", []string{}},
}
for _, e := range examples {
initializeDB(e.m)
if !db.HasTable(e.name) {
t.Errorf("initializeDB didn't create table %s", e.name)
}
for _, name := range e.m2m {
if !db.HasTable(name) {
t.Errorf("initializeDB didn't create table %s", name)
}
}
}
}
// TestSave is a unit testing function for Save() and customSave() function
func TestSave(t *testing.T) {
Schema["teststruct"], _ = getSchema(TestStruct{})
models["teststruct"] = TestStruct{}
// Schema["teststruct1"], _ = getSchema(TestStruct1{})
// models["teststruct1"] = TestStruct1{}
Schema["teststruct2"], _ = getSchema(TestStruct2{})
models["teststruct2"] = TestStruct2{}
r1 := TestStruct{
Name: "",
}
r2 := TestStruct{
Name: "abc",
}
r3 := TestStruct{
Name: "ABC",
}
m1 := TestStruct1{
Name: "abc",
}
examples := []struct {
m interface{}
}{
{&r1},
{&r2},
{&r3},
}
mExamples := []struct {
m interface{}
}{
{&m1},
}
for _, e := range examples {
Save(e.m)
}
if Count(TestStruct{}, "") != len(examples) {
t.Errorf("Count is invalid after saving. Got %d expected %d", Count(TestStruct{}, ""), len(examples))
}
for _, e := range mExamples {
Save(e.m)
}
if Count(TestStruct{}, "") != len(examples) {
t.Errorf("Count is invalid after saving. Got %d expected %d", Count(TestStruct1{}, ""), len(mExamples))
}
r4 := TestStruct{
Name: "test",
Children: []TestStruct{r1, r2},
ParentID: r3.ID,
Parent: &r3,
OtherModelID: m1.ID,
OtherModel: m1,
}
examples2 := []struct {
m interface{}
}{
{&r4},
}
for _, e := range examples2 {
Save(e.m)
}
if Count(TestStruct{}, "") != len(examples)+len(examples2) {
t.Errorf("Count is invalid after saving. Got %d expected %d", Count(TestStruct{}, ""), len(examples)+len(examples2))
}
var count int
db.Table("teststruct_teststruct").Count(&count)
if count != len(r4.Children) {
t.Errorf("M2M count is invalid after saving. Got %d expected %d", count, len(r4.Children))
}
r4.Children = []TestStruct{r1}
Save(&r4)
db.Table("teststruct_teststruct").Count(&count)
if count != len(r4.Children) {
t.Errorf("M2M count is invalid after saving. Got %d expected %d", count, len(r4.Children))
}
r5 := TestStruct{}
Get(&r5, "name = ?", "test")
if r4.ID != r5.ID {
t.Errorf("Get didn't return the correct record.ID. Got %#v expected %#v", r5, r4)
}
if r4.Name != r5.Name {
t.Errorf("Get didn't return the correct record.Name. Got %#v expected %#v", r5, r4)
}
if len(r4.Children) != len(r5.Children) {
t.Errorf("Get didn't return the correct record.Children. Got %#v expected %#v", r5, r4)
}
if r4.ParentID != r5.ParentID {
t.Errorf("Get didn't return the correct record.ParentID. Got %#v expected %#v", r5, r4)
}
// Test FilterBuilder
f := map[string]interface{}{
"name = ?": "abc",
"parent_id = ?": 0,
}
q, args := FilterBuilder(f)
expectedArgs := []interface{}{"abc", 0}
expectedQ1 := "name = ? AND parent_id = ?"
expectedQ2 := "parent_id = ? AND name = ?"
if q != expectedQ1 {
if q != expectedQ2 {
t.Errorf("FilterBuilder didn't return the correct q. Got %#v expected %#v", q, expectedQ2)
} else {
expectedArgs = []interface{}{0, "abc"}
}
}
if len(args) != len(expectedArgs) {
t.Errorf("FilterBuilder didn't return the correct len(args). Got %#v expected %#v", len(args), len(expectedArgs))
} else {
for i := range args {
if args[i] != expectedArgs[i] {
t.Errorf("FilterBuilder didn't return the correct args[%d]. Got %#v expected %#v", i, args[i], expectedArgs[i])
}
}
}
rows := []TestStruct{}
Filter(&rows, q, args...)
if len(rows) != 1 {
t.Errorf("Filter didn't return the correct number of records. Got %d expected %d", len(rows), 1)
}
Preload(&r5)
if (r4.Parent == nil) != (r5.Parent == nil) {
t.Errorf("Preload didn't return the correct record.Parent. Got %#v expected %#v", r5, r4)
} else {
if (r4.Parent != nil) && (r5.Parent != nil) {
if r4.Parent.ID != r5.Parent.ID {
t.Errorf("Preload didn't return the correct record.Parent. Got %#v expected %#v", r5, r4)
}
}
}
if r4.OtherModel.ID != r5.OtherModel.ID {
t.Errorf("Preload didn't return the correct record.OtherModel. Got %#v expected %#v", r5, r4)
}
// testing AdminPage
rows = []TestStruct{}
AdminPage("id", true, 0, -1, &rows, "name = ?", "abc")
if len(rows) != 1 {
t.Errorf("AdminPage didn't return the correct number of records. Got %d expected %d", len(rows), 1)
}
rows = []TestStruct{}
AdminPage("id", true, 0, 1, &rows, "name = ?", "abc")
if len(rows) != 1 {
t.Errorf("AdminPage didn't return the correct number of records. Got %d expected %d", len(rows), 1)
}
Update(TestStruct{}, "name", "abc", "")
rows = []TestStruct{}
AdminPage("id", true, 0, -1, &rows, "name = ?", "abc")
if len(rows) != len(examples)+len(examples2) {
t.Errorf("AdminPage didn't return the correct number of records after Update. Got %d expected %d", len(rows), len(examples)+len(examples2))
}
Delete(r1)
if Count(TestStruct{}, "") != len(examples)+len(examples2)-1 {
t.Errorf("Count is invalid after Delete. Got %d expected %d", Count(TestStruct{}, ""), len(examples)+len(examples2)-1)
}
DeleteList(TestStruct{}, "")
if Count(TestStruct{}, "") != 0 {
t.Errorf("Count is invalid after DeleteList. Got %d expected %d", Count(TestStruct{}, ""), 0)
}
}