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