This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
indexer_test.go
128 lines (107 loc) · 3.04 KB
/
indexer_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
package bucket
import (
"context"
"testing"
"github.com/couchbase/gocb"
"github.com/stretchr/testify/assert"
)
func TestIndexCreate(t *testing.T) {
type webshopWithNonPointerNestedStruct struct {
webshop
Something string `json:"something" cb_indexable:"true"`
NestedData struct {
Data1 int `json:"data_1"`
}
}
instance := webshopWithNonPointerNestedStruct{}
if err := th.Index(context.Background(), instance); err != nil {
t.Fatal(err)
}
indexes, err := th.GetManager(context.Background()).GetIndexes()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, 5, len(indexes))
for _, ind := range indexes {
t.Logf("%+v", ind.Name)
}
}
func TestPrimaryIndexCreateError(t *testing.T) {
h := defaultHandler()
_ = h.state.bucket.Close()
assert.NotNil(t, h.Index(context.Background(), webshop{}))
}
func TestExistingIndex(t *testing.T) {
if err := th.Index(context.Background(), webshop{}); err != nil {
t.Fatal(err)
}
if err := th.Index(context.Background(), webshop{}); err != nil {
t.Fatal(err)
}
}
func TestMakeIndex(t *testing.T) {
assert.Nil(t, makeIndex(th.GetManager(context.Background()), "randomIndexName", []string{"randomField"}))
assert.Nil(t, th.GetManager(context.Background()).DropIndex("randomIndexName", true))
}
func TestMakeIndexMissingIndexName(t *testing.T) {
h := defaultHandler()
assert.NotNil(t, makeIndex(h.GetManager(context.Background()), "", nil))
}
func TestDropAndCreateMissingIndexName(t *testing.T) {
h := defaultHandler()
assert.NotNil(t, dropAndCreateIndex(h.GetManager(context.Background()), "", nil))
}
func BenchmarkCreateIndex(b *testing.B) {
for i := 0; i < b.N; i++ {
instance := webshop{}
if err := th.Index(context.Background(), instance); err != nil {
b.Fatal(err)
}
//indexes, _ := th.GetManager(context.Background()).GetIndexes()
//for _, ind := range indexes {
// _ = th.GetManager(context.Background()).DropIndex(ind.Name, true)
//}
}
}
func BenchmarkWithIndex(b *testing.B) {
b.StopTimer()
if err := th.Index(context.Background(), webshop{}); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := searchIndexedProperty(&testing.T{})
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkWithoutIndex(b *testing.B) {
b.StopTimer()
if err := th.Index(context.Background(), webshop{}); err != nil {
b.Fatal(err)
}
b.StartTimer()
for i := 0; i < b.N; i++ {
_, err := searchNotIndexedProperty(&testing.T{})
if err != nil {
b.Fatal(err)
}
}
}
func searchIndexedProperty(t *testing.T) (gocb.QueryResults, error) {
resp, err := th.state.bucket.ExecuteN1qlQuery(gocb.NewN1qlQuery("select * from `company` where CONTAINS(email, $1)"), []interface{}{"a"})
if err != nil {
return nil, err
}
_ = resp.Close()
return resp, nil
}
func searchNotIndexedProperty(t *testing.T) (gocb.QueryResults, error) {
resp, err := th.state.bucket.ExecuteN1qlQuery(gocb.NewN1qlQuery("select * from `company` where CONTAINS(billing_address_address_2, $1)"), []interface{}{"a"})
if err != nil {
return nil, err
}
_ = resp.Close()
return resp, nil
}