-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
197 lines (172 loc) · 4.22 KB
/
index_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
package sod
import (
"errors"
"math/rand"
"testing"
"time"
"github.com/0xrawsec/toast"
)
func TestSearchNoIndexObject(t *testing.T) {
db := createFreshTestDb(10000, DefaultSchema)
defer db.Close()
start := time.Now()
if sr := db.Search(&testStruct{}, "A", "<", 10).And("C", "=", "bar").And("B", ">", 10); sr.Err() != nil {
t.Log(errors.Is(sr.Err(), ErrFieldNotIndexed))
t.Error(sr.Err())
} else {
t.Log(sr.Len())
t.Logf("Search time: %s", time.Since(start))
if objects, err := sr.Collect(); err != nil {
t.Error(err)
} else {
for _, o := range objects {
ts := o.(*testStruct)
switch {
case ts.A >= 10:
t.Error("A must be lesser than 10")
case ts.B <= 10:
t.Error("B must be greater than 10")
case ts.C != "bar":
t.Error("C must be equal to \"bar\"")
}
}
}
}
}
func TestSearchIndexObject(t *testing.T) {
db := createFreshTestDb(10000, DefaultSchema)
defer db.Close()
start := time.Now()
if sr := db.Search(&testStruct{}, "A", "<", 10).And("C", "=", "bar").And("B", ">", 10); sr.Err() != nil {
t.Log(errors.Is(sr.Err(), ErrFieldNotIndexed))
t.Error(sr.Err())
} else {
t.Log(sr.Len())
t.Logf("Search time: %s", time.Since(start))
if objects, err := sr.Collect(); err != nil {
t.Error(err)
} else {
for _, o := range objects {
ts := o.(*testStruct)
switch {
case ts.A >= 10:
t.Error("A must be lesser than 10")
case ts.B <= 10:
t.Error("B must be greater than 10")
case ts.C != "bar":
t.Error("C must be equal to \"bar\"")
}
}
}
}
}
func TestSearchOr(t *testing.T) {
db := createFreshTestDb(10000, DefaultSchema)
defer db.Close()
start := time.Now()
if sr := db.Search(&testStruct{}, "A", "<", 10).Or("C", "=", "bar"); sr.Err() != nil {
t.Log(errors.Is(sr.Err(), ErrFieldNotIndexed))
t.Error(sr.Err())
} else {
t.Log(sr.Len())
t.Logf("Search time: %s", time.Since(start))
if objects, err := sr.Collect(); err != nil {
t.Error(err)
} else {
for _, o := range objects {
ts := o.(*testStruct)
if !(ts.A < 10 || ts.C == "bar") {
t.Errorf("Or failed")
}
}
}
}
}
func TestSearchDeleteObject(t *testing.T) {
var s *Schema
var err error
deln := 0
size := 10000
db := createFreshTestDb(size, DefaultSchema)
defer db.Close()
if s, err = db.Schema(&testStruct{}); err != nil {
t.Error(err)
t.FailNow()
}
for fn, fi := range s.ObjectIndex.Fields {
t.Logf("FieldIndex %s size: %d", fn, fi.Len())
}
if s, err := db.All(&testStruct{}); err != nil {
t.Error(err)
} else {
// deleting deln objects
for _, o := range s {
if rand.Int()%2 == 0 {
t := o.(*testStruct)
db.delete(t)
deln++
}
}
}
if s.ObjectIndex.len() != size-deln {
t.Errorf("Expecting index length of %d got %d", size-deln, s.ObjectIndex.len())
}
for fn, fi := range s.ObjectIndex.Fields {
t.Logf("FieldIndex %s size after deletion: %d", fn, fi.Len())
if fi.Len() != size-deln {
t.Errorf("Expecting field index (%s) length of %d got %d", fn, size-deln, fi.Len())
}
}
start := time.Now()
if sr := db.Search(&testStruct{}, "A", "<", 10).And("C", "=", "bar").And("B", ">", 10); sr.Err() != nil {
t.Log(errors.Is(sr.Err(), ErrFieldNotIndexed))
t.Error(sr.Err())
} else {
t.Log(sr.Len())
t.Logf("Search time: %s", time.Since(start))
if objects, err := sr.Collect(); err != nil {
t.Error(err)
} else {
for _, o := range objects {
ts := o.(*testStruct)
switch {
case ts.A >= 10:
t.Error("A must be lesser than 10")
case ts.B <= 10:
t.Error("B must be greater than 10")
case ts.C != "bar":
t.Error("C must be equal to \"bar\"")
}
}
}
}
}
func TestFieldPath(t *testing.T) {
tt := toast.FromT(t)
s := &nestedStruct{
A: 41,
B: 42,
C: 43,
In: inner{
D: 44,
E: 45,
F: "F",
},
}
s.In.Anon.G = "G"
ts := &testStruct{Nested: s}
i, ok := fieldByName(s, fieldPath("A"))
tt.Assert(ok)
tt.Assert(i.(int) == 41)
i, ok = fieldByName(s, fieldPath("In.D"))
tt.Assert(ok)
tt.Assert(i.(float64) == 44)
i, ok = fieldByName(s, fieldPath("In.Anon.G"))
tt.Assert(ok)
tt.Assert(i.(string) == "G")
_, ok = fieldByName(s, fieldPath(""))
tt.Assert(!ok)
i, ok = fieldByName(ts, fieldPath("Nested.A"))
tt.Assert(i.(int) == 41)
tt.Assert(ok)
}