-
Notifications
You must be signed in to change notification settings - Fork 3
/
session_test.go
81 lines (58 loc) · 1.79 KB
/
session_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
package mongofixtures
import (
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
"strconv"
"testing"
)
type document struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Title string `bson:"title"`
}
func checkCount(t *testing.T, collection *mgo.Collection, search string, count int, message string) {
c, err := collection.Find(bson.M{"title": search}).Count()
if err != nil {
t.Fatal(err)
}
if c != count {
t.Fatal(message + " : " + strconv.Itoa(c))
}
}
func TestLoader(t *testing.T) {
mongoSession, err := mgo.Dial("localhost")
collection := mongoSession.DB("sample").C("collection1")
session, err := Begin("localhost", "sample")
defer session.End()
if err != nil {
t.Fatal(err)
}
type Test struct {
Bla []int
}
err = session.Push("collection2", Test{Bla: []int{1, 2, 3, 4}})
err = session.Push("collection1", document{Id: bson.NewObjectId(), Title: "This is a demo"})
if err != nil {
t.Fatal(err)
}
checkCount(t, collection, "This is a demo", 1, "Wrong count after inserting a document")
err = session.Push("collection1", document{Id: bson.NewObjectId(), Title: "This is a demo 2"})
if err != nil {
t.Fatal(err)
}
checkCount(t, collection, "This is a demo 2", 1, "Wrong count after inserting a document")
session.ImportYamlFile("test.yml")
count, _ := mongoSession.DB("sample").C("employees").Find(nil).Count()
if count != 3 {
t.Fatal("Wrong count after inserting employees via yml")
}
// @todo check that Moss is friend with Roy
// @todo check that Roy is friend with Moss
// @todo check that Moss & Roy belongs to the basement
err = session.Clean("collection1")
err = session.Clean("employees")
err = session.Clean("locations")
if err != nil {
t.Fatal(err)
}
checkCount(t, collection, "This is a demo", 0, "Wrong count after freeing the collection")
}