-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind.go
95 lines (79 loc) · 2.01 KB
/
find.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
package reltest
import (
"context"
"reflect"
"github.com/go-rel/rel"
)
type find []*MockFind
func (f *find) register(ctxData ctxData, queriers ...rel.Querier) *MockFind {
mf := &MockFind{
assert: &Assert{ctxData: ctxData, repeatability: 1},
argQuery: rel.Build("", queriers...),
}
*f = append(*f, mf)
return mf
}
func (f find) execute(ctx context.Context, entity any, queriers ...rel.Querier) error {
query := rel.Build("", queriers...)
for _, mf := range f {
if matchQuery(mf.argQuery, query) &&
mf.assert.call(ctx) {
if mf.argEntity != nil {
reflect.ValueOf(entity).Elem().Set(reflect.ValueOf(mf.argEntity))
}
return mf.retError
}
}
mf := &MockFind{
assert: &Assert{ctxData: fetchContext(ctx)},
argQuery: query,
argEntity: entity,
}
panic(failExecuteMessage(mf, f))
}
func (f *find) assert(t TestingT) bool {
t.Helper()
for _, mf := range *f {
if !mf.assert.assert(t, mf) {
return false
}
}
*f = nil
return true
}
// MockFind asserts and simulate find function for test.
type MockFind struct {
assert *Assert
argQuery rel.Query
argEntity any
retError error
}
// Result sets the result of this query.
func (mf *MockFind) Result(result any) *Assert {
if mf.argQuery.Table == "" {
mf.argQuery.Table = rel.NewDocument(result, true).Table()
}
mf.argEntity = result
return mf.assert
}
// Error sets error to be returned.
func (mf *MockFind) Error(err error) *Assert {
mf.retError = err
return mf.assert
}
// ConnectionClosed sets this error to be returned.
func (mf *MockFind) ConnectionClosed() *Assert {
return mf.Error(ErrConnectionClosed)
}
// NotFound sets NotFoundError to be returned.
func (mf *MockFind) NotFound() *Assert {
return mf.Error(rel.NotFoundError{})
}
// String representation of mocked call.
func (mf MockFind) String() string {
return mf.assert.sprintf("Find(ctx, <Any>, %s)", mf.argQuery)
}
// ExpectString representation of mocked call.
func (mf MockFind) ExpectString() string {
return mf.assert.sprintf("ExpectFind(%s)", mf.argQuery)
}