forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfake.go
672 lines (553 loc) · 18.9 KB
/
fake.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
// Copyright 2018 John Deng ([email protected]).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gorm
import (
"database/sql"
"time"
"fmt"
"errors"
"github.com/jinzhu/copier"
)
type Mocker interface {
Mock(method string, data interface{}) *FakeRepository
Expect(err error)
}
// DB contains information for current db connection
type FakeRepository struct {
value interface{}
err error
rowsAffected int64
// single db
db SQLCommon
blockGlobalUpdate bool
logMode int
logger Logger
search *Search
values map[string]interface{}
// global db
parent Repository
callbacks *Callback
dialect Dialect
singularTable bool
mockData map[string]interface{}
}
// New clone a new db connection without search conditions
func (r *FakeRepository) New() Repository {
clone := r.Clone()
clone.SetSearch(nil)
clone.SetValue(nil)
return clone
}
// Close close current db connection. If database connection is not an io.Closer, returns an error.
func (r *FakeRepository) Close() error {
if db, ok := r.Parent().SQLCommonDB().(closer); ok {
return db.Close()
}
return errors.New("can't close current db")
}
// DB get `*sql.DB` from current connection
// If the underlying database connection is not a *sql.DB, returns nil
func (r *FakeRepository) SqlDB() *sql.DB {
db, _ := r.db.(*sql.DB)
return db
}
// CommonDB return the underlying `*sql.DB` or `*sql.Tx` instance, mainly intended to allow coexistence with legacy non-GORM code.
func (r *FakeRepository) CommonDB() SQLCommon {
return r.db
}
// Dialect get dialect
func (r *FakeRepository) Dialect() Dialect {
return r.dialect
}
// Callback return `Callbacks` container, you could add/change/delete callbacks with it
// db.Callback().Create().Register("update_created_at", updateCreated)
// Refer https://jinzhu.github.io/gorm/development.html#callbacks
func (r *FakeRepository) Callback() *Callback {
r.parent.SetCallbacks(r.parent.Callbacks().clone())
return r.parent.Callbacks()
}
// SetLogger replace default logger
func (r *FakeRepository) SetLogger(log Logger) Repository {
r.logger = log
return r
}
// LogMode set log mode, `true` for detailed logs, `false` for no log, default, will only print error logs
func (r *FakeRepository) LogMode(enable bool) Repository {
if enable {
r.logMode = 2
} else {
r.logMode = 1
}
return r
}
// BlockGlobalUpdate if true, generates an error on update/delete without where clause.
// This is to prevent eventual error with empty objects updates/deletions
func (r *FakeRepository) BlockGlobalUpdate(enable bool) Repository {
r.blockGlobalUpdate = enable
return r
}
// HasBlockGlobalUpdate return state of block
func (r *FakeRepository) HasBlockGlobalUpdate() bool {
return r.blockGlobalUpdate
}
// SingularTable use singular table by default
func (r *FakeRepository) SingularTable(enable bool) {
r.parent.SetIsSingularTable(enable)
}
// NewScope create a scope for current operation
func (r *FakeRepository) NewScope(value interface{}) *Scope {
dbClone := r.Clone()
dbClone.SetValue(value)
scope := &Scope{db: dbClone, Search: dbClone.Search().clone(), Value: value}
return scope
}
// QueryExpr returns the query as expr object
func (r *FakeRepository) QueryExpr() *Expression {
scope := r.NewScope(r.value)
scope.InstanceSet("skip_bindvar", true)
scope.prepareQuerySQL()
return Expr(scope.SQL, scope.SQLVars...)
}
// SubQuery returns the query as sub query
func (r *FakeRepository) SubQuery() *Expression {
scope := r.NewScope(r.value)
scope.InstanceSet("skip_bindvar", true)
scope.prepareQuerySQL()
return Expr(fmt.Sprintf("(%v)", scope.SQL), scope.SQLVars...)
}
// Where return a new relation, filter records with given conditions, accepts `map`, `struct` or `string` as conditions, refer http://jinzhu.github.io/gorm/crud.html#query
func (r *FakeRepository) Where(query interface{}, args ...interface{}) Repository {
return r
}
// Or filter records that match before conditions or this one, similar to `Where`
func (r *FakeRepository) Or(query interface{}, args ...interface{}) Repository {
return r
}
// Not filter records that don't match current conditions, similar to `Where`
func (r *FakeRepository) Not(query interface{}, args ...interface{}) Repository {
return r
}
// Limit specify the number of records to be retrieved
func (r *FakeRepository) Limit(limit interface{}) Repository {
return r
}
// Offset specify the number of records to skip before starting to return the records
func (r *FakeRepository) Offset(offset interface{}) Repository {
return r
}
// Order specify order when retrieve records from database, set reorder to `true` to overwrite defined conditions
// db.Order("name DESC")
// db.Order("name DESC", true) // reorder
// db.Order(gorm.Expr("name = ? DESC", "first")) // sql expression
func (r *FakeRepository) Order(value interface{}, reorder ...bool) Repository {
return r
}
// Select specify fields that you want to retrieve from database when querying, by default, will select all fields;
// When creating/updating, specify fields that you want to save to database
func (r *FakeRepository) Select(query interface{}, args ...interface{}) Repository {
return r
}
// Omit specify fields that you want to ignore when saving to database for creating, updating
func (r *FakeRepository) Omit(columns ...string) Repository {
return r
}
// Group specify the group method on the find
func (r *FakeRepository) Group(query string) Repository {
return r
}
// Having specify HAVING conditions for GROUP BY
func (r *FakeRepository) Having(query interface{}, values ...interface{}) Repository {
return r
}
// Joins specify Joins conditions
// db.Joins("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "[email protected]").Find(&user)
func (r *FakeRepository) Joins(query string, args ...interface{}) Repository {
return r
}
func (r *FakeRepository) Scopes(funcs ...func(Repository) Repository) Repository {
return r
}
// Unscoped return all record including deleted record, refer Soft Delete https://jinzhu.github.io/gorm/crud.html#soft-delete
func (r *FakeRepository) Unscoped() Repository {
return r
}
// Attrs initialize struct with argument if record not found with `FirstOrInit` https://jinzhu.github.io/gorm/crud.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/crud.html#firstorcreate
func (r *FakeRepository) Attrs(attrs ...interface{}) Repository {
return r
}
// Assign assign result with argument regardless it is found or not with `FirstOrInit` https://jinzhu.github.io/gorm/crud.html#firstorinit or `FirstOrCreate` https://jinzhu.github.io/gorm/crud.html#firstorcreate
func (r *FakeRepository) Assign(attrs ...interface{}) Repository {
return r
}
// First find first record that match given conditions, order by primary key
func (r *FakeRepository) First(out interface{}, where ...interface{}) Repository {
r.copyData("First", out)
return r
}
// Take return a record that match given conditions, the order will depend on the database implementation
func (r *FakeRepository) Take(out interface{}, where ...interface{}) Repository {
r.copyData("Take", out)
return r
}
// Last find last record that match given conditions, order by primary key
func (r *FakeRepository) Last(out interface{}, where ...interface{}) Repository {
r.copyData("Last", out)
return r
}
// Find find records that match given conditions
func (r *FakeRepository) Find(out interface{}, where ...interface{}) Repository {
r.copyData("Find", out)
return r
}
// Scan scan value to a struct
func (r *FakeRepository) Scan(dest interface{}) Repository {
r.copyData("Scan", dest)
return r
}
// Row return `*sql.Row` with given conditions
func (r *FakeRepository) Row() *sql.Row {
return nil
}
// Rows return `*sql.Rows` with given conditions
func (r *FakeRepository) Rows() (*sql.Rows, error) {
return nil, nil
}
// ScanRows scan `*sql.Rows` to give struct
func (r *FakeRepository) ScanRows(rows *sql.Rows, result interface{}) error {
return nil
}
// Pluck used to query single column from a model as a map
// var ages []int64
// db.Find(&users).Pluck("age", &ages)
func (r *FakeRepository) Pluck(column string, value interface{}) Repository {
return r
}
// Count get how many records for a model
func (r *FakeRepository) Count(value interface{}) Repository {
return r
}
// Related get related associations
func (r *FakeRepository) Related(value interface{}, foreignKeys ...string) Repository {
return r
}
// FirstOrInit find first matched record or initialize a new one with given conditions (only works with struct, map conditions)
// https://jinzhu.github.io/gorm/crud.html#firstorinit
func (r *FakeRepository) FirstOrInit(out interface{}, where ...interface{}) Repository {
r.copyData("FirstOrInit", out)
return r
}
// FirstOrCreate find first matched record or create a new one with given conditions (only works with struct, map conditions)
// https://jinzhu.github.io/gorm/crud.html#firstorcreate
func (r *FakeRepository) FirstOrCreate(out interface{}, where ...interface{}) Repository {
r.copyData("FirstOrCreate", out)
return r
}
// Update update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (r *FakeRepository) Update(attrs ...interface{}) Repository {
return r
}
// Updates update attributes with callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (r *FakeRepository) Updates(values interface{}, ignoreProtectedAttrs ...bool) Repository {
return r
}
// UpdateColumn update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (r *FakeRepository) UpdateColumn(attrs ...interface{}) Repository {
return r
}
// UpdateColumns update attributes without callbacks, refer: https://jinzhu.github.io/gorm/crud.html#update
func (r *FakeRepository) UpdateColumns(values interface{}) Repository {
return r
}
// Save update value in database, if the value doesn't have primary key, will insert it
func (r *FakeRepository) Save(value interface{}) Repository {
return r
}
// Create insert the value into database
func (r *FakeRepository) Create(value interface{}) Repository {
return r
}
// Delete delete value match given conditions, if the value has primary key, then will including the primary key as condition
func (r *FakeRepository) Delete(value interface{}, where ...interface{}) Repository {
return r
}
// Raw use raw sql as conditions, won't run it unless invoked by other methods
// db.Raw("SELECT name, age FROM users WHERE name = ?", 3).Scan(&result)
func (r *FakeRepository) Raw(sql string, values ...interface{}) Repository {
return r
}
// Exec execute raw sql
func (r *FakeRepository) Exec(sql string, values ...interface{}) Repository {
return r
}
// Model specify the model you would like to run db operations
// // update all users's name to `hello`
// db.Model(&User{}).Update("name", "hello")
// // if user's primary key is non-blank, will use it as condition, then will only update the user's name to `hello`
// db.Model(&user).Update("name", "hello")
func (r *FakeRepository) Model(value interface{}) Repository {
return r
}
// Table specify the table you would like to run db operations
func (r *FakeRepository) Table(name string) Repository {
return r
}
// Debug start debug mode
func (r *FakeRepository) Debug() Repository {
return r
}
// Begin begin a transaction
func (r *FakeRepository) Begin() Repository {
return r
}
// Commit commit a transaction
func (r *FakeRepository) Commit() Repository {
return r
}
// Rollback rollback a transaction
func (r *FakeRepository) Rollback() Repository {
return r
}
// NewRecord check if value's primary key is blank
func (r *FakeRepository) NewRecord(value interface{}) bool {
return false
}
// RecordNotFound check if returning ErrRecordNotFound error
func (r *FakeRepository) RecordNotFound() bool {
return false
}
// CreateTable create table for models
func (r *FakeRepository) CreateTable(models ...interface{}) Repository {
return r
}
// DropTable drop table for models
func (r *FakeRepository) DropTable(values ...interface{}) Repository {
return r
}
// DropTableIfExists drop table if it is exist
func (r *FakeRepository) DropTableIfExists(values ...interface{}) Repository {
return r
}
// HasTable check has table or not
func (r *FakeRepository) HasTable(value interface{}) bool {
return false
}
// AutoMigrate run auto migration for given models, will only add missing fields, won't delete/change current data
func (r *FakeRepository) AutoMigrate(values ...interface{}) Repository {
return r
}
// ModifyColumn modify column to type
func (r *FakeRepository) ModifyColumn(column string, typ string) Repository {
return r
}
// DropColumn drop a column
func (r *FakeRepository) DropColumn(column string) Repository {
return r
}
// AddIndex add index for columns with given name
func (r *FakeRepository) AddIndex(indexName string, columns ...string) Repository {
return r
}
// AddUniqueIndex add unique index for columns with given name
func (r *FakeRepository) AddUniqueIndex(indexName string, columns ...string) Repository {
return r
}
// RemoveIndex remove index with name
func (r *FakeRepository) RemoveIndex(indexName string) Repository {
return r
}
// AddForeignKey Add foreign key to the given scope, e.g:
// db.Model(&User{}).AddForeignKey("city_id", "cities(id)", "RESTRICT", "RESTRICT")
func (r *FakeRepository) AddForeignKey(field string, dest string, onDelete string, onUpdate string) Repository {
return r
}
// RemoveForeignKey Remove foreign key from the given scope, e.g:
// db.Model(&User{}).RemoveForeignKey("city_id", "cities(id)")
func (r *FakeRepository) RemoveForeignKey(field string, dest string) Repository {
return r
}
// Association start `Association Mode` to handler relations things easir in that mode, refer: https://jinzhu.github.io/gorm/associations.html#association-mode
func (r *FakeRepository) Association(column string) *Association {
return nil
}
// Preload preload associations with given conditions
// db.Preload("Orders", "state NOT IN (?)", "cancelled").Find(&users)
func (r *FakeRepository) Preload(column string, conditions ...interface{}) Repository {
return r
}
// Set set setting by name, which could be used in callbacks, will clone a new db, and update its setting
func (r *FakeRepository) Set(name string, value interface{}) Repository {
return r
}
// InstantSet instant set setting, will affect current db
func (r *FakeRepository) InstantSet(name string, value interface{}) Repository {
return r
}
// Get get setting by name
func (r *FakeRepository) Get(name string) (value interface{}, ok bool) {
value, ok = nil, false
return
}
// SetJoinTableHandler set a model's join table handler for a relation
func (r *FakeRepository) SetJoinTableHandler(source interface{}, column string, handler JoinTableHandlerInterface) {
}
// AddError add error to the db
func (r *FakeRepository) AddError(err error) error {
if err != nil {
if err != ErrRecordNotFound {
if r.logMode == 0 {
go r.Print(fileWithLineNum(), err)
} else {
r.Log(err)
}
errs := Errors(r.GetErrors())
errs = errs.Add(err)
if len(errs) > 1 {
err = errs
}
}
r.SetError(err)
}
return err
}
// GetErrors get happened errors from the db
func (r *FakeRepository) GetErrors() []error {
if errs, ok := r.Error().(Errors); ok {
return errs
} else if r.Error() != nil {
return []error{r.Error()}
}
return []error{}
}
func (r *FakeRepository) Value() interface{} {
return r.value
}
func (r *FakeRepository) SetValue(v interface{}) Repository {
r.value = v
return r
}
func (r *FakeRepository) Error() error {
return r.err
}
func (r *FakeRepository) SetError(err error) Repository {
r.err = err
return r
}
func (r *FakeRepository) RowsAffected() int64 {
return r.rowsAffected
}
func (r *FakeRepository) SetRowsAffected(row int64) Repository {
r.rowsAffected = row
return r
}
func (r *FakeRepository) Search() *Search {
return r.search
}
func (r *FakeRepository) SetSearch(search *Search) Repository {
r.search = search
return r
}
func (r *FakeRepository) Parent() Repository {
return r.parent
}
func (r *FakeRepository) SetParent(p Repository) Repository {
r.parent = p
return r
}
func (r *FakeRepository) SQLCommonDB() SQLCommon {
return r.db
}
func (r *FakeRepository) SetSQLCommonDB(sc SQLCommon) Repository {
r.db = sc
return r
}
func (r *FakeRepository) Callbacks() *Callback {
return r.callbacks
}
func (r *FakeRepository) SetCallbacks(cb *Callback) Repository {
r.callbacks = cb
return r
}
func (r *FakeRepository) IsSingularTable() bool {
return r.singularTable
}
func (r *FakeRepository) SetIsSingularTable(singularTable bool) Repository {
r.singularTable = singularTable
return r
}
func (r *FakeRepository) Values() map[string]interface{} {
return r.values
}
func (r *FakeRepository) SetValues(vals map[string]interface{}) Repository {
r.values = vals
return r
}
func (r *FakeRepository) SetDialect(d Dialect) Repository {
r.dialect = d
return r
}
////////////////////////////////////////////////////////////////////////////////
// Private Methods For DB
////////////////////////////////////////////////////////////////////////////////
func (r *FakeRepository) Clone() Repository {
db := &FakeRepository{
db: r.db,
parent: r.parent,
logger: r.logger,
logMode: r.logMode,
values: map[string]interface{}{},
value: r.value,
err: r.Error(),
blockGlobalUpdate: r.blockGlobalUpdate,
dialect: newDialect(r.dialect.GetName(), r.db),
}
for key, value := range r.values {
db.values[key] = value
}
if r.search == nil {
db.search = &Search{limit: -1, offset: -1}
} else {
db.search = r.Search().clone()
}
db.Search().db = db
return db
}
func (r *FakeRepository) Print(v ...interface{}) {
r.logger.Print(v...)
}
func (r *FakeRepository) Log(v ...interface{}) {
if r != nil && r.logMode == 2 {
r.Print(append([]interface{}{"log", fileWithLineNum()}, v...)...)
}
}
func (r *FakeRepository) Slog(sql string, t time.Time, vars ...interface{}) {
if r.logMode == 2 {
r.Print("sql", fileWithLineNum(), NowFunc().Sub(t), sql, vars, r.RowsAffected())
}
}
func (r *FakeRepository) Mock(method string, data interface{}) *FakeRepository {
if r.mockData == nil {
r.mockData = make(map[string]interface{})
}
r.mockData[method] = data
return r
}
func (r *FakeRepository) Expect(err error) {
r.SetError(err)
}
func (r *FakeRepository) copyData(name string, out interface{}) {
md := r.mockData[name]
if md != nil {
copier.Copy(out, md)
}
}