-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsert_all.go
120 lines (101 loc) · 2.9 KB
/
insert_all.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
package reltest
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/go-rel/rel"
)
type insertAll []*MockInsertAll
func (ia *insertAll) register(ctxData ctxData) *MockInsertAll {
mia := &MockInsertAll{
assert: &Assert{ctxData: ctxData, repeatability: 1},
}
*ia = append(*ia, mia)
return mia
}
func (ia insertAll) execute(ctx context.Context, entities any) error {
for _, mia := range ia {
if (mia.argEntity == nil || reflect.DeepEqual(mia.argEntity, entities)) &&
(mia.argEntityType == "" || mia.argEntityType == reflect.TypeOf(entities).String()) &&
(mia.argEntityTable == "" || mia.argEntityTable == rel.NewCollection(entities, true).Table()) &&
mia.assert.call(ctx) {
return mia.retError
}
}
mia := &MockInsertAll{
assert: &Assert{ctxData: fetchContext(ctx)},
argEntity: entities,
}
panic(failExecuteMessage(mia, ia))
}
func (ia *insertAll) assert(t TestingT) bool {
t.Helper()
for _, mia := range *ia {
if !mia.assert.assert(t, mia) {
return false
}
}
*ia = nil
return true
}
// MockInsertAll asserts and simulate Insert function for test.
type MockInsertAll struct {
assert *Assert
argEntity any
argEntityType string
argEntityTable string
retError error
}
// For assert calls for given entity.
func (mia *MockInsertAll) For(entity any) *MockInsertAll {
mia.argEntity = entity
return mia
}
// ForType assert calls for given type.
// Type must include package name, example: `model.User`.
func (mia *MockInsertAll) ForType(typ string) *MockInsertAll {
mia.argEntityType = "*" + strings.TrimPrefix(typ, "*")
return mia
}
// ForTable assert calls for given table.
func (mia *MockInsertAll) ForTable(typ string) *MockInsertAll {
mia.argEntityTable = typ
return mia
}
// Error sets error to be returned.
func (mia *MockInsertAll) Error(err error) *Assert {
mia.retError = err
return mia.assert
}
// Success sets no error to be returned.
func (mia *MockInsertAll) Success() *Assert {
return mia.Error(nil)
}
// ConnectionClosed sets this error to be returned.
func (mia *MockInsertAll) ConnectionClosed() *Assert {
return mia.Error(ErrConnectionClosed)
}
// NotUnique sets not unique error to be returned.
func (mia *MockInsertAll) NotUnique(key string) *Assert {
return mia.Error(rel.ConstraintError{
Key: key,
Type: rel.UniqueConstraint,
})
}
// String representation of mocked call.
func (mia MockInsertAll) String() string {
argEntity := "<Any>"
if mia.argEntity != nil {
argEntity = csprint(mia.argEntity, true)
} else if mia.argEntityType != "" {
argEntity = fmt.Sprintf("<Type: %s>", mia.argEntityType)
} else if mia.argEntityTable != "" {
argEntity = fmt.Sprintf("<Table: %s>", mia.argEntityTable)
}
return mia.assert.sprintf("InsertAll(ctx, %s)", argEntity)
}
// ExpectString representation of mocked call.
func (mia MockInsertAll) ExpectString() string {
return mia.assert.sprintf("InsertAll().ForType(\"%T\")", mia.argEntity)
}