-
Notifications
You must be signed in to change notification settings - Fork 1
/
testdb_test.go
136 lines (112 loc) · 2.95 KB
/
testdb_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
// Package testdb_test provides tests for testdb.
package testdb_test
import (
"context"
"os"
"testing"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/mongo-go/testdb"
)
var (
defaultUrl = "mongodb://localhost"
defaultDb = "mongo-go"
defaultTimeout = time.Duration(2) * time.Second
)
// A quick smoke test to make sure the basics work.
func TestTestDB(t *testing.T) {
testDb := testdb.NewTestDB(defaultUrl, defaultDb, defaultTimeout)
// CreateRandomCollection errors if called before Connect.
coll, err := testDb.CreateRandomCollection(testdb.NoIndexes)
if err == nil {
t.Fatal("expected an error, did not get one")
}
// Connect to the db.
if err = testDb.Connect(); err != nil {
t.Fatal(err)
}
defer testDb.Close()
// Calling OverrideWithEnvVars after Connect does nothing.
testDb.OverrideWithEnvVars()
indexes := []mongo.IndexModel{
{
Keys: bson.D{{"iamunique", 1}},
Options: options.Index().SetUnique(true),
},
}
// Create a collection with a unique index.
coll, err = testDb.CreateRandomCollection(indexes)
if err != nil {
t.Error(err)
}
defer coll.Drop(context.Background())
doc := map[string]string{
"iamunique": "a",
}
_, err = coll.InsertOne(context.Background(), doc)
if err != nil {
t.Error(err)
}
// Should get a duplicate key error when we try to insert the same doc.
_, err = coll.InsertOne(context.Background(), doc)
if !testdb.IsDupeKeyError(err) {
t.Errorf("expected a duplicate key error, did not get one (err: %s)", err)
}
}
func TestCreateCollectionInvalidIndex(t *testing.T) {
testDb := testdb.NewTestDB(defaultUrl, defaultDb, defaultTimeout)
if err := testDb.Connect(); err != nil {
t.Fatal(err)
}
defer testDb.Close()
// An invalid index.
indexes := []mongo.IndexModel{
{
Keys: bson.D{{"", 1}},
},
}
coll, err := testDb.CreateRandomCollection(indexes)
if err == nil {
t.Error("expected an error, did not get one")
}
if coll != nil {
coll.Drop(context.Background())
}
}
func TestEnvVarOverride(t *testing.T) {
url := "jibberish:99999999999"
db := "another_db"
testDb := testdb.NewTestDB(url, db, defaultTimeout)
if err := os.Setenv(testdb.ENV_VAR_TEST_MONGO_URL, defaultUrl); err != nil {
t.Error(err)
}
if err := os.Setenv(testdb.ENV_VAR_TEST_MONGO_DB, defaultDb); err != nil {
t.Error(err)
}
testDb.OverrideWithEnvVars()
err := testDb.Connect()
if err != nil {
t.Fatal(err)
}
}
func TestInvalidUrl(t *testing.T) {
url := "thisis?invalid"
db := "test"
testDb := testdb.NewTestDB(url, db, defaultTimeout)
err := testDb.Connect()
if err == nil {
t.Fatal("expected an error, did not get one")
}
}
func TestConnectError(t *testing.T) {
url := "jibberish:99999999999"
db := "test"
timeout := time.Duration(100) * time.Millisecond
testDb := testdb.NewTestDB(url, db, timeout)
err := testDb.Connect()
if err == nil {
t.Fatal("expected an error, did not get one")
}
}