-
Notifications
You must be signed in to change notification settings - Fork 63
/
seed_test.go
90 lines (77 loc) · 1.88 KB
/
seed_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
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2021 Kuei-chun Chen. All rights reserved.
package keyhole
import (
"context"
"errors"
"os"
"testing"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
UnitTestURL = "mongodb://localhost/?replicaSet=replset"
)
func GetMongoClient() *mongo.Client {
var err error
var client *mongo.Client
if os.Getenv("DATABASE_URL") != "" {
UnitTestURL = os.Getenv("DATABASE_URL")
}
if client, err = mongo.Connect(context.Background(), options.Client().ApplyURI(UnitTestURL)); err != nil {
panic(err)
}
return client
}
func TestSeed(t *testing.T) {
var err error
var client = GetMongoClient()
defer client.Disconnect(context.Background())
dbName := "keyhole"
total := 100
f := NewSeed()
f.SetDatabase(dbName)
f.SetTotal(total)
f.SetIsDrop(true)
f.SetShowProgress(false)
// calling seed()
f.SeedData(client)
db := client.Database(dbName)
coll := db.Collection("vehicles")
var count int64
if count, err = coll.CountDocuments(context.Background(), bson.M{}); err != nil {
t.Fatal(err)
}
if int64(total) != count {
t.Fatal(errors.New("vehicles count doesn't match"))
}
}
func TestSeedFromTemplate(t *testing.T) {
var err error
var client *mongo.Client
ctx := context.Background()
client = GetMongoClient()
defer client.Disconnect(context.Background())
file := "../examples/template.json"
collection := "template"
total := 100
dbName := "keyhole"
f := NewSeed()
f.SetCollection(collection)
f.SetDatabase(dbName)
f.SetFile(file)
f.SetIsDrop(true)
f.SetShowProgress(false)
f.SetTotal(total)
// calling seedFromTemplate
f.SeedData(client)
db := client.Database(dbName)
coll := db.Collection("vehicles")
var count int64
if count, err = coll.CountDocuments(ctx, bson.M{}); err != nil {
t.Fatal(err)
}
if int64(total) != count {
t.Fatal(errors.New("vehicles count doesn't match"))
}
}