-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate_test.go
More file actions
103 lines (99 loc) · 2.74 KB
/
Copy pathaggregate_test.go
File metadata and controls
103 lines (99 loc) · 2.74 KB
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
96
97
98
99
100
101
102
103
package easymongo_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/tophergopher/easymongo"
"go.mongodb.org/mongo-driver/bson"
)
func TestAggregate(t *testing.T) {
setup(t)
coll := createBatmanArchive(t)
t.Run("Aggregate().One()", func(t *testing.T) {
is := assert.New(t)
// This aggregation query just sums up the number of documents that have notes on them
// (Which should be 3)
pipe := []bson.M{
0: {
"$match": bson.M{
"notes": bson.M{"$ne": nil},
},
},
1: {
"$count": "total",
},
}
var result struct {
Total int `bson:"total"`
}
err := coll.Aggregate(pipe).One(&result)
is.NoError(err, "could not Aggregate().One()")
is.Equal(3, result.Total, "the count appears to be incorrect from the aggregation result")
})
t.Run("Aggregate().All()", func(t *testing.T) {
is := assert.New(t)
pipe := []bson.M{
0: {
// Look for all objects
"$match": bson.M{},
},
1: {
"$group": bson.M{
"_id": "$deceased",
"total": bson.M{
"$sum": 1,
},
},
},
}
var result []struct {
Deceased bool `bson:"_id"`
Total int `bson:"total"`
}
err := coll.Aggregate(pipe).All(&result)
is.NoError(err, "could not Aggregate().All()")
is.Len(result, 2, "two categories should be returned from the group")
if len(result) != 2 {
t.FailNow()
}
for _, res := range result {
if res.Deceased {
is.Equal(1, res.Total, "there should only be 1 document in this group")
} else {
is.GreaterOrEqual(res.Total, 5, "there should be at least 5 documents in this group")
}
}
// output, err := coll.Aggregate(pipe).FetchRawResult()
// is.NoError(err, "could not fetch the raw output of the query")
// _ = output
// is.GreaterOrEqual(len(output), 0, "the raw output appears empty")
// fmt.Println(output)
})
t.Run("Aggregate().One() with options", func(t *testing.T) {
is := assert.New(t)
pipe := []bson.M{
0: {
"$match": bson.M{
"notes": bson.M{"$ne": nil},
},
},
1: {
"$count": "total",
},
}
var result struct {
Total int `bson:"total"`
}
err := coll.Aggregate(pipe).Timeout(time.Minute * 2).One(&result)
is.NoError(err, "could not Aggregate().One() with a Timeout")
is.Equal(3, result.Total, "the count appears to be incorrect from the aggregation result")
ctx := context.Background()
err = coll.Aggregate(pipe).WithContext(ctx).One(&result)
is.NoError(err, "could not Aggregate().One() using a Context")
is.Equal(3, result.Total, "the count appears to be incorrect from the aggregation result")
// Set a timeout of 0 so we trigger an error
err = coll.Aggregate(pipe).Timeout(0).One(&result)
is.Equal(easymongo.ErrTimeoutOccurred, err, "A timeout was expected")
})
}