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