clean cleans for tests

master
bel 2020-04-13 02:29:46 +00:00
parent 117ad922e2
commit 14692392e2
1 changed files with 18 additions and 14 deletions

View File

@ -5,30 +5,35 @@ import (
"crypto/rand"
"encoding/base64"
"testing"
"time"
)
func testMongoNew(t *testing.T) *Mongo {
func testMongoNew(t *testing.T) (*Mongo, func()) {
b := make([]byte, 5)
rand.Read(b)
m, err := NewMongo(10, base64.URLEncoding.EncodeToString(b), "mongodb://localhost:27017")
ns := "gotest_" + base64.URLEncoding.EncodeToString(b)
m, err := NewMongo(10, ns, "mongodb://localhost:27017")
if err != nil {
t.Fatal(err)
}
return m
}
func TestMongoNew(t *testing.T) {
if err := testMongoNew(t).Close(); err != nil {
t.Fatal(err)
return m, func() {
ctx, can := context.WithTimeout(context.Background(), time.Second*30)
defer can()
m.client.Database(ns).Drop(ctx)
m.Close()
}
}
func TestMongoNew(t *testing.T) {
_, can := testMongoNew(t)
can()
}
func TestMongoFind(t *testing.T) {
m := testMongoNew(t)
defer m.Close()
m, can := testMongoNew(t)
defer can()
c := m.Account()
defer c.Drop(context.TODO())
if _, err := c.InsertMany(context.TODO(), []interface{}{
map[string]interface{}{"_id": "1", "a": "b"},
map[string]interface{}{"_id": "2", "a": "b", "c": "d"},
@ -65,11 +70,10 @@ func TestMongoFind(t *testing.T) {
}
func TestMongoUpsert(t *testing.T) {
m := testMongoNew(t)
defer m.Close()
m, can := testMongoNew(t)
defer can()
c := m.Account()
defer c.Drop(context.TODO())
if _, err := c.InsertMany(context.TODO(), []interface{}{
map[string]interface{}{"_id": "1", "a": "b"},
map[string]interface{}{"_id": "2", "a": "b", "c": "d"},