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