forked from go-rel/reltest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount.go
86 lines (72 loc) · 1.94 KB
/
count.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
package reltest
import (
"context"
"github.com/go-rel/rel"
)
type count []*MockCount
func (c *count) register(ctxData ctxData, collection string, queriers ...rel.Querier) *MockCount {
mc := &MockCount{
assert: &Assert{ctxData: ctxData, repeatability: 1},
argCollection: collection,
argQuery: rel.Build(collection, queriers...),
}
*c = append(*c, mc)
return mc
}
func (c count) execute(ctx context.Context, collection string, queriers ...rel.Querier) (int, error) {
query := rel.Build(collection, queriers...)
for _, mc := range c {
if mc.argCollection == collection &&
matchQuery(mc.argQuery, query) &&
mc.assert.call(ctx) {
return mc.retCount, mc.retError
}
}
mc := &MockCount{
assert: &Assert{ctxData: fetchContext(ctx)},
argCollection: collection,
argQuery: query,
}
panic(failExecuteMessage(mc, c))
}
func (c *count) assert(t TestingT) bool {
t.Helper()
for _, mc := range *c {
if !mc.assert.assert(t, mc) {
return false
}
}
*c = nil
return true
}
// MockCount asserts and simulate UpdateAny function for test.
type MockCount struct {
assert *Assert
argCollection string
argQuery rel.Query
retCount int
retError error
}
// Result sets the result of this query.
func (mc *MockCount) Result(count int) *Assert {
mc.retCount = count
return mc.assert
}
// Error sets error to be returned.
func (mc *MockCount) Error(err error) *Assert {
mc.retError = err
return mc.assert
}
// ConnectionClosed sets this error to be returned.
func (mc *MockCount) ConnectionClosed() *Assert {
mc.Error(ErrConnectionClosed)
return mc.assert
}
// String representation of mocked call.
func (mc MockCount) String() string {
return mc.assert.sprintf(`Count(ctx, "%s", %s)`, mc.argCollection, mc.argQuery)
}
// ExpectString representation of mocked call.
func (mc MockCount) ExpectString() string {
return mc.assert.sprintf(`ExpectCount("%s", %s)`, mc.argCollection, mc.argQuery)
}