-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
all_keys_test.go
112 lines (84 loc) · 2.13 KB
/
all_keys_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
package goriak
import (
"log"
"math/rand"
"os"
"testing"
"time"
)
// Cleanup Bucket
func TestMain(m *testing.M) {
deleteAllIn("testsuite", "tests")
deleteAllIn("customtype", "maps")
deleteAllIn("testsuitemap", "maps")
deleteAllIn("json", "default")
deleteAllIn("testdelete", "default")
deleteAllIn("hll-test", "hlls")
rand.Seed(time.Now().UnixNano())
os.Exit(m.Run())
}
func deleteAllIn(bucket, bucketType string) {
cb := func(res []string) error {
for _, key := range res {
Bucket(bucket, bucketType).Delete(key).Run(con())
log.Println("Delete: " + key)
}
return nil
}
Bucket(bucket, bucketType).AllKeys(cb).Run(con())
}
func TestAllKeys(t *testing.T) {
c := con()
Bucket("testdelete", "default").SetRaw([]byte{1, 2, 3, 4}).Run(c)
Bucket("testdelete", "default").SetRaw([]byte{1, 2, 3, 4}).Run(c)
Bucket("testdelete", "default").SetRaw([]byte{1, 2, 3, 4}).Run(c)
Bucket("testdelete", "default").SetRaw([]byte{1, 2, 3, 4}).Run(c)
foundCount := 0
_, err := Bucket("testdelete", "default").AllKeys(func(res []string) error {
foundCount = foundCount + len(res)
return nil
}).Run(con())
if err != nil {
t.Error(err)
}
if foundCount == 0 {
t.Error("No keys found")
}
}
func TestAllKeysNoBucket(t *testing.T) {
cmd := &Command{}
_, err := cmd.AllKeys(func(res []string) error {
return nil
}).Run(con())
if err.Error() != "ClientError|Bucket is required" {
t.Error("Unexpected error")
}
}
func TestDelete(t *testing.T) {
res, err := Bucket("testdelete", "default").SetRaw([]byte{1, 2, 3, 4}).Run(con())
if err != nil {
t.Error(err)
}
_, err = Bucket("testdelete", "default").Delete(res.Key).Run(con())
if err != nil {
t.Error(err)
}
var out []byte
res, err = Bucket("testdelete", "default").GetRaw(res.Key, &out).Run(con())
if !res.NotFound {
t.Error("Found after delete")
}
if err == nil {
t.Error("No error")
return
}
if err.Error() != "Not found" {
t.Error("Unexpected error:", err)
}
}
func TestDeleteNoKey(t *testing.T) {
_, err := Bucket("testdelete", "default").Delete("").Run(con())
if err.Error() != "ClientError|Key is required" {
t.Error("Unexpected error")
}
}