-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection_test.go
More file actions
193 lines (129 loc) · 3.8 KB
/
collection_test.go
File metadata and controls
193 lines (129 loc) · 3.8 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package mgoStreamingCollection_test
import (
"fmt"
"github.com/ballad89/mgoStreamingCollection"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"os"
"testing"
)
func TestConnection(t *testing.T) {
database, err := setup()
if err != nil {
t.Fatalf("error connecting to database with error: %v", err)
}
defer database.Session.Close()
collectionName := "testConnection"
collectionSize := 1024
_, err = mgoStreamingCollection.CreateOrConvertCollection(database, collectionName, collectionSize)
if err != nil {
t.Errorf("collection creation failed with error: %v", err)
}
}
func TestCollectionExistsAndStats(t *testing.T) {
database, err := setup()
if err != nil {
t.Fatalf("error connecting to database with error: %v", err)
}
defer database.Session.Close()
testColl := "testStats"
colSize := 8192
coll, err := mgoStreamingCollection.CreateOrConvertCollection(database, testColl, colSize)
if err != nil {
t.Errorf("error creating collection with error: %v", err)
}
exists, err := mgoStreamingCollection.CollectionExists(database, testColl)
if err != nil {
t.Errorf("error checking collection existence with error: %v", err)
}
if !exists {
t.Errorf("collection %s does not exist", testColl)
}
stats, err := mgoStreamingCollection.Stats(coll)
if err != nil {
t.Errorf("error getting collection stats with error: %v", err)
}
if !stats.Capped {
t.Errorf("collection %s not capped", testColl)
}
if stats.MaxBytes != colSize {
t.Log(stats.MaxBytes)
t.Errorf("collection %s size not set %d", testColl, colSize)
}
}
func TestConvertCollection(t *testing.T) {
database, err := setup()
if err != nil {
t.Fatalf("error connecting to database with error: %v", err)
}
defer database.Session.Close()
testColl := "testConvert"
colSize := 8192
coll := database.C(testColl)
coll.DropCollection()
coll.Insert(bson.M{"name": "husayn"})
stats, err := mgoStreamingCollection.Stats(coll)
if err != nil {
t.Errorf("error getting collection stats with error: %v", err)
}
if stats.Capped {
t.Errorf("collection %s should not be capped", testColl)
}
err = mgoStreamingCollection.ConvertToCapped(coll, colSize)
if err != nil {
t.Errorf("error converting collection with error: %v", err)
}
newStats, err := mgoStreamingCollection.Stats(coll)
if err != nil {
t.Errorf("error getting collection stats with error: %v", err)
}
if !newStats.Capped {
t.Errorf("collection %s should be capped", testColl)
}
if newStats.MaxBytes != colSize {
t.Errorf("collection %s size not set %d", testColl, colSize)
}
}
func TestTailQuery(t *testing.T) {
database, err := setup()
if err != nil {
t.Fatalf("error connecting to database with error: %v", err)
}
defer database.Session.Close()
collectionName := "testTailQuery"
collectionSize := 1024
if exists, _ := mgoStreamingCollection.CollectionExists(database, collectionName); exists {
database.C(collectionName).DropCollection()
}
cappedCollection, err := mgoStreamingCollection.CreateOrConvertCollection(database, collectionName, collectionSize)
if err != nil {
t.Errorf("collection creation failed with error: %v", err)
}
ch := make(chan interface{})
go mgoStreamingCollection.TailQuery(cappedCollection.Find(nil), ch)
for i := 0; i < 3; i++ {
cappedCollection.Insert(bson.M{"name": "husayn", "count": i})
for msg := range ch {
if msg != nil {
t.Log(msg)
break
}
t.Errorf("inserted document not read with cound %d", i)
}
}
close(ch)
}
func setup() (*mgo.Database, error) {
testServer := os.Getenv("DatabaseServer")
if testServer == "" {
testServer = "127.0.0.1"
}
connectUrl := fmt.Sprintf("mongodb://%s/test", testServer)
session, err := mgo.Dial(connectUrl)
if err != nil {
return nil, err
}
session.SetSafe(&mgo.Safe{})
database := session.DB("test")
return database, nil
}