-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
pop_test.go
495 lines (414 loc) · 12.8 KB
/
pop_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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
package pop
import (
stdlog "log"
"os"
"testing"
"time"
"github.com/gobuffalo/nulls"
"github.com/gobuffalo/pop/v6/logging"
"github.com/gobuffalo/validate/v3"
"github.com/gobuffalo/validate/v3/validators"
"github.com/gofrs/uuid"
"github.com/stretchr/testify/suite"
)
var PDB *Connection
type PostgreSQLSuite struct {
suite.Suite
}
type MySQLSuite struct {
suite.Suite
}
type SQLiteSuite struct {
suite.Suite
}
type CockroachSuite struct {
suite.Suite
}
func TestSpecificSuites(t *testing.T) {
switch os.Getenv("SODA_DIALECT") {
case "postgres":
suite.Run(t, &PostgreSQLSuite{})
case "mysql", "mysql_travis":
suite.Run(t, &MySQLSuite{})
case "sqlite":
suite.Run(t, &SQLiteSuite{})
case "cockroach":
suite.Run(t, &CockroachSuite{})
}
}
func init() {
Debug = false
AddLookupPaths("./")
dialect := os.Getenv("SODA_DIALECT")
if dialect == "" {
log(logging.Info, "Skipping integration tests because SODA_DIALECT is blank or unset")
return
}
if err := LoadConfigFile(); err != nil {
stdlog.Panic(err)
}
var err error
log(logging.Info, "Run test with dialect %v", dialect)
PDB, err = Connect(dialect)
if err != nil {
stdlog.Panic(err)
}
}
func transaction(fn func(tx *Connection)) {
err := PDB.Rollback(func(tx *Connection) {
fn(tx)
})
if err != nil {
stdlog.Fatal(err)
}
}
func ts(s string) string {
return PDB.Dialect.TranslateSQL(s)
}
type Client struct {
ClientID string `db:"id"`
}
func (c Client) TableName() string {
return "clients"
}
type User struct {
ID int `db:"id"`
UserName string `db:"user_name"`
Email string `db:"email"`
Name nulls.String `db:"name"`
Alive nulls.Bool `db:"alive"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
BirthDate nulls.Time `db:"birth_date"`
Bio nulls.String `db:"bio"`
Price nulls.Float64 `db:"price"`
FullName nulls.String `db:"full_name" select:"name as full_name"`
Books Books `has_many:"books" order_by:"title asc"`
FavoriteSong Song `has_one:"song" fk_id:"u_id"`
Houses Addresses `many_to_many:"users_addresses"`
}
type UserPointerAssocs struct {
ID int `db:"id"`
UserName string `db:"user_name"`
Email string `db:"email"`
Name nulls.String `db:"name"`
Alive nulls.Bool `db:"alive"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
BirthDate nulls.Time `db:"birth_date"`
Bio nulls.String `db:"bio"`
Price nulls.Float64 `db:"price"`
FullName nulls.String `db:"full_name" select:"name as full_name"`
Books Books `has_many:"books" order_by:"title asc" fk_id:"user_id"`
FavoriteSong *Song `has_one:"song" fk_id:"u_id"`
Houses Addresses `many_to_many:"users_addresses"`
}
func (UserPointerAssocs) TableName() string {
return "users"
}
// Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (u *User) Validate(tx *Connection) (*validate.Errors, error) {
return validate.Validate(
&validators.StringIsPresent{Field: u.Name.String, Name: "Name"},
), nil
}
type Users []User
type UserAttribute struct {
ID int `db:"id"`
UserName string `db:"user_name"`
NickName string `db:"nick_name"`
User User `json:"user" belongs_to:"user" fk_id:"UserName" primary_id:"UserName"`
}
type Book struct {
ID int `db:"id"`
Title string `db:"title"`
Isbn string `db:"isbn"`
UserID nulls.Int `db:"user_id"`
User User `belongs_to:"user"`
Description string `db:"description"`
Writers Writers `has_many:"writers"`
TaxiID nulls.Int `db:"taxi_id"`
Taxi Taxi `belongs_to:"taxi"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type Taxi struct {
ID int `db:"id"`
Model string `db:"model"`
UserID nulls.Int `db:"user_id"`
AddressID nulls.Int `db:"address_id"`
Driver *User `belongs_to:"user" fk_id:"user_id"`
Address Address `belongs_to:"address"`
ToAddressID *int `db:"to_address_id"`
ToAddress *Address `belongs_to:"address"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type Taxis []Taxi
// Validate gets run every time you call a "Validate*" (ValidateAndSave, ValidateAndCreate, ValidateAndUpdate) method.
// This method is not required and may be deleted.
func (b *Book) Validate(tx *Connection) (*validate.Errors, error) {
// Execute another query to test if Validate causes eager creation to fail
if err := tx.All(&Taxis{}); err != nil {
return nil, err
}
return validate.Validate(
&validators.StringIsPresent{Field: b.Description, Name: "Description"},
), nil
}
type Books []Book
type Writer struct {
ID int `db:"id"`
Name string `db:"name"`
Addresses Addresses `has_many:"addresses"`
Friends []Friend `has_many:"friends"`
BookID int `db:"book_id"`
Book Book `belongs_to:"book"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type Writers []Writer
type Address struct {
ID int `db:"id"`
Street string `db:"street"`
WriterID int `db:"writer_id"`
HouseNumber int `db:"house_number"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
TaxisToHere Taxis `has_many:"taxis" fk_id:"to_address_id" order_by:"created_at asc"`
}
type Addresses []Address
type UsersAddress struct {
ID int `db:"id"`
UserID int `db:"user_id"`
AddressID int `db:"address_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type UsersAddressQuery struct {
ID int `db:"id"`
UserID int `db:"user_id"`
AddressID int `db:"address_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
UserName *string `db:"name" json:"user_name"`
UserEmail *string `db:"email" json:"user_email"`
}
func (UsersAddressQuery) TableName() string {
return "users_addresses"
}
type Friend struct {
ID int `db:"id"`
FirstName string `db:"first_name"`
WriterID int `db:"writer_id"`
LastName string `db:"last_name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
func (Friend) TableName() string {
return "good_friends"
}
type Family struct {
ID int `db:"id"`
FirstName string `db:"first_name"`
LastName string `db:"last_name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
func (Family) TableName() string {
// schema.table_name
return "family.members"
}
type Enemy struct {
A string
}
type Song struct {
ID uuid.UUID `db:"id"`
Title string `db:"title"`
UserID int `db:"u_id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
ComposedByID int `json:"composed_by_id" db:"composed_by_id"`
ComposedBy Composer `belongs_to:"composer"`
}
type Composer struct {
ID int `db:"id"`
Name string `db:"name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type Course struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
type CourseCode struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
CourseID uuid.UUID `json:"course_id" db:"course_id"`
Course Course `json:"-" belongs_to:"course"`
// Course Course `belongs_to:"course"`
}
type NetClient struct {
ID uuid.UUID `json:"id" db:"id"`
Hops []Hop `json:"hop_id" has_many:"hops"`
}
type Hop struct {
ID uuid.UUID `json:"id" db:"id"`
NetClient *NetClient `json:"net_client" belongs_to:"net_client" fk_id:"NetClientID"`
NetClientID uuid.UUID `json:"net_client_id" db:"net_client_id"`
Server *Server `json:"course" belongs_to:"server" fk_id:"ServerID" oder_by:"id asc"`
ServerID uuid.NullUUID `json:"server_id" db:"server_id"`
}
type Server struct {
ID uuid.UUID `json:"id" db:"id"`
}
type ValidatableCar struct {
ID int64 `db:"id"`
Name string `db:"name"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
var validationLogs []string
func (v *ValidatableCar) Validate(tx *Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "Validate")
verrs := validate.Validate(&validators.StringIsPresent{Field: v.Name, Name: "Name"})
return verrs, nil
}
func (v *ValidatableCar) ValidateSave(tx *Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "ValidateSave")
return nil, nil
}
func (v *ValidatableCar) ValidateUpdate(tx *Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "ValidateUpdate")
return nil, nil
}
func (v *ValidatableCar) ValidateCreate(tx *Connection) (*validate.Errors, error) {
validationLogs = append(validationLogs, "ValidateCreate")
return nil, nil
}
type NotValidatableCar struct {
ID int `db:"id"`
Name string `db:"name"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
type CallbacksUser struct {
ID int `db:"id"`
BeforeS string `db:"before_s"`
BeforeC string `db:"before_c"`
BeforeU string `db:"before_u"`
BeforeD string `db:"before_d"`
BeforeV string `db:"before_v"`
AfterS string `db:"after_s"`
AfterC string `db:"after_c"`
AfterU string `db:"after_u"`
AfterD string `db:"after_d"`
AfterF string `db:"after_f"`
AfterEF string `db:"after_ef"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
type CallbacksUsers []CallbacksUser
func (u *CallbacksUser) BeforeSave(tx *Connection) error {
u.BeforeS = "BeforeSave"
return nil
}
func (u *CallbacksUser) BeforeUpdate(tx *Connection) error {
u.BeforeU = "BeforeUpdate"
return nil
}
func (u *CallbacksUser) BeforeCreate(tx *Connection) error {
u.BeforeC = "BeforeCreate"
return nil
}
func (u *CallbacksUser) BeforeDestroy(tx *Connection) error {
u.BeforeD = "BeforeDestroy"
return nil
}
func (u *CallbacksUser) BeforeValidate(tx *Connection) error {
u.BeforeV = "BeforeValidate"
return nil
}
func (u *CallbacksUser) AfterSave(tx *Connection) error {
u.AfterS = "AfterSave"
return nil
}
func (u *CallbacksUser) AfterUpdate(tx *Connection) error {
u.AfterU = "AfterUpdate"
return nil
}
func (u *CallbacksUser) AfterCreate(tx *Connection) error {
u.AfterC = "AfterCreate"
return nil
}
func (u *CallbacksUser) AfterDestroy(tx *Connection) error {
u.AfterD = "AfterDestroy"
return nil
}
func (u *CallbacksUser) AfterFind(tx *Connection) error {
u.AfterF = "AfterFind"
return nil
}
func (u *CallbacksUser) AfterEagerFind(tx *Connection) error {
u.AfterEF = "AfterEagerFind"
return nil
}
type Label struct {
ID string `db:"id"`
}
type SingleID struct {
ID int `db:"id"`
}
type Body struct {
ID int `json:"id" db:"id"`
Head *Head `json:"head" has_one:"head"`
}
type Head struct {
ID int `json:"id,omitempty" db:"id"`
BodyID int `json:"-" db:"body_id"`
Body *Body `json:"body,omitempty" belongs_to:"body"`
}
type HeadPtr struct {
ID int `json:"id,omitempty" db:"id"`
BodyID *int `json:"-" db:"body_id"`
Body *Body `json:"body,omitempty" belongs_to:"body"`
}
type Student struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
// https://github.com/gobuffalo/pop/issues/302
type Parent struct {
ID uuid.UUID `json:"id" db:"id"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
Students []*Student `many_to_many:"parents_students"`
}
type CrookedColour struct {
ID int `db:"pk"`
Name string `db:"name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type CrookedSong struct {
ID string `db:"name"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type NonStandardID struct {
ID int `db:"pk"`
OutfacingID string `db:"id"`
}
type InnerStruct struct {
ID int `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type EmbeddingStruct struct {
InnerStruct
AdditionalField string `db:"additional_field"`
}