-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathreplace_test.go
118 lines (110 loc) · 3.5 KB
/
replace_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
// Copyright 2018 Kuei-chun Chen. All rights reserved.
package examples
import (
"context"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func TestReplaceOne(t *testing.T) {
var err error
var client *mongo.Client
var collection *mongo.Collection
var ctx = context.Background()
var doc = bson.M{"_id": primitive.NewObjectID(), "hometown": "Atlanta"}
var result *mongo.UpdateResult
if client, err = getMongoClient(); err != nil {
t.Fatal(err)
}
defer client.Disconnect(ctx)
collection = client.Database(dbName).Collection(collectionExamples)
if _, err = collection.InsertOne(ctx, doc); err != nil {
t.Fatal(err)
}
doc["year"] = 1998
if result, err = collection.ReplaceOne(ctx, bson.M{"_id": doc["_id"]}, doc); err != nil {
t.Fatal(err)
}
if result.MatchedCount != 1 || result.ModifiedCount != 1 {
t.Fatal("replace failed, expected 1 but got", result.MatchedCount)
}
res, _ := collection.DeleteMany(ctx, bson.M{"hometown": "Atlanta"})
if res.DeletedCount != 1 {
t.Fatal("replace failed, expected 1 but got", res.DeletedCount)
}
}
func TestReplaceLoop(t *testing.T) {
var err error
var client *mongo.Client
var collection *mongo.Collection
var cur *mongo.Cursor
var result *mongo.UpdateResult
var ctx = context.Background()
var docs []interface{}
docs = append(docs, bson.M{"hometown": "Atlanta", "year": 1998})
docs = append(docs, bson.M{"hometown": "Jacksonville", "year": 1990})
if client, err = getMongoClient(); err != nil {
t.Fatal(err)
}
defer client.Disconnect(ctx)
collection = client.Database(dbName).Collection(collectionExamples)
if _, err = collection.InsertMany(ctx, docs); err != nil {
t.Fatal(err)
}
if cur, err = collection.Find(ctx, bson.M{"hometown": bson.M{"$exists": 1}}); err != nil {
t.Fatal(err)
}
var doc bson.M
for cur.Next(ctx) {
cur.Decode(&doc)
doc["updated"] = time.Now()
if result, err = collection.ReplaceOne(ctx, bson.M{"_id": doc["_id"]}, doc); err != nil {
t.Fatal(err)
}
if result.MatchedCount != 1 || result.ModifiedCount != 1 {
t.Fatal("replace failed, expected 1 but got", result.MatchedCount)
}
}
res, _ := collection.DeleteMany(ctx, bson.M{"hometown": bson.M{"$exists": 1}})
if res.DeletedCount != int64(len(docs)) {
t.Fatal("replace failed, expected", len(docs), "but got", res.DeletedCount)
}
}
func TestFindOneAndReplace(t *testing.T) {
var err error
var client *mongo.Client
var collection *mongo.Collection
var ctx = context.Background()
year := int32(time.Now().Year())
var doc = bson.M{"hometown": "Atlanta", "year": int32(1998)}
var docs []interface{}
for i := 0; i < 3; i++ {
docs = append(docs, doc)
}
if client, err = getMongoClient(); err != nil {
t.Fatal(err)
}
defer client.Disconnect(ctx)
collection = client.Database(dbName).Collection(collectionExamples)
if _, err = collection.InsertMany(ctx, docs); err != nil {
t.Fatal(err)
}
doc["year"] = year
opts := options.FindOneAndReplace()
if err = collection.FindOneAndReplace(ctx, bson.M{"hometown": "Atlanta"}, doc, opts).Decode(&doc); err != nil {
t.Fatal(err)
}
if err = collection.FindOne(ctx, bson.M{"_id": doc["_id"]}).Decode(&doc); err != nil {
t.Fatal(err)
}
if doc["year"] != year {
t.Fatal("expected", year, "but got", doc["year"])
}
res, _ := collection.DeleteMany(ctx, bson.M{"hometown": "Atlanta"})
if res.DeletedCount != int64(len(docs)) {
t.Fatal("delete failed, expected 1 but got", res.DeletedCount)
}
}