-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery_test.go
More file actions
121 lines (105 loc) · 2.63 KB
/
query_test.go
File metadata and controls
121 lines (105 loc) · 2.63 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
package riaken_struct
import (
"os/exec"
"testing"
)
type Animal struct {
Id string
Name string `json:"name" riak:"index"`
}
func TestQuery(t *testing.T) {
client, err := dial()
if err != nil {
t.Fatal(err.Error())
}
defer client.Close()
session := client.Session()
defer session.Release()
session.Query()
}
func TestSecondaryIndexes(t *testing.T) {
client, err := dial()
if err != nil {
t.Fatal(err.Error())
}
defer client.Close()
session := client.Session()
defer session.Release()
// Setup
animal := Animal{
Name: "chicken",
}
bucket := session.GetBucket("animals")
object := bucket.Object("2i-a1")
if _, err := object.Store(&animal); err != nil {
t.Error(err.Error())
}
// Query
var check []Animal
query := session.Query()
if _, err := query.Key("Id").Out(&check).SecondaryIndexes([]byte("animals"), []byte("name_bin"), []byte("chicken"), nil, nil, 0, nil); err != nil {
t.Error(err.Error())
}
if len(check) == 0 {
t.Error("expected results")
} else {
if check[0].Name != "chicken" {
t.Errorf("expected: chicken, got: %s", check[0].Name)
}
if check[0].Id != "2i-a1" {
t.Errorf("expected: 2i-a1, got: %s", check[0].Id)
}
}
// Cleanup
if _, err := object.Delete(); err != nil {
t.Error(err.Error())
}
}
func TestSearch(t *testing.T) {
client, err := dial()
if err != nil {
t.Fatal(err.Error())
}
defer client.Close()
session := client.Session()
defer session.Release()
// Set bucket properties.
// Unfortunately these still aren't exposed via PBC, so do it manually with curl.
if _, err := exec.Command("curl", "-XPUT", "-H", "content-type:application/json", "http://127.0.0.1:8093/riak/animals", "-d", `{"props":{"precommit":[{"mod":"riak_search_kv_hook","fun":"precommit"}]}}`).Output(); err != nil {
t.Error(err.Error())
}
bucket := session.GetBucket("animals")
animal := &Animal{
Name: "pig",
}
o1 := bucket.Object("a1")
o1.ContentType([]byte("application/json"))
if _, err := o1.Store(animal); err != nil {
t.Error(err.Error())
}
animal = &Animal{
Name: "dog",
}
o2 := bucket.Object("a2")
o2.ContentType([]byte("application/json"))
if _, err := o2.Store(animal); err != nil {
t.Error(err.Error())
}
var animals []Animal
query := session.Query()
if _, err := query.Key("Id").Out(&animals).Search([]byte("animals"), []byte("name:pig OR name:dog")); err != nil {
t.Error(err.Error())
}
if len(animals) != 2 {
t.Error("expected 2 documents")
}
if animals[0].Id != "a1" && animals[0].Id != "a2" {
t.Error("unexpected key")
}
if _, err := o1.Delete(); err != nil {
t.Error(err.Error())
}
if _, err := o2.Delete(); err != nil {
t.Error(err.Error())
}
}