-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration-local-mongo_test.go
51 lines (41 loc) · 1.09 KB
/
integration-local-mongo_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
//go:build !integration
// +build !integration
package function
import (
"context"
"fmt"
"os"
"testing"
"time"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var dbClient *mongo.Client
var dbName = "url-shortener-test"
var urlCollName = "urls"
// TestMain will only run if the build tags do not contain "integration". This TestMain will setup a connection to a local database and runs much faster than the dockertest setup.
func TestMain(m *testing.M) {
mongoURI := os.Getenv("MONGO_URI")
if len(mongoURI) == 0 {
mongoURI = fmt.Sprintf("mongodb://localhost:27017/%s", dbName)
}
client, err := mongo.Connect(
context.TODO(),
options.Client().ApplyURI(mongoURI).SetConnectTimeout(time.Second*5),
)
if err != nil {
panic(fmt.Errorf("connect: %v", err))
}
dbClient = client
err = dbClient.Database(dbName).Drop(context.Background())
if err != nil {
panic(fmt.Errorf("drop: %v", err))
}
// Run the tests
code := m.Run()
// disconnect mongodb client
if err = dbClient.Disconnect(context.TODO()); err != nil {
panic(err)
}
os.Exit(code)
}