clean cleans for tests
parent
117ad922e2
commit
14692392e2
|
|
@ -5,30 +5,35 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testMongoNew(t *testing.T) *Mongo {
|
func testMongoNew(t *testing.T) (*Mongo, func()) {
|
||||||
b := make([]byte, 5)
|
b := make([]byte, 5)
|
||||||
rand.Read(b)
|
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 {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
return m
|
return m, func() {
|
||||||
}
|
ctx, can := context.WithTimeout(context.Background(), time.Second*30)
|
||||||
|
defer can()
|
||||||
func TestMongoNew(t *testing.T) {
|
m.client.Database(ns).Drop(ctx)
|
||||||
if err := testMongoNew(t).Close(); err != nil {
|
m.Close()
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMongoNew(t *testing.T) {
|
||||||
|
_, can := testMongoNew(t)
|
||||||
|
can()
|
||||||
|
}
|
||||||
|
|
||||||
func TestMongoFind(t *testing.T) {
|
func TestMongoFind(t *testing.T) {
|
||||||
m := testMongoNew(t)
|
m, can := testMongoNew(t)
|
||||||
defer m.Close()
|
defer can()
|
||||||
|
|
||||||
c := m.Account()
|
c := m.Account()
|
||||||
defer c.Drop(context.TODO())
|
|
||||||
if _, err := c.InsertMany(context.TODO(), []interface{}{
|
if _, err := c.InsertMany(context.TODO(), []interface{}{
|
||||||
map[string]interface{}{"_id": "1", "a": "b"},
|
map[string]interface{}{"_id": "1", "a": "b"},
|
||||||
map[string]interface{}{"_id": "2", "a": "b", "c": "d"},
|
map[string]interface{}{"_id": "2", "a": "b", "c": "d"},
|
||||||
|
|
@ -65,11 +70,10 @@ func TestMongoFind(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMongoUpsert(t *testing.T) {
|
func TestMongoUpsert(t *testing.T) {
|
||||||
m := testMongoNew(t)
|
m, can := testMongoNew(t)
|
||||||
defer m.Close()
|
defer can()
|
||||||
|
|
||||||
c := m.Account()
|
c := m.Account()
|
||||||
defer c.Drop(context.TODO())
|
|
||||||
if _, err := c.InsertMany(context.TODO(), []interface{}{
|
if _, err := c.InsertMany(context.TODO(), []interface{}{
|
||||||
map[string]interface{}{"_id": "1", "a": "b"},
|
map[string]interface{}{"_id": "1", "a": "b"},
|
||||||
map[string]interface{}{"_id": "2", "a": "b", "c": "d"},
|
map[string]interface{}{"_id": "2", "a": "b", "c": "d"},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue