-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_test.go
78 lines (68 loc) · 2.45 KB
/
common_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
package common
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// func TestMain(m *testing.M) {
// setup()
// code := m.Run()
// shutdown()
// os.Exit(code)
// }
func Test_setClientOptions(t *testing.T) {
connectionURI := "mongodb://localhost"
appName := "common-test"
client := setClientOptions(connectionURI, appName, true)
assert.Equal(t, appName, *client.AppName)
assert.Equal(t, ConnectTimeout, *client.ConnectTimeout)
assert.Equal(t, MaxConnIdleTime, *client.MaxConnIdleTime)
assert.Equal(t, ServerSelectionTimeout, *client.ServerSelectionTimeout)
assert.Equal(t, connectionURI, client.GetURI())
}
func Test_setClientOptionsWithCredentials(t *testing.T) {
connectionURI := "mongodb://localhost"
appName := "common-test"
authDB := "admin"
userDB := "user"
passDB := "password"
credentials := options.Credential{AuthSource: authDB, Username: userDB, Password: passDB}
client := setClientOptionsWithCredentials(connectionURI, appName, credentials, true)
assert.Equal(t, appName, *client.AppName)
assert.Equal(t, ConnectTimeout, *client.ConnectTimeout)
assert.Equal(t, MaxConnIdleTime, *client.MaxConnIdleTime)
assert.Equal(t, ServerSelectionTimeout, *client.ServerSelectionTimeout)
assert.Equal(t, connectionURI, client.GetURI())
assert.Equal(t, authDB, client.Auth.AuthSource)
assert.Equal(t, userDB, client.Auth.Username)
assert.Equal(t, passDB, client.Auth.Password)
}
func Test_returnClient(t *testing.T) {
connectionURI := "localhost"
appName := "common-test"
client, err := ReturnClient(connectionURI, appName, true)
assert.Nil(t, err, "should be nil")
assert.NotNil(t, client, "should not be nil")
err = client.Ping(context.TODO(), nil)
assert.Nil(t, err, "should be nil")
}
func Test_Total(t *testing.T) {
client := getClient("localhost", "common-test")
total, err := Total(client, "test", "test", bson.M{})
assert.Nil(t, err, "should be nil")
assert.Equal(t, int64(0), total)
}
func Test_InsertOne(t *testing.T) {
client := getClient("localhost", "common-test")
insertResult, err := InsertOne(client, "test", "test", bson.E{Key: "timestamp", Value: time.Now()})
assert.Nil(t, err, "should be nil")
assert.NotNil(t, insertResult, "should not be nil")
}
func getClient(connectionURI string, appName string) *mongo.Client {
client, _ := ReturnClient(connectionURI, appName, true)
return client
}