-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo_test.go
176 lines (133 loc) · 4.14 KB
/
mongo_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
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
package clean_like_gopher
import (
"fmt"
"gopkg.in/mgo.v2"
"testing"
)
type Person struct {
Name string
Phone string
}
type Animal struct {
Kind string
Character string
}
var mongoStartOptions = map[string]string{"host": "localhost", "dbName": "test", "port": "27017"}
func makeDbDirty(session *mgo.Session) {
c := session.DB("test").C("people")
err := c.Insert(&Person{"Ale", "+55 53 8116 9639"},
&Person{"Cla", "+55 53 8402 8510"})
c = session.DB("test").C("animals")
err = c.Insert(&Animal{"gopher", "cute"},
&Animal{"snake", "not cute"})
if err != nil {
fmt.Println(err)
}
}
func CountRows(dbName, collectionName string, session *mgo.Session) int {
collection := session.DB(dbName).C(collectionName)
count, _ := collection.Count()
return count
}
func TestNewCleaningGopherMongoWithIncorrectConnection(t *testing.T) {
dbIncorrectOptions := map[string]string{"host": "localhost", "dbName": "test", "port": "27010"}
_, err := NewCleaningGopher("mongo", dbIncorrectOptions)
if err == nil {
t.Errorf("Expected error since the host does not exists")
}
}
func TestMongoCleanAll(t *testing.T) {
m, _ := NewCleaningGopher("mongo", mongoStartOptions)
session, _ := mgo.Dial("localhost:27017")
defer session.Close()
makeDbDirty(session)
m.Clean(nil)
m.Close()
countOfPeople := CountRows("test", "people", session)
countOfAnimals := CountRows("test", "animals", session)
if countOfAnimals != 0 || countOfPeople != 0 {
t.Errorf("Expected db to be empty")
}
}
func TestMongoCleanOnly(t *testing.T) {
m, _ := NewCleaningGopher("mongo", mongoStartOptions)
session, _ := mgo.Dial("localhost:27017")
defer session.Close()
makeDbDirty(session)
options := make(map[string][]string)
options["only"] = []string{"animals"}
m.Clean(options)
m.Close()
countOfPeople := CountRows("test", "people", session)
countOfAnimals := CountRows("test", "animals", session)
if countOfPeople == 0 {
t.Errorf("Expected people collection to not be deleted")
}
if countOfAnimals != 0 {
t.Errorf("Expected animals to be deleted")
}
}
func TestMongoCleanExcept(t *testing.T) {
m, _ := NewCleaningGopher("mongo", mongoStartOptions)
session, _ := mgo.Dial("localhost:27017")
defer session.Close()
makeDbDirty(session)
options := make(map[string][]string)
options["except"] = []string{"animals"}
m.Clean(options)
m.Close()
countOfAnimals := CountRows("test", "animals", session)
countOfPeople := CountRows("test", "people", session)
if countOfAnimals == 0 {
t.Errorf("Expected animals to not be deleted")
}
if countOfPeople != 0 {
t.Errorf("Expected people to be deleted")
}
}
func TestNewCleaningGopherMongoInvalidOptionsAreIgnored(t *testing.T) {
m, _ := NewCleaningGopher("mongo", mongoStartOptions)
session, _ := mgo.Dial("localhost:27017")
defer session.Close()
makeDbDirty(session)
options := make(map[string][]string)
options["invalid"] = []string{"animals"}
m.Clean(options)
m.Close()
countOfAnimals := CountRows("test", "animals", session)
countOfPeople := CountRows("test", "people", session)
if countOfAnimals != 0 {
t.Errorf("Expected animals to be deleted")
}
if countOfPeople != 0 {
t.Errorf("Expected people to be deleted")
}
}
func TestNewCleaningGopherMongoWithMissingDbName(t *testing.T) {
dbIncorrectOptions := map[string]string{"host": "localhost", "port": "27017"}
_, err := NewCleaningGopher("mongo", dbIncorrectOptions)
if err == nil {
t.Errorf("Expected error since the db is not specified")
}
}
func TestMongoCleanTwice(t *testing.T) {
m, _ := NewCleaningGopher("mongo", mongoStartOptions)
session, _ := mgo.Dial("localhost:27017")
defer session.Close()
makeDbDirty(session)
m.Clean(nil)
countOfPeople := CountRows("test", "people", session)
countOfAnimals := CountRows("test", "animals", session)
if countOfAnimals != 0 || countOfPeople != 0 {
t.Errorf("Expected db to be empty")
}
makeDbDirty(session)
m.Clean(nil)
countOfPeople = CountRows("test", "people", session)
countOfAnimals = CountRows("test", "animals", session)
if countOfAnimals != 0 || countOfPeople != 0 {
t.Errorf("Expected db to be empty")
}
m.Close()
}
func TestNewCleaningGopherMongoWithIncorrectStrategy(t *testing.T) {}