-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdistinct_test.go
47 lines (40 loc) · 1 KB
/
distinct_test.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
// Copyright 2019 Kuei-chun Chen. All rights reserved.
package examples
import (
"context"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
func TestDistinct(t *testing.T) {
var err error
var client *mongo.Client
var collection *mongo.Collection
var cur *mongo.Cursor
var ctx = context.Background()
if client, err = getMongoClient(); err != nil {
t.Fatal(err)
}
defer client.Disconnect(ctx)
seedCarsData(client, dbName)
collection = client.Database(dbName).Collection(collectionName)
filter := bson.D{{}}
if cur, err = collection.Find(ctx, filter); err != nil {
t.Fatal(err)
}
defer cur.Close(ctx)
amap := map[string]string{}
var doc bson.M
for cur.Next(ctx) {
cur.Decode(&doc)
brand := doc["brand"].(string)
amap[brand] = brand
}
var brands []interface{}
if brands, err = collection.Distinct(ctx, "brand", filter); err != nil {
t.Fatal(err)
}
if len(brands) != len(amap) {
t.Fatal("find failed, expected", len(amap), "but got", len(brands))
}
}