-
Notifications
You must be signed in to change notification settings - Fork 93
/
model_query_test.go
238 lines (217 loc) · 5.04 KB
/
model_query_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
package dbx
import (
"database/sql"
"testing"
"github.com/stretchr/testify/assert"
)
type Item struct {
ID2 int
Name string
}
func TestModelQuery_Insert(t *testing.T) {
db := getPreparedDB()
defer db.Close()
name := "test"
email := "[email protected]"
{
// inserting normally
customer := Customer{
Name: name,
Email: email,
}
err := db.Model(&customer).Insert()
if assert.Nil(t, err) {
assert.Equal(t, 4, customer.ID)
var c Customer
db.Select().From("customer").Where(HashExp{"ID": 4}).One(&c)
assert.Equal(t, name, c.Name)
assert.Equal(t, email, c.Email)
assert.Equal(t, 0, c.Status)
assert.False(t, c.Address.Valid)
}
}
{
// inserting with pointer-typed fields
customer := CustomerPtr{
Name: name,
Email: &email,
}
err := db.Model(&customer).Insert()
if assert.Nil(t, err) && assert.NotNil(t, customer.ID) {
assert.Equal(t, 5, *customer.ID)
var c CustomerPtr
db.Select().From("customer").Where(HashExp{"ID": 4}).One(&c)
assert.Equal(t, name, c.Name)
if assert.NotNil(t, c.Email) {
assert.Equal(t, email, *c.Email)
}
if assert.NotNil(t, c.Status) {
assert.Equal(t, 0, *c.Status)
}
assert.Nil(t, c.Address)
}
}
{
// inserting with null-typed fields
customer := CustomerNull{
Name: name,
Email: sql.NullString{email, true},
}
err := db.Model(&customer).Insert()
if assert.Nil(t, err) {
// potential todo: need to check if the field implements sql.Scanner
// assert.Equal(t, int64(6), customer.ID.Int64)
var c CustomerNull
db.Select().From("customer").Where(HashExp{"ID": 4}).One(&c)
assert.Equal(t, name, c.Name)
assert.Equal(t, email, c.Email.String)
if assert.NotNil(t, c.Status) {
assert.Equal(t, int64(0), c.Status.Int64)
}
assert.False(t, c.Address.Valid)
}
}
{
// inserting with embedded structures
customer := CustomerEmbedded{
Id: 100,
Email: &email,
InnerCustomer: InnerCustomer{
Name: &name,
Status: sql.NullInt64{1, true},
},
}
err := db.Model(&customer).Insert()
if assert.Nil(t, err) {
assert.Equal(t, 100, customer.Id)
var c CustomerEmbedded
db.Select().From("customer").Where(HashExp{"ID": 100}).One(&c)
assert.Equal(t, name, *c.Name)
assert.Equal(t, email, *c.Email)
if assert.NotNil(t, c.Status) {
assert.Equal(t, int64(1), c.Status.Int64)
}
assert.False(t, c.Address.Valid)
}
}
{
// inserting with include/exclude fields
customer := Customer{
Name: name,
Email: email,
Status: 1,
}
err := db.Model(&customer).Exclude("Name").Insert("Name", "Email")
if assert.Nil(t, err) {
assert.Equal(t, 101, customer.ID)
var c Customer
db.Select().From("customer").Where(HashExp{"ID": 101}).One(&c)
assert.Equal(t, "", c.Name)
assert.Equal(t, email, c.Email)
assert.Equal(t, 0, c.Status)
assert.False(t, c.Address.Valid)
}
}
var a int
assert.NotNil(t, db.Model(&a).Insert())
}
func TestModelQuery_Update(t *testing.T) {
db := getPreparedDB()
defer db.Close()
id := 2
name := "test"
email := "[email protected]"
{
// updating normally
customer := Customer{
ID: id,
Name: name,
Email: email,
}
err := db.Model(&customer).Update()
if assert.Nil(t, err) {
var c Customer
db.Select().From("customer").Where(HashExp{"ID": id}).One(&c)
assert.Equal(t, name, c.Name)
assert.Equal(t, email, c.Email)
assert.Equal(t, 0, c.Status)
}
}
{
// updating without primary keys
item2 := Item{
Name: name,
}
err := db.Model(&item2).Update()
assert.Equal(t, MissingPKError, err)
}
{
// updating all fields
customer := CustomerPtr{
ID: &id,
Name: name,
Email: &email,
}
err := db.Model(&customer).Update()
if assert.Nil(t, err) {
assert.Equal(t, id, *customer.ID)
var c CustomerPtr
db.Select().From("customer").Where(HashExp{"ID": id}).One(&c)
assert.Equal(t, name, c.Name)
if assert.NotNil(t, c.Email) {
assert.Equal(t, email, *c.Email)
}
assert.Nil(t, c.Status)
}
}
{
// updating selected fields only
id = 3
customer := CustomerPtr{
ID: &id,
Name: name,
Email: &email,
}
err := db.Model(&customer).Update("Name", "Email")
if assert.Nil(t, err) {
assert.Equal(t, id, *customer.ID)
var c CustomerPtr
db.Select().From("customer").Where(HashExp{"ID": id}).One(&c)
assert.Equal(t, name, c.Name)
if assert.NotNil(t, c.Email) {
assert.Equal(t, email, *c.Email)
}
if assert.NotNil(t, c.Status) {
assert.Equal(t, 2, *c.Status)
}
}
}
{
// updating non-struct
var a int
assert.NotNil(t, db.Model(&a).Update())
}
}
func TestModelQuery_Delete(t *testing.T) {
db := getPreparedDB()
defer db.Close()
customer := Customer{
ID: 2,
}
err := db.Model(&customer).Delete()
if assert.Nil(t, err) {
var m Customer
err := db.Select().From("customer").Where(HashExp{"ID": 2}).One(&m)
assert.NotNil(t, err)
}
{
// deleting without primary keys
item2 := Item{
Name: "",
}
err := db.Model(&item2).Delete()
assert.Equal(t, MissingPKError, err)
}
var a int
assert.NotNil(t, db.Model(&a).Delete())
}