-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongodbv2_test.go
More file actions
53 lines (42 loc) · 1.04 KB
/
mongodbv2_test.go
File metadata and controls
53 lines (42 loc) · 1.04 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
package testdock
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.mongodb.org/mongo-driver/v2/bson"
)
func TestMongoDBV2(t *testing.T) {
t.Parallel()
ctx := context.Background()
// Create test database and return a MongoDB client
db, informer := GetMongoDatabaseV2(t,
DefaultMongoDSN,
WithDockerRepository("mongo"),
WithDockerImage("6.0.20"),
WithMigrations("migrations/mongodb", GolangMigrateFactory),
)
checkInformer(t, DefaultMongoDSN, informer)
// Test collection
collection := db.Collection("test_collection")
// Insert duplicate document
_, err := collection.InsertOne(ctx,
bson.M{
"_id": "test1",
"name": "test1",
})
require.Error(t, err)
// Insert new document
_, err = collection.InsertOne(ctx,
bson.M{
"_id": "test2",
"name": "test2",
})
require.NoError(t, err)
// Query test document
var result struct {
Name string `bson:"name"`
}
err = collection.FindOne(ctx, bson.M{"_id": "test2"}).Decode(&result)
require.NoError(t, err)
require.Equal(t, "test2", result.Name)
}