-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_integration_test.go
175 lines (148 loc) · 4.3 KB
/
index_integration_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
/*
* go-leia
* Copyright (C) 2022 Nuts community
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
package leia
import (
"testing"
"github.com/stretchr/testify/assert"
"go.etcd.io/bbolt"
)
var doc1 = []byte(`
{
"key1": "1",
"key2": "23"
}
`)
var doc2 = []byte(`
{
"key1": "12",
"key2": "3"
}
`)
var doc3 = []byte(`
{
"key1": "0"
}
`)
// TestIndex_CursorDynamics contains a set of tests to see if all relevant documents are returned
func TestIndex_CursorDynamics(t *testing.T) {
ref1 := defaultReferenceCreator(doc1)
ref2 := defaultReferenceCreator(doc2)
ref3 := defaultReferenceCreator(doc3)
key1 := NewJSONPath("key1")
key2 := NewJSONPath("key2")
db, c := testCollection(t)
i := c.NewIndex(t.Name(),
NewFieldIndexer(key1),
NewFieldIndexer(key2),
)
_ = db.Update(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
_ = i.Add(b, ref1, doc1)
_ = i.Add(b, ref2, doc2)
return i.Add(b, ref3, doc3)
})
t.Run("2 docs found on single prefix key", func(t *testing.T) {
q := New(Prefix(key1, MustParseScalar("1")))
found := 0
err := db.View(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
return i.Iterate(b, q, func(key Reference, value []byte) error {
found++
return nil
})
})
assert.NoError(t, err)
assert.Equal(t, 2, found)
})
t.Run("2 docs found on single prefix key using duplicate key", func(t *testing.T) {
q := New(Prefix(key1, MustParseScalar("1"))).And(
Prefix(key1, MustParseScalar("1")))
found := 0
err := db.View(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
return i.Iterate(b, q, func(key Reference, value []byte) error {
found++
return nil
})
})
assert.NoError(t, err)
assert.Equal(t, 2, found)
})
t.Run("2 docs found on prefix key and notNil", func(t *testing.T) {
q := New(Prefix(key1, MustParseScalar("1"))).And(NotNil(key2))
found := 0
err := db.View(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
return i.Iterate(b, q, func(key Reference, value []byte) error {
found++
return nil
})
})
assert.NoError(t, err)
assert.Equal(t, 2, found)
})
t.Run("2 docs found on empty prefix key and notNil", func(t *testing.T) {
// the first hit for the cursor on the empty prefix is a hit where the second matcher fails (notNil)
// A bug prevented continuing evaluation by breaking instead of advancing the cursor and continuing
q := New(Prefix(key1, MustParseScalar(""))).And(NotNil(key2))
found := 0
err := db.View(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
return i.Iterate(b, q, func(key Reference, value []byte) error {
found++
return nil
})
})
assert.NoError(t, err)
assert.Equal(t, 2, found)
})
}
func TestIndex_Bugs(t *testing.T) {
t.Run("#28 iterator skipping value when it's shorter than the previous value", func(t *testing.T) {
doc0 := []byte(`{"key1": "06","key2": "1"}`)
doc1 := []byte(`{"key1": "0645"}`)
doc2 := []byte(`{"key1": "068","key2": "0682726"}`)
doc3 := []byte(`{"key1": "0682"}`)
key1 := NewJSONPath("key1")
key2 := NewJSONPath("key2")
db, c := testCollection(t)
i := c.NewIndex(t.Name(),
NewFieldIndexer(key1),
NewFieldIndexer(key2),
)
_ = db.Update(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
_ = i.Add(b, defaultReferenceCreator(doc0), doc0)
_ = i.Add(b, defaultReferenceCreator(doc1), doc1)
_ = i.Add(b, defaultReferenceCreator(doc2), doc2)
return i.Add(b, defaultReferenceCreator(doc3), doc3)
})
q := New(Prefix(key1, MustParseScalar("06"))).And(NotNil(key2))
found := 0
err := db.View(func(tx *bbolt.Tx) error {
b := testBucket(t, tx)
return i.Iterate(b, q, func(key Reference, value []byte) error {
found++
return nil
})
})
assert.NoError(t, err)
assert.Equal(t, 2, found)
})
}