forked from cosn/firebase
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfirebase_test.go
More file actions
340 lines (282 loc) · 7.72 KB
/
Copy pathfirebase_test.go
File metadata and controls
340 lines (282 loc) · 7.72 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package firebase_test
import (
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
"time"
"github.com/JustinTulloss/firebase"
"github.com/JustinTulloss/firebase/mock_firebase"
"github.com/golang/mock/gomock"
)
type Name struct {
First string `json:",omitempty"`
Last string `json:",omitempty"`
}
func nameAlloc() interface{} {
return &Name{}
}
/*
Set the two variables below and set them to your own
Firebase URL and credentials (optional) if you're forking the code
and want to test your changes.
*/
// enter your firebase credentials for testing.
var (
testUrl string = os.Getenv("FIREBASE_TEST_URL")
testAuth string = os.Getenv("FIREBASE_TEST_AUTH")
)
func TestValue(t *testing.T) {
client := firebase.NewClient(testUrl+"/tests", testAuth, nil)
var r map[string]interface{}
err := client.Value(&r)
if err != nil {
t.Error(err)
}
if r == nil {
t.Fatalf("No values returned from the server\n")
}
}
func TestChild(t *testing.T) {
client := firebase.NewClient(testUrl+"/tests", testAuth, nil)
r := client.Child("")
if r == nil {
t.Fatalf("No child returned from the server\n")
}
}
func TestPush(t *testing.T) {
client := firebase.NewClient(testUrl+"/tests", testAuth, nil)
name := &Name{First: "FirstName", Last: "LastName"}
r, err := client.Push(name, nil)
if err != nil {
t.Fatalf("%v\n", err)
}
if r == nil {
t.Fatalf("No client returned from the server\n")
}
newName := &Name{}
c2 := firebase.NewClient(r.String(), testAuth, nil)
c2.Value(newName)
if !reflect.DeepEqual(name, newName) {
t.Errorf("Expected %v to equal %v", name, newName)
}
}
func TestSet(t *testing.T) {
c1 := firebase.NewClient(testUrl+"/tests/users", testAuth, nil)
name := &Name{First: "First", Last: "last"}
c2, _ := c1.Push(name, nil)
newName := &Name{First: "NewFirst", Last: "NewLast"}
r, err := c2.Set("", newName, map[string]string{"print": "silent"})
if err != nil {
t.Fatalf("%v\n", err)
}
if r == nil {
t.Fatalf("No client returned from the server\n")
}
}
func TestUpdate(t *testing.T) {
c1 := firebase.NewClient(testUrl+"/tests/users", testAuth, nil)
name := &Name{First: "First", Last: "last"}
c2, _ := c1.Push(name, nil)
newName := &Name{Last: "NewLast"}
err := c2.Update("", newName, nil)
if err != nil {
t.Fatalf("%v\n", err)
}
}
func TestRemovet(t *testing.T) {
c1 := firebase.NewClient(testUrl+"/tests/users", testAuth, nil)
name := &Name{First: "First", Last: "last"}
c2, _ := c1.Push(name, nil)
err := c2.Remove("", nil)
if err != nil {
t.Fatalf("%v\n", err)
}
var val map[string]interface{}
c3 := firebase.NewClient(c2.String(), testAuth, nil)
err = c3.Value(&val)
if err != nil {
t.Error(err)
}
if len(val) != 0 {
t.Errorf("Expected %s to be removed, was %v", c2.String(), val)
}
}
func TestRules(t *testing.T) {
client := firebase.NewClient(testUrl, testAuth, nil)
r, err := client.Rules(nil)
if err != nil {
t.Fatalf("Error retrieving rules: %v\n", err)
}
if r == nil {
t.Fatalf("No child returned from the server\n")
}
}
func TestSetRules(t *testing.T) {
client := firebase.NewClient(testUrl, testAuth, nil)
rules := &firebase.Rules{
"rules": map[string]interface{}{
".read": "auth.username == 'admin'",
".write": "auth.username == 'admin'",
"ordered": map[string]interface{}{
".indexOn": []string{"First"},
"kids": map[string]interface{}{
".indexOn": []string{"Age"},
},
},
},
}
err := client.SetRules(rules, nil)
if err != nil {
t.Fatalf("Error setting rules: %v\n", err)
}
}
func TestOrderBy(t *testing.T) {
client := firebase.NewClient(testUrl, testAuth, nil).Child("ordered")
defer client.Remove("", nil)
names := []*Name{
&Name{First: "BBBB", Last: "YYYY"},
&Name{First: "AAAA", Last: "ZZZZZ"},
}
for _, n := range names {
_, err := client.Push(n, nil)
if err != nil {
t.Fatalf("Couldn't push new name: %s\n", err)
}
}
i := 0
for n := range client.OrderBy(firebase.KeyProp).Iterator(nameAlloc) {
if n.Value.(*Name).First != names[i].First {
t.Fatalf("Key order was not delivered")
}
i++
}
if i == 0 {
t.Fatalf("Did not receive names ordered by key")
}
expectedOrder := []*Name{names[1], names[0]}
i = 0
for n := range client.OrderBy("First").Iterator(nameAlloc) {
if n.Value.(*Name).First != expectedOrder[i].First {
t.Fatalf("Child prop order was not delivered")
}
i++
}
if i == 0 {
t.Fatalf("Did not receive names ordered by first name")
}
kids := map[string]map[string]interface{}{
"a": map[string]interface{}{"Name": "Bob", "Age": 14},
"b": map[string]interface{}{"Name": "Alice", "Age": 13},
}
_, err := client.Set("kids", kids, nil)
if err != nil {
t.Fatalf("Could not set kids: %s\n", err)
}
expectedKidsOrder := []map[string]interface{}{kids["b"], kids["a"]}
i = 0
for n := range client.Child("kids").OrderBy("Age").Iterator(nil) {
if (*n.Value.(*map[string]interface{}))["First"] != expectedKidsOrder[i]["First"] {
t.Fatalf("Child prop order by age was not delivered")
}
i++
}
if i == 0 {
t.Fatalf("Did not receive names ordered by age")
}
}
func TestTimestamp(t *testing.T) {
ts := firebase.Timestamp(time.Now())
marshaled, err := json.Marshal(&ts)
if err != nil {
t.Fatalf("Could not marshal a timestamp to json: %s\n", err)
}
unmarshaledTs := firebase.Timestamp{}
err = json.Unmarshal(marshaled, &unmarshaledTs)
if err != nil {
t.Fatalf("Could not unmarshal a timestamp to json: %s\n", err)
}
// Compare unix timestamps as we lose some fidelity in the nanoseconds
if time.Time(ts).Unix() != time.Time(unmarshaledTs).Unix() {
t.Fatalf("Unmarshaled time %s not equivalent to marshaled time %s",
unmarshaledTs,
ts,
)
}
}
func TestServerTimestamp(t *testing.T) {
b, err := json.Marshal(firebase.ServerTimestamp)
if err != nil {
t.Fatalf("Could not marshal server timestamp: %s\n", err)
}
if string(b) != `{".sv":"timestamp"}` {
t.Fatalf("Unexpected timestamp json value: %s\n", b)
}
}
func TestIterator(t *testing.T) {
client := firebase.NewClient(testUrl+"/test-iterator", testAuth, nil)
defer client.Remove("", nil)
names := []Name{
{First: "FirstName", Last: "LastName"},
{First: "Second", Last: "Seconder"},
}
for _, name := range names {
_, err := client.Push(name, nil)
if err != nil {
t.Fatalf("%v\n", err)
}
}
var i = 0
for nameEntry := range client.Iterator(nameAlloc) {
name := nameEntry.Value.(*Name)
if !reflect.DeepEqual(&names[i], name) {
t.Errorf("Expected %v to equal %v", &names[i], name)
}
i++
}
if i != len(names) {
t.Fatalf("Did not receive all names, received %d\n", i)
}
}
func TestShallow(t *testing.T) {
client := firebase.NewClient(testUrl+"/test-shallow", testAuth, nil)
defer client.Remove("", nil)
client.Set("one", 1, nil)
client.Set("two", 2, nil)
keys, err := client.Shallow()
if err != nil {
t.Errorf("Error when calling shallow: %s\n", err)
}
if !reflect.DeepEqual(keys, []string{"one", "two"}) {
t.Errorf("keys (%v) were not correct\n", keys)
}
}
func TestKey(t *testing.T) {
client := firebase.NewClient(testUrl+"/test", testAuth, nil)
if client.Key() != "test" {
t.Errorf("Key should have been test, was %s\n", client.Key())
}
client = firebase.NewClient(testUrl, testAuth, nil)
if client.Key() != "" {
t.Errorf("Key should have been empty, was %s\n", client.Key())
}
client = client.Child("/a/b/c/d/e/f/g")
if client.Key() != "g" {
t.Errorf("Key should have been 'g', was %s\n", client.Key())
}
}
func TestMockable(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockFire := mock_firebase.NewMockClient(mockCtrl)
mockFire.EXPECT().Child("test")
mockFire.Child("test")
}
func TestMain(m *testing.M) {
if testUrl == "" || testAuth == "" {
fmt.Printf("You need to set FIREBASE_TEST_URL and FIREBASE_TEST_AUTH\n")
os.Exit(1)
}
os.Exit(m.Run())
}