-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcblcgo_test.go
551 lines (448 loc) · 14.7 KB
/
cblcgo_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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
// +build !replication
package cblcgo
import "testing"
import "fmt"
import "context"
import "time"
func TestConnection(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, err := Open("my_db", &config); err == nil {
if !DatabaseExists("my_db", "./db") {
t.Error("Database doesn't exist.")
}
if !CopyDatabase("./db/my_db.cblite2", "my_db2", &config) {
t.Error("Can't copy database.")
}
if DatabaseExists("my_db2", "./db") && !DeleteDatabase("my_db2", "./db") {
t.Error("Can't delete database.")
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(err)
}
}
func TestSaveAndDeleteDocuments(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db3", &config); db_err == nil {
doc_to_delete := NewDocument()
doc_to_delete.Props["name"] = "Luke"
doc_to_delete.Props["lastname"] = "Skywalker"
doc_to_delete.Props["age"] = 30
doc_to_delete.Props["email"] = "[email protected]"
doc_to_delete.Props["action"] = "delete"
doc_to_purge := NewDocument()
doc_to_purge.Props["name"] = "Anakin"
doc_to_purge.Props["lastname"] = "Skywalker"
doc_to_purge.Props["age"] = 30
doc_to_purge.Props["email"] = "[email protected]"
doc_to_delete.Props["action"] = "purge"
doc_to_purge_by_id := NewDocumentWithId("myId")
doc_to_purge_by_id.Props["name"] = "Obi-Wan"
doc_to_purge_by_id.Props["lastname"] = "Kenobi"
doc_to_purge_by_id.Props["age"] = 30
doc_to_purge_by_id.Props["email"] = "[email protected]"
doc_to_delete.Props["action"] = "purge_id"
doc_to_expire := NewDocument()
doc_to_expire.Props["name"] = "Master"
doc_to_expire.Props["lastname"] = "Yoda"
doc_to_expire.Props["age"] = 900
doc_to_expire.Props["email"] = "[email protected]"
doc_to_delete.Props["action"] = "expire"
var docs []*Document = []*Document{doc_to_delete, doc_to_purge, doc_to_purge_by_id, doc_to_expire}
for i:=0; i < len(docs); i++ {
if _doc, err := db.Save(docs[i], LastWriteWins); err == nil {
switch _doc.Props["action"] {
case "delete":
if e := db.DeleteDocument(_doc, FailOnConflict); e != nil {
t.Error(e)
}
// Release the underlying CBLDocument ptr
if !_doc.Release() || !docs[i].Release() {
t.Error("Error releasing a document.")
}
break;
case "purge":
if e := db.Purge(_doc); e != nil {
t.Error(e)
}
// Release the underlying CBLDocument ptr
if !_doc.Release() || !docs[i].Release() {
t.Error("Error releasing a document.")
}
break;
case "purge_id":
if e := db.PurgeById("myId"); e != nil {
t.Error(e)
}
break;
case "expire":
if db.SetDocumentExpiration(_doc.Id(), 1571517982) {
if db.PurgeExpiredDocuments() <= 0 {
t.Error("Couldn't purge expired documents.")
}
} else {
t.Error("Couldn't set document expiration.")
}
break;
}
} else {
t.Error(err)
}
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestSaveAndRetrieveDocuments(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db4", &config); db_err == nil {
doc := NewDocumentWithId("test")
doc.SetPropertiesAsJSON("{\"name\": \"Marcel\", \"lastname\": \"Rivera\", \"age\": 30, \"email\": \"[email protected]\"}")
// Save the doc, returns the same doc so only release one reference at the end.
if _doc, err := db.Save(doc, LastWriteWins); err == nil {
// Retrieve the saved doc
if savedDoc, e := db.GetMutableDocument("test"); e == nil {
for k, v := range savedDoc.Props {
if _doc.Props[k] != v {
t.Error("Saved document and retrieved document are different.")
}
}
savedDoc.Release()
} else {
t.Error(e)
}
} else {
t.Error(err)
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestProperties(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db5", &config); db_err == nil {
original := NewDocumentWithId("test2")
original.Props["name"] = "Kylo"
original.Props["lastname"] = "Ren"
original.Props["age"] = 30
original.Props["email"] = "[email protected]"
newProps := make(map[string]interface{})
newProps["name"] = "Rey"
newProps["lastname"] = ""
newProps["age"] = 25
newProps["email"] = "[email protected]"
cpy := DocumentMutableCopy(original)
if cpy.SetProperties(newProps) {
for k, v := range cpy.Props {
if original.Props[k] == v {
t.Error("Original document and copied document have a similar property.")
}
}
original.Release()
cpy.Release()
} else {
t.Error("Couldn't set the properties with new map.")
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestDocumentListener(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db6", &config); db_err == nil {
doc := NewDocumentWithId("documentToListenTo")
doc.SetPropertiesAsJSON("{\"name\": \"Marcel\", \"lastname\": \"Rivera\", \"age\": 30, \"email\": \"[email protected]\"}")
// Save the doc, returns the same doc so only release one reference at the end.
if _, err := db.Save(doc, LastWriteWins); err == nil {
ctx := context.WithValue(context.Background(), "package", "cblcgo")
ctx = context.WithValue(ctx, uuid, "myUniqueIDGlobaly")
// Create and set the listener
var documentChangeCallback = func (ctx context.Context, db *Database, docId string) {
fmt.Println("I'm in callback")
fmt.Println(ctx.Value("package").(string))
}
if token, ee := db.AddDocumentChangeListener(documentChangeCallback, "documentToListenTo", ctx, []string{"package", uuid}); ee == nil {
// Change and save the document
time.Sleep(2 * time.Second)
doc.Props["name"] = "test"
if _, e := db.Save(doc, LastWriteWins); e != nil {
t.Error(e)
}
time.Sleep(2 * time.Second)
db.RemoveListener(token)
}
} else {
t.Error(err)
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestQuery(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db7", &config); db_err == nil {
// Create an index
var spec IndexSpec
spec.IgnoreAccents = true
spec.KeyExpressionsJSON = "[\"name\"]"
spec.Language = "en"
spec.Type = ValueIndex
if db.CreateIndex("myFirstIndex", spec) {
indexes := db.IndexNames()
//fmt.Println(indexes)
if indexes[0] != "myFirstIndex" {
t.Error("Created index not in db.")
}
doc := NewDocumentWithId("docToQuery")
doc.SetPropertiesAsJSON("{\"name\": \"Marcel\", \"lastname\": \"Rivera\", \"age\": 30, \"email\": \"[email protected]\"}")
// Save the doc, returns the same doc so only release one reference at the end.
if _, err := db.Save(doc, LastWriteWins); err != nil {
t.Error(err)
}
doc.Release()
if query, qerr := db.NewQuery(N1QLLanguage, "SELECT COUNT(1) where name = $name"); qerr == nil {
// Add parameter
queryParam := make(map[string]interface{})
queryParam["name"] = "Marcel"
if perr := query.SetParameters(queryParam); perr == nil {
fmt.Println(query.Explain())
if resultSet, eerr := query.Execute(); eerr == nil {
query.Release()
for resultSet.Next() {
if val, ok := resultSet.ValueAtIndex(0).(int64); !ok || val != 1 {
t.Error("Queried name doesn't equal expected result.")
}
}
} else {
t.Error(eerr)
}
} else {
t.Error(perr)
}
} else {
t.Error(qerr)
}
if !db.DeleteIndex("myFirstIndex") {
t.Error("Couldn't delete index.")
}
} else {
t.Error("Couldn't Create Index.")
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestBlob(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db8", &config); db_err == nil {
doc := NewDocumentWithId("docBlob")
doc.SetPropertiesAsJSON("{\"name\": \"Marcel\", \"lastname\": \"Rivera\", \"age\": 30, \"email\": \"[email protected]\"}")
if blob, berr := NewBlobWithData("", []byte("This is a test")); berr == nil {
fmt.Println("New Blob")
fmt.Println(blob)
doc.Props["blob"] = blob
// Save the doc, returns the same doc so only release one reference at the end.
if _, err := db.Save(doc, LastWriteWins); err == nil {
blob.Release()
doc.Release()
// Read the document back
if docBlob, dblobErr := db.GetMutableDocument("docBlob"); dblobErr == nil {
if iblob, ok := docBlob.Props["blob"]; ok {
var b *Blob
if b, ok = iblob.(*Blob); ok {
// Make a stream
brs := b.NewReadStream()
// make destination buffer
dst := make([]byte, b.Length())
var totalBytes int = 0
for uint64(totalBytes) < b.Length() {
if bytesRead, brerr := blob.Read(brs, dst); brerr == nil {
totalBytes += bytesRead
} else {
t.Error(brerr)
}
}
if "This is a test" != string(dst) {
t.Error("Blob content doesn't match.")
}
b.CloseReader(brs)
} else {
t.Error("Couln't convert var iblob to type *Blob")
}
} else {
t.Error("No entry for key \"blob\"")
}
// If you get a doc with a blob and try to release the doc after releasing the blob it will cause and error
// or it could be because of the if's
docBlob.Release()
} else {
t.Error(dblobErr)
}
} else {
t.Error(err)
}
} else {
t.Error(berr)
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestListeners(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db9", &config); db_err == nil {
// Save the doc, returns the same doc so only release one reference at the end.
ctx := context.WithValue(context.Background(), "package", "cblcgo")
ctx = context.WithValue(ctx, uuid, "myUniqueIDGlobaly")
// set a whole bunch of listeners
// Database listener
db_callback := func(ctx context.Context, db *Database, docIDs []string) {
fmt.Println(len(docIDs))
fmt.Println("db callback")
}
// This will be a live query that will be called when the result set changes.
query_callback := func(ctx context.Context, query *Query) {
fmt.Println("query callback")
}
if db_token, dberr := db.AddDatabaseChangeListener(db_callback, ctx, []string{"package", uuid}); dberr == nil {
if query, qerr := db.NewQuery(N1QLLanguage, "SELECT COUNT(1) where name = $name"); qerr == nil {
if query_token, qerr2 := query.AddChangeListener(query_callback, ctx, []string{"package", uuid}); qerr2 == nil {
doc := NewDocumentWithId("docListener")
doc.SetPropertiesAsJSON("{\"name\": \"Marcel\", \"lastname\": \"Rivera\", \"age\": 30, \"email\": \"[email protected]\"}")
if _, err := db.Save(doc, LastWriteWins); err == nil {
time.Sleep(3 * time.Second)
doc.Release()
db.RemoveListener(db_token)
db.RemoveListener(query_token)
}
} else {
t.Error(qerr2)
}
} else {
t.Error(qerr)
}
} else {
t.Error(dberr)
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}
func TestNotificationCallback(t *testing.T) {
var config DatabaseConfiguration
var encryption_key EncryptionKey
encryption_key.Algorithm = EncryptionNone
encryption_key.Bytes = make([]byte, 0)
config.Directory = "./db"
config.EncryptionKey = encryption_key
config.Flags = Database_Create
if db, db_err := Open("my_db10", &config); db_err == nil {
doc := NewDocumentWithId("notifCallback")
doc.SetPropertiesAsJSON("{\"name\": \"Marcel\", \"lastname\": \"Rivera\", \"age\": 30, \"email\": \"[email protected]\"}")
// Save the doc, returns the same doc so only release one reference at the end.
if _, err := db.Save(doc, LastWriteWins); err == nil {
ctx := context.WithValue(context.Background(), "package", "cblcgo")
ctx = context.WithValue(ctx, uuid, "myUniqueIDGlobaly")
// Create and set the listener
var documentChangeCallback = func (ctx context.Context, db *Database, docId string) {
fmt.Println("I'm in callback")
fmt.Println(ctx.Value("package").(string))
}
if token, ee := db.AddDocumentChangeListener(documentChangeCallback, "notifCallback", ctx, []string{"package", uuid}); ee == nil {
// Notification change listener
notif_callback := func (ctx context.Context, db *Database) {
fmt.Println("notification callback")
db.SendNotifications()
}
ctx2 := context.WithValue(context.Background(), "package", "cblcgo")
db.DatabaseBufferNotifications(notif_callback, ctx2, []string{"package"})
// Change and save the document
time.Sleep(2 * time.Second)
doc.Props["name"] = "test"
if _, e := db.Save(doc, LastWriteWins); e != nil {
t.Error(e)
}
time.Sleep(4 * time.Second)
db.RemoveListener(token)
}
} else {
t.Error(err)
}
if !db.Close() {
t.Error("Couldn't close the database.")
}
} else {
t.Error(db_err)
}
}