-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongo_integration_test.go
More file actions
192 lines (172 loc) · 4.69 KB
/
mongo_integration_test.go
File metadata and controls
192 lines (172 loc) · 4.69 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package storage
import (
"context"
"fmt"
"log"
"os"
"testing"
"github.com/ory/dockertest/v3"
"github.com/ory/dockertest/v3/docker"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"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"
)
const (
mongoPort = "27017/tcp"
mongoURI = "mongodb://root:password@localhost"
mongoTestDB = "testDB"
mongoTestCollection = "testCollection"
)
var dbClient *mongo.Client
var testMongoConfig mongoConfig
type item struct {
Id string `bson:"_id"`
Name string `bson:"name"`
Stat int64 `bson:"status"`
}
func (i item) ID() string {
return i.Id
}
func (i item) Status() int64 {
return i.Stat
}
func TestMain(m *testing.M) {
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
// pull mongodb docker image for version 5.0
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "mongo",
Tag: "5.0",
Env: []string{
// username and password for mongodb superuser
"MONGO_INITDB_ROOT_USERNAME=root",
"MONGO_INITDB_ROOT_PASSWORD=password",
},
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
config.RestartPolicy = docker.RestartPolicy{
Name: "no",
}
})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
err = pool.Retry(func() error {
var err error
dbClient, err = mongo.Connect(
context.TODO(),
options.Client().ApplyURI(
fmt.Sprintf("%s:%s", mongoURI, resource.GetPort(mongoPort)),
),
)
if err != nil {
return err
}
return dbClient.Ping(context.TODO(), nil)
})
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
testMongoConfig = mongoConfig{
mongoTestDB,
fmt.Sprintf("%s:%s", mongoURI, resource.GetPort(mongoPort)),
}
// run tests
code := m.Run()
// When you're done, kill and remove the container
if err = pool.Purge(resource); err != nil {
log.Fatalf("Could not purge resource: %s", err)
}
// disconnect mongodb client
if err = dbClient.Disconnect(context.TODO()); err != nil {
panic(err)
}
os.Exit(code)
}
func createItems(t *testing.T) []interface{} {
t.Helper()
db := dbClient.Database(mongoTestDB).Collection(mongoTestCollection)
docs := []interface{}{
bson.D{primitive.E{Key: "name", Value: "Bob"}},
bson.D{primitive.E{Key: "name", Value: "Alice"}},
}
results, err := db.InsertMany(context.TODO(), docs)
assert.NoError(t, err)
return results.InsertedIDs
}
func tearDown(t *testing.T) {
t.Helper()
db := dbClient.Database(mongoTestDB).Collection(mongoTestCollection)
err := db.Drop(context.TODO())
require.NoError(t, err)
}
func TestCreate(t *testing.T) {
defer tearDown(t)
newItem := item{Name: "Bob"}
ctx := context.Background()
mongo, err1 := NewMongoDB[item](ctx, testMongoConfig, mongoTestCollection)
require.NoError(t, err1)
t.Run("Create item", func(t *testing.T) {
err2 := mongo.Create(&newItem)
assert.NoError(t, err2)
})
}
func TestFindOne(t *testing.T) {
defer tearDown(t)
ctx := context.Background()
ids := createItems(t)
mongo, err1 := NewMongoDB[item](ctx, testMongoConfig, mongoTestCollection)
require.NoError(t, err1)
t.Run("Find one item", func(t *testing.T) {
item, err2 := mongo.FindOne(ids[0].(primitive.ObjectID).Hex())
assert.Equal(t, item.Name, "Bob")
assert.NoError(t, err2)
})
}
func TestUpdate(t *testing.T) {
defer tearDown(t)
ctx := context.Background()
ids := createItems(t)
mongo, err1 := NewMongoDB[item](ctx, testMongoConfig, mongoTestCollection)
require.NoError(t, err1)
t.Run("Update item", func(t *testing.T) {
id := ids[0].(primitive.ObjectID).Hex()
item := item{
Id: id,
Name: "Bill",
Stat: 200,
}
err2 := mongo.Update(item)
assert.NoError(t, err2)
})
}
func TestDelete(t *testing.T) {
defer tearDown(t)
ctx := context.Background()
ids := createItems(t)
mongo, err1 := NewMongoDB[item](ctx, testMongoConfig, mongoTestCollection)
require.NoError(t, err1)
t.Run("Delete item", func(t *testing.T) {
err2 := mongo.Delete(ids[0].(primitive.ObjectID).Hex())
assert.NoError(t, err2)
})
}
func TestFindAll(t *testing.T) {
defer tearDown(t)
ctx := context.Background()
ids := createItems(t)
mongo, err1 := NewMongoDB[item](ctx, testMongoConfig, mongoTestCollection)
require.NoError(t, err1)
t.Run("Find all items", func(t *testing.T) {
results, err2 := mongo.FindAll()
assert.Equal(t, len(ids), len(results))
assert.NoError(t, err2)
})
}