-
Notifications
You must be signed in to change notification settings - Fork 17
/
read_test.go
214 lines (177 loc) · 6.2 KB
/
read_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
package crud_test
import (
"testing"
"github.com/azer/crud/v2"
"github.com/stretchr/testify/assert"
)
func TestResolveReadParams(t *testing.T) {
query, params, err := crud.ResolveReadParams([]interface{}{})
assert.Equal(t, query, "")
assert.Equal(t, len(params), 0)
assert.Nil(t, err)
query, params, err = crud.ResolveReadParams([]interface{}{123, 456})
assert.Equal(t, query, "")
assert.Nil(t, params)
assert.NotNil(t, err)
query, params, err = crud.ResolveReadParams([]interface{}{"yolo", 456, 123})
assert.Equal(t, query, "yolo")
assert.Equal(t, len(params), 2)
assert.Nil(t, err)
}
func TestReadingMultipleRows(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
result := []UserProfile{}
err := DB.Read(&result, "SELECT * FROM user_profiles")
assert.Nil(t, err)
assert.Equal(t, len(result), 3)
assert.Equal(t, result[0].Name, "Nova")
assert.Equal(t, result[0].Bio, "Photographer")
assert.Equal(t, result[0].Email, "[email protected]")
assert.Equal(t, result[1].Name, "Azer")
assert.Equal(t, result[1].Bio, "Engineer")
assert.Equal(t, result[1].Email, "[email protected]")
assert.Equal(t, string(result[1].Attachment), "{ \"azer\": \"bar\" }")
resultptr := []*UserProfile{}
err = DB.Read(&resultptr, "SELECT * FROM user_profiles")
assert.Nil(t, err)
assert.Equal(t, len(resultptr), 3)
assert.Equal(t, resultptr[0].Name, "Nova")
assert.Equal(t, resultptr[0].Bio, "Photographer")
assert.Equal(t, resultptr[0].Email, "[email protected]")
assert.Equal(t, resultptr[1].Name, "Azer")
assert.Equal(t, resultptr[1].Bio, "Engineer")
assert.Equal(t, resultptr[1].Email, "[email protected]")
var results []*UserProfile
err = DB.Read(&results, "SELECT * FROM user_profiles")
assert.Nil(t, err)
assert.Equal(t, len(results), 3)
assert.Equal(t, results[0].Name, "Nova")
assert.Equal(t, results[0].Bio, "Photographer")
assert.Equal(t, results[0].Email, "[email protected]")
assert.Equal(t, results[1].Name, "Azer")
assert.Equal(t, results[1].Bio, "Engineer")
assert.Equal(t, results[1].Email, "[email protected]")
var notmatching []*UserProfile
err = DB.Read(¬matching, "SELECT * FROM user_profiles WHERE name='not matching'")
assert.Nil(t, err)
assert.Equal(t, len(notmatching), 0)
}
func TestReadingSingleRow(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
nova := UserProfile{}
err := DB.Read(&nova, "SELECT * FROM user_profiles WHERE name = ?", "Nova")
assert.Nil(t, err)
assert.Equal(t, nova.Id, 1)
assert.Equal(t, nova.Name, "Nova")
assert.Equal(t, nova.Bio, "Photographer")
assert.Equal(t, nova.Email, "[email protected]")
var azer *UserProfile = &UserProfile{}
err = DB.Read(azer, "SELECT * FROM user_profiles WHERE name = ?", "Azer")
assert.Nil(t, err)
assert.Equal(t, azer.Id, 2)
assert.Equal(t, azer.Name, "Azer")
assert.Equal(t, azer.Bio, "Engineer")
assert.Equal(t, azer.Email, "[email protected]")
var az UserProfile
err = DB.Read(&az, "SELECT * FROM user_profiles WHERE name = ?", "Azer")
assert.Nil(t, err)
assert.Equal(t, az.Id, 2)
assert.Equal(t, az.Name, "Azer")
assert.Equal(t, az.Bio, "Engineer")
assert.Equal(t, az.Email, "[email protected]")
no := UserProfile{}
err = DB.Read(&no, "SELECT * FROM user_profiles WHERE name = ?", "Not matching")
assert.NotNil(t, err)
DB.DropTables(UserProfile{})
}
func TestGeneratingQueries(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
result := []UserProfile{}
err := DB.Read(&result, "SELECT * FROM user_profiles")
assert.Nil(t, err)
assert.Equal(t, len(result), 3)
assert.Equal(t, result[0].Name, "Nova")
assert.Equal(t, result[0].Bio, "Photographer")
assert.Equal(t, result[0].Email, "[email protected]")
assert.Equal(t, result[1].Name, "Azer")
assert.Equal(t, result[1].Bio, "Engineer")
assert.Equal(t, result[1].Email, "[email protected]")
assert.Equal(t, result[2].Name, "Hola")
assert.Equal(t, result[2].Bio, "")
assert.Equal(t, result[2].Email, "[email protected]")
nova := UserProfile{}
err = DB.Read(&nova, "SELECT * FROM user_profiles WHERE name=?", "Nova")
assert.Nil(t, err)
assert.Equal(t, nova.Name, "Nova")
assert.Equal(t, nova.Bio, "Photographer")
assert.Equal(t, nova.Email, "[email protected]")
DB.DropTables(UserProfile{})
}
func TestScanningToCustomValues(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
names := []string{}
err := DB.Read(&names, "SELECT name FROM user_profiles ORDER BY id ASC")
assert.Nil(t, err)
assert.Equal(t, len(names), 3)
assert.Equal(t, names[0], "Nova")
assert.Equal(t, names[1], "Azer")
name := ""
err = DB.Read(&name, "SELECT name FROM user_profiles WHERE id=1")
assert.Nil(t, err)
assert.Equal(t, name, "Nova")
count := 0
err = DB.Read(&count, "SELECT COUNT(id) FROM user_profiles")
assert.Nil(t, err)
assert.Equal(t, count, 3)
DB.DropTables(UserProfile{})
}
func TestScanningToNullTypes(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
nova := UserProfileNull{}
err := DB.Read(&nova, "SELECT * FROM user_profiles WHERE name = ?", "Nova")
assert.Nil(t, err)
assert.Equal(t, nova.Id.Int64, int64(1))
assert.Equal(t, nova.Name.String, "Nova")
assert.Equal(t, nova.Bio.String, "Photographer")
assert.Equal(t, nova.Email.String, "[email protected]")
DB.DropTables(UserProfile{})
}
func TestUnexistingFields(t *testing.T) {
assert.Nil(t, CreateUserProfiles())
nova := UserProfile{}
err := DB.Read(&nova, "SELECT u.*, COUNT(u.id) as ucount FROM user_profiles u WHERE name=?", "Nova")
assert.Nil(t, err)
assert.Equal(t, nova.Name, "Nova")
assert.Equal(t, nova.Bio, "Photographer")
assert.Equal(t, nova.Email, "[email protected]")
DB.DropTables(UserProfile{})
}
func CreateUserProfiles() error {
if err := DB.ResetTables(UserProfile{}); err != nil {
return err
}
if err := DB.Create(UserProfile{
Name: "Nova",
Bio: "Photographer",
Email: "[email protected]",
Attachment: []byte("{ \"nova\": \"bar\" }"),
}); err != nil {
return err
}
if err := DB.Create(UserProfile{
Name: "Azer",
Bio: "Engineer",
Email: "[email protected]",
Attachment: []byte("{ \"azer\": \"bar\" }"),
}); err != nil {
return err
}
if err := DB.Create(UserProfile{
Name: "Hola",
Email: "[email protected]",
Attachment: []byte("{ \"hola\": \"bar\" }"),
}); err != nil {
return err
}
return nil
}