-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.go
140 lines (115 loc) · 2.56 KB
/
client_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
package mongo
import (
"gopkg.in/mgo.v2/bson"
"testing"
)
type HelloWorld struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"name,omitempty" gobly:"slug"`
Value string `bson:"value,omitempty"`
}
type Hello struct {
HelloWorld `bson:",inline"`
}
const collectionName = "testCollection"
func connect(t *testing.T) *Client {
m := Client{}
err := m.Connect("localhost", "local", collectionName)
if err != nil {
t.Errorf("Could not connect to database: %s", err.Error())
}
return &m
}
func TestAnonymousConnection(t *testing.T) {
connect(t)
}
func TestCreateCollection(t *testing.T) {
m := connect(t)
if m == nil {
return
}
err := m.CreateCollection()
if err != nil {
t.Errorf("Could not cteate test collection: %s", err.Error())
}
}
func TestInsertCollection(t *testing.T) {
m := connect(t)
if m == nil {
return
}
v := HelloWorld{Name: "world", Value: "Hello"}
err := m.Insert(&v)
if err != nil {
t.Errorf("Could not insert test data to collection: %s", err.Error())
}
}
func TestInsertInlineCollection(t *testing.T) {
m := connect(t)
if m == nil {
return
}
v := Hello{}
v.Name = "world"
v.Value = "Hello"
err := m.Insert(&v)
if err != nil {
t.Errorf("Could not insert test data to collection: %s", err.Error())
}
}
func TestFindDocuments(t *testing.T) {
m := connect(t)
if m == nil {
return
}
q := HelloWorld{Name: "world"}
v := []HelloWorld{}
err := m.FindByValue(q, &v)
if err != nil {
t.Errorf("Could not lookup collection: %s", err.Error())
return
}
if v[0].Name != "world" || v[0].Value != "Hello" {
t.Errorf("Got back invalid data: name=%s, value=%s", v[0].Name, v[0].Value)
}
}
func TestReadDocument(t *testing.T) {
m := connect(t)
if m == nil {
return
}
q := HelloWorld{Name: "world"}
err := m.ReadByValue(&q)
if err != nil {
t.Errorf("Could not read from collection %s", err.Error())
return
}
if q.Name != "world" || q.Value != "Hello" {
t.Errorf("Got back invalid data: name=%s, value=%s", q.Name, q.Value)
}
}
func TestReturnDocumentFromSlug(t *testing.T) {
m := connect(t)
if m == nil {
return
}
q := HelloWorld{}
err := m.ReadBySlug("world", &q)
if err != nil {
t.Errorf("Could not read from collection %s", err.Error())
return
}
if q.Name != "world" || q.Value != "Hello" {
t.Errorf("Got back invalid data: name=%s, value=%s", q.Name, q.Value)
}
}
func TestDeleteCollection(t *testing.T) {
m := connect(t)
if m == nil {
return
}
err := m.DropCollection()
if err != nil {
t.Errorf("Could not delete test collection: %s", err.Error())
}
}