-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmutate.go
151 lines (129 loc) · 3.82 KB
/
mutate.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
package reltest
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/go-rel/rel"
)
type mutate []*MockMutate
func (m *mutate) register(name string, ctxData ctxData, mutators ...rel.Mutator) *MockMutate {
mm := &MockMutate{
assert: &Assert{ctxData: ctxData, repeatability: 1},
name: name,
argMutators: mutators,
}
*m = append(*m, mm)
return mm
}
func (m mutate) execute(name string, ctx context.Context, entity any, mutators ...rel.Mutator) error {
for _, mm := range m {
if (mm.argEntity == nil || reflect.DeepEqual(mm.argEntity, entity)) &&
(mm.argEntityType == "" || mm.argEntityType == reflect.TypeOf(entity).String()) &&
(mm.argEntityTable == "" || mm.argEntityTable == rel.NewDocument(entity, true).Table()) &&
(mm.argEntityContains == nil || matchContains(mm.argEntityContains, entity)) &&
(mm.argMutators == nil || matchMutators(mm.argMutators, mutators)) &&
mm.assert.call(ctx) {
return mm.retError
}
}
mm := &MockMutate{
assert: &Assert{ctxData: fetchContext(ctx)},
name: name,
argEntity: entity,
argMutators: mutators,
}
panic(failExecuteMessage(mm, m))
}
func (m *mutate) assert(t TestingT) bool {
t.Helper()
for _, mm := range *m {
if !mm.assert.assert(t, mm) {
return false
}
}
*m = nil
return true
}
// MockMutate asserts and simulate Insert function for test.
type MockMutate struct {
assert *Assert
name string
argEntity any
argEntityType string
argEntityTable string
argEntityContains any
argMutators []rel.Mutator
retError error
}
// For assert calls for given entity.
func (mm *MockMutate) For(entity any) *MockMutate {
mm.argEntity = entity
return mm
}
// ForType assert calls for given type.
// Type must include package name, example: `model.User`.
func (mm *MockMutate) ForType(typ string) *MockMutate {
mm.argEntityType = "*" + strings.TrimPrefix(typ, "*")
return mm
}
// ForTable assert calls for given table.
func (mm *MockMutate) ForTable(typ string) *MockMutate {
mm.argEntityTable = typ
return mm
}
// ForContains assert calls to contains some value of given struct.
func (mm *MockMutate) ForContains(contains any) *MockMutate {
mm.argEntityContains = contains
return mm
}
// Error sets error to be returned.
func (mm *MockMutate) Error(err error) *Assert {
mm.retError = err
return mm.assert
}
// Success sets no error to be returned.
func (mm *MockMutate) Success() *Assert {
return mm.Error(nil)
}
// ConnectionClosed sets this error to be returned.
func (mm *MockMutate) ConnectionClosed() *Assert {
return mm.Error(ErrConnectionClosed)
}
// NotUnique sets not unique error to be returned.
func (mm *MockMutate) NotUnique(key string) *Assert {
return mm.Error(rel.ConstraintError{
Key: key,
Type: rel.UniqueConstraint,
})
}
// String representation of mocked call.
func (mm MockMutate) String() string {
argEntity := "<Any>"
if mm.argEntity != nil {
argEntity = csprint(mm.argEntity, true)
} else if mm.argEntityContains != nil {
argEntity = fmt.Sprintf("<Contains: %s>", csprint(mm.argEntityContains, true))
} else if mm.argEntityType != "" {
argEntity = fmt.Sprintf("<Type: %s>", mm.argEntityType)
} else if mm.argEntityTable != "" {
argEntity = fmt.Sprintf("<Table: %s>", mm.argEntityTable)
}
argMutators := ""
for i := range mm.argMutators {
argMutators += fmt.Sprintf(", %v", mm.argMutators[i])
}
return mm.assert.sprintf("%s(ctx, %s%s)", mm.name, argEntity, argMutators)
}
// ExpectString representation of mocked call.
func (mm MockMutate) ExpectString() string {
argMutators := ""
for i := range mm.argMutators {
if i > 0 {
argMutators += fmt.Sprintf(", %v", mm.argMutators[i])
} else {
argMutators += fmt.Sprintf("%v", mm.argMutators[i])
}
}
return mm.assert.sprintf("Expect%s(%s).ForType(\"%T\")", mm.name, argMutators, mm.argEntity)
}