forked from qiniu/qmgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
149 lines (125 loc) · 3.46 KB
/
example_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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package qmgo
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
const (
URI = "mongodb://localhost:27017"
DATABASE = "class"
COLL = "user"
)
type BsonT map[string]interface{}
type UserInfo struct {
Name string `bson:"name"`
Age uint16 `bson:"age"`
Weight uint32 `bson:"weight"`
}
var oneUserInfo = UserInfo{
Name: "xm",
Age: 7,
Weight: 40,
}
var batchUserInfo = []UserInfo{
{Name: "a1", Age: 6, Weight: 20},
{Name: "b2", Age: 6, Weight: 25},
{Name: "c3", Age: 6, Weight: 30},
{Name: "d4", Age: 6, Weight: 35},
{Name: "a1", Age: 7, Weight: 40},
{Name: "a1", Age: 8, Weight: 45},
}
var batchUserInfoI = []interface{}{
UserInfo{Name: "a1", Age: 6, Weight: 20},
UserInfo{Name: "b2", Age: 6, Weight: 25},
UserInfo{Name: "c3", Age: 6, Weight: 30},
UserInfo{Name: "d4", Age: 6, Weight: 35},
UserInfo{Name: "a1", Age: 7, Weight: 40},
UserInfo{Name: "a1", Age: 8, Weight: 45},
}
func TestQmgo(t *testing.T) {
ast := require.New(t)
ctx := context.Background()
// create connect
cli, err := Open(ctx, &Config{Uri: URI, Database: DATABASE, Coll: COLL})
ast.Nil(err)
defer func() {
if err = cli.Close(ctx); err != nil {
panic(err)
}
}()
defer cli.DropDatabase(ctx)
cli.EnsureIndexes(ctx, []string{"_id"}, []string{"age", "name,weight"})
// insert one document
_, err = cli.InsertOne(ctx, oneUserInfo)
ast.Nil(err)
// find one document
one := UserInfo{}
err = cli.Find(ctx, BsonT{"name": oneUserInfo.Name}).One(&one)
ast.Nil(err)
ast.Equal(oneUserInfo, one)
// multiple insert
_, err = cli.Collection.InsertMany(ctx, batchUserInfoI)
ast.Nil(err)
// find all 、sort and limit
batch := []UserInfo{}
cli.Find(ctx, BsonT{"age": 6}).Sort("weight").Limit(7).All(&batch)
ast.Equal(4, len(batch))
count, err := cli.Find(ctx, BsonT{"age": 6}).Count()
ast.NoError(err)
ast.Equal(int64(4), count)
// aggregate
matchStage := bson.D{{"$match", []bson.E{{"weight", bson.D{{"$gt", 30}}}}}}
groupStage := bson.D{{"$group", bson.D{{"_id", "$name"}, {"total", bson.D{{"$sum", "$age"}}}}}}
var showsWithInfo []bson.M
err = cli.Aggregate(context.Background(), Pipeline{matchStage, groupStage}).All(&showsWithInfo)
ast.Equal(3, len(showsWithInfo))
for _, v := range showsWithInfo {
if "a1" == v["_id"] {
ast.Equal(int32(15), v["total"])
continue
}
if "d4" == v["_id"] {
ast.Equal(int32(6), v["total"])
continue
}
ast.Error(errors.New("error"), "impossible")
}
//remove
err = cli.Remove(ctx, BsonT{"age": 7})
ast.Nil(err)
}
func TestOfficialMongoDriver(t *testing.T) {
ast := require.New(t)
ctx := context.Background()
// create connect
var opts *options.ClientOptions
opts = new(options.ClientOptions)
opts.ApplyURI(URI)
c, err := mongo.Connect(ctx, opts)
ast.Nil(err)
db := c.Database(DATABASE)
coll := db.Collection(COLL)
defer db.Drop(ctx)
// insert one document
_, err = coll.InsertOne(ctx, oneUserInfo)
ast.Nil(err)
// find one document
one := UserInfo{}
err = coll.FindOne(ctx, BsonT{"name": oneUserInfo.Name}).Decode(&one)
ast.Nil(err)
// batch insert
_, err = coll.InsertMany(ctx, batchUserInfoI)
ast.Nil(err)
// find all 、sort and limit
findOptions := options.Find()
findOptions.SetLimit(7)
var sorts bson.D
sorts = append(sorts, bson.E{Key: "weight", Value: 1})
findOptions.SetSort(sorts)
_, err = coll.Find(ctx, BsonT{"age": 6}, findOptions)
ast.Nil(err)
}