-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_test.go
228 lines (189 loc) · 6.54 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package crud_test
import (
"testing"
"github.com/azer/crud/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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, "nova@roadbeats.com")
assert.Equal(t, result[1].Name, "Azer")
assert.Equal(t, result[1].Bio, "Engineer")
assert.Equal(t, result[1].Email, "azer@roadbeats.com")
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, "nova@roadbeats.com")
assert.Equal(t, resultptr[1].Name, "Azer")
assert.Equal(t, resultptr[1].Bio, "Engineer")
assert.Equal(t, resultptr[1].Email, "azer@roadbeats.com")
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, "nova@roadbeats.com")
assert.Equal(t, results[1].Name, "Azer")
assert.Equal(t, results[1].Bio, "Engineer")
assert.Equal(t, results[1].Email, "azer@roadbeats.com")
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, "nova@roadbeats.com")
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, "azer@roadbeats.com")
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, "azer@roadbeats.com")
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, "nova@roadbeats.com")
assert.Equal(t, result[1].Name, "Azer")
assert.Equal(t, result[1].Bio, "Engineer")
assert.Equal(t, result[1].Email, "azer@roadbeats.com")
assert.Equal(t, result[2].Name, "Hola")
assert.Equal(t, result[2].Bio, "")
assert.Equal(t, result[2].Email, "hola@roadbeats.com")
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, "nova@roadbeats.com")
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, "nova@roadbeats.com")
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=? GROUP BY u.id", "Nova")
assert.Nil(t, err)
assert.Equal(t, nova.Name, "Nova")
assert.Equal(t, nova.Bio, "Photographer")
assert.Equal(t, nova.Email, "nova@roadbeats.com")
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: "nova@roadbeats.com",
Attachment: []byte("{ \"nova\": \"bar\" }"),
}); err != nil {
return err
}
if err := DB.Create(UserProfile{
Name: "Azer",
Bio: "Engineer",
Email: "azer@roadbeats.com",
Attachment: []byte("{ \"azer\": \"bar\" }"),
}); err != nil {
return err
}
if err := DB.Create(UserProfile{
Name: "Hola",
Email: "hola@roadbeats.com",
Attachment: []byte("{ \"hola\": \"bar\" }"),
}); err != nil {
return err
}
return nil
}
func BenchmarkRead(b *testing.B) {
require.NoError(b, CreateUserProfiles())
b.ResetTimer()
for i := 0; i < b.N; i++ {
var user UserProfile
err := DB.Read(&user, "SELECT * FROM user_profiles LIMIT 1")
require.NoError(b, err)
assert.NotZero(b, user.Id)
assert.NotZero(b, user.Name)
}
}