forked from go-rel/reltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_and_count_all.go
92 lines (77 loc) · 2.25 KB
/
find_and_count_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
package reltest
import (
"context"
"reflect"
"github.com/go-rel/rel"
)
type findAndCountAll []*MockFindAndCountAll
func (fca *findAndCountAll) register(ctxData ctxData, queriers ...rel.Querier) *MockFindAndCountAll {
mfca := &MockFindAndCountAll{
assert: &Assert{ctxData: ctxData, repeatability: 1},
argQuery: rel.Build("", queriers...),
}
*fca = append(*fca, mfca)
return mfca
}
func (fca findAndCountAll) execute(ctx context.Context, entities any, queriers ...rel.Querier) (int, error) {
query := rel.Build("", queriers...)
for _, mfca := range fca {
if matchQuery(mfca.argQuery, query) &&
mfca.assert.call(ctx) {
if mfca.argEntities != nil {
reflect.ValueOf(entities).Elem().Set(reflect.ValueOf(mfca.argEntities))
}
return mfca.retCount, mfca.retError
}
}
mfca := &MockFindAndCountAll{
assert: &Assert{ctxData: fetchContext(ctx)},
argQuery: query,
argEntities: entities,
}
panic(failExecuteMessage(mfca, fca))
}
func (fca *findAndCountAll) assert(t TestingT) bool {
t.Helper()
for _, mfca := range *fca {
if !mfca.assert.assert(t, mfca) {
return false
}
}
*fca = nil
return true
}
// MockFindAndCountAll asserts and simulate find and count all function for test.
type MockFindAndCountAll struct {
assert *Assert
argQuery rel.Query
argEntities any
retCount int
retError error
}
// Result sets the result of this query.
func (mfca *MockFindAndCountAll) Result(result any, count int) *Assert {
if mfca.argQuery.Table == "" {
mfca.argQuery.Table = rel.NewCollection(result, true).Table()
}
mfca.argEntities = result
mfca.retCount = count
return mfca.assert
}
// Error sets error to be returned.
func (mfca *MockFindAndCountAll) Error(err error) *Assert {
mfca.retError = err
return mfca.assert
}
// ConnectionClosed sets this error to be returned.
func (mfca *MockFindAndCountAll) ConnectionClosed() *Assert {
return mfca.Error(ErrConnectionClosed)
}
// String representation of mocked call.
func (mfca MockFindAndCountAll) String() string {
return mfca.assert.sprintf("FindAndCountAll(ctx, <Any>, %s)", mfca.argQuery)
}
// ExpectString representation of mocked call.
func (mfca MockFindAndCountAll) ExpectString() string {
return mfca.assert.sprintf("ExpectFindAndCountAll(%s)", mfca.argQuery)
}