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