forked from go-rel/reltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_any.go
111 lines (92 loc) · 2.7 KB
/
update_any.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
package reltest
import (
"context"
"fmt"
"github.com/go-rel/rel"
)
type updateAny []*MockUpdateAny
func (ua *updateAny) register(ctxData ctxData, query rel.Query, mutates ...rel.Mutate) *MockUpdateAny {
mua := &MockUpdateAny{
assert: &Assert{ctxData: ctxData, repeatability: 1},
argQuery: query,
argMutates: mutates,
}
*ua = append(*ua, mua)
return mua
}
func (ua updateAny) execute(ctx context.Context, query rel.Query, mutates ...rel.Mutate) (int, error) {
for _, mua := range ua {
if matchQuery(mua.argQuery, query) &&
matchMutates(mua.argMutates, mutates) &&
mua.assert.call(ctx) {
if query.Table == "" {
panic("reltest: Cannot call UpdateAny without table. use rel.From(tableName)")
}
if !mua.unsafe && query.WhereQuery.None() {
panic("reltest: unsafe UpdateAny detected. if you want to mutate all entities without filter, please use call .Unsafe()")
}
return mua.retUpdatedCount, mua.retError
}
}
mua := &MockUpdateAny{
assert: &Assert{ctxData: fetchContext(ctx)},
argQuery: query,
argMutates: mutates,
}
panic(failExecuteMessage(mua, ua))
}
func (ua *updateAny) assert(t TestingT) bool {
t.Helper()
for _, mua := range *ua {
if !mua.assert.assert(t, mua) {
return false
}
}
*ua = nil
return true
}
// MockUpdateAny asserts and simulate UpdateAny function for test.
type MockUpdateAny struct {
assert *Assert
unsafe bool
argQuery rel.Query
argMutates []rel.Mutate
retUpdatedCount int
retError error
}
// Unsafe allows for unsafe operation to delete entities without where condition.
func (mua *MockUpdateAny) Unsafe() *MockUpdateAny {
mua.unsafe = true
return mua
}
// UpdatedCount set the returned deleted count of this function.
func (mua *MockUpdateAny) UpdatedCount(updatedCount int) *Assert {
mua.retUpdatedCount = updatedCount
return mua.assert
}
// Error sets error to be returned.
func (mua *MockUpdateAny) Error(err error) *Assert {
mua.retUpdatedCount = 0
mua.retError = err
return mua.assert
}
// ConnectionClosed sets this error to be returned.
func (mua *MockUpdateAny) ConnectionClosed() *Assert {
return mua.Error(ErrConnectionClosed)
}
// String representation of mocked call.
func (mua MockUpdateAny) String() string {
argMutates := ""
for i := range mua.argMutates {
argMutates += fmt.Sprintf(", %v", mua.argMutates[i])
}
return mua.assert.sprintf("UpdateAny(ctx, %s%s)", mua.argQuery, argMutates)
}
// ExpectString representation of mocked call.
func (mua MockUpdateAny) ExpectString() string {
argMutates := ""
for i := range mua.argMutates {
argMutates += fmt.Sprintf(", %v", mua.argMutates[i])
}
return mua.assert.sprintf("ExpectUpdateAny(%s%s)", mua.argQuery, argMutates)
}