forked from jmcvetta/neoism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_index_test.go
253 lines (240 loc) · 6.52 KB
/
node_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Copyright (c) 2012-2013 Jason McVetta. This is Free Software, released under
// the terms of the GPL v3. See http://www.gnu.org/copyleft/gpl.html for details.
// Resist intellectual serfdom - the ownership of ideas is akin to slavery.
package neoism
import (
"fmt"
"github.com/stretchr/testify/assert"
"strconv"
"testing"
)
// 18.9.1. Create node index
func TestCreateLegacyNodeIndex(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
//
// Create new index
//
idx0, err := db.CreateLegacyNodeIndex(name, "", "")
if err != nil {
t.Error(err)
}
defer idx0.Delete()
assert.Equal(t, idx0.Name, name)
//
// Get the index we just created
//
idx1, err := db.LegacyNodeIndex(name)
if err != nil {
t.Error(err)
}
assert.Equal(t, idx0.Name, idx1.Name)
assert.Equal(t, idx0.HrefIndex, idx1.HrefIndex)
}
// 18.9.2. Create node index with configuration
func TestLegacyNodeIndexCreateWithConf(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
indexType := "fulltext"
provider := "lucene"
//
// Create new index
//
idx0, err := db.CreateLegacyNodeIndex(name, indexType, provider)
if err != nil {
t.Error(err)
}
defer idx0.Delete()
assert.Equal(t, idx0.IndexType, indexType)
assert.Equal(t, idx0.Provider, provider)
assert.Equal(t, idx0.Name, name)
//
// Get the index we just created
//
idx1, err := db.LegacyNodeIndex(name)
if err != nil {
t.Error(err)
}
assert.Equal(t, idx0.Name, idx1.Name)
}
// 18.9.3. Delete node index
func TestDeleteLegacyNodeIndex(t *testing.T) {
db := connectTest(t)
// Include a space in the name to ensure correct URL escaping.
name := rndStr(t) + " " + rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
err := idx0.Delete()
if err != nil {
t.Error(err)
}
_, err = db.LegacyNodeIndex(name)
assert.Equal(t, err, NotFound)
}
// 18.9.4. List node indexes
func TestListLegacyNodeIndexes(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
defer idx0.Delete()
indexes, err := db.LegacyNodeIndexes()
if err != nil {
t.Error(err)
}
valid := false
for _, i := range indexes {
if i.Name == name {
valid = true
}
}
assert.True(t, valid, "Newly created Index not found in listing of all Indexes.")
}
// 18.9.5. Add node to index
func TestAddNodeToIndex(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
key := rndStr(t)
value := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{})
defer n0.Delete()
err := idx0.Add(n0, key, value)
if err != nil {
t.Error(err)
}
}
func TestAddNodeToExistingIndex(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
key := rndStr(t)
value := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
defer idx0.Delete()
idx1, _ := db.LegacyNodeIndex(name)
n0, _ := db.CreateNode(Props{})
defer n0.Delete()
err := idx1.Add(n0, key, value)
if err != nil {
t.Fatal(err)
}
}
// 18.9.6. Remove all entries with a given node from an index
func TestRemoveNodeFromIndex(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
key := rndStr(t)
value := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{})
defer n0.Delete()
idx0.Add(n0, key, value)
err := idx0.Remove(n0, "", "")
if err != nil {
t.Error(err)
}
}
// 18.9.7. Remove all entries with a given node and key from an indexj
func TestRemoveNodeAndKeyFromIndex(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
key := rndStr(t)
value := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{})
defer n0.Delete()
idx0.Add(n0, key, value)
err := idx0.Remove(n0, key, "")
if err != nil {
t.Error(err)
}
}
// 18.9.8. Remove all entries with a given node, key and value from an index
func TestRemoveNodeKeyAndValueFromIndex(t *testing.T) {
db := connectTest(t)
name := rndStr(t)
key := rndStr(t)
value := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(name, "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{})
defer n0.Delete()
idx0.Add(n0, key, value)
err := idx0.Remove(n0, key, "")
if err != nil {
t.Error(err)
}
}
// 18.9.9. Find node by exact match
func TestFindNodeByExactMatch(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
idxName := rndStr(t)
key0 := rndStr(t)
key1 := rndStr(t)
value0 := rndStr(t)
value1 := rndStr(t)
idx0, _ := db.CreateLegacyNodeIndex(idxName, "", "")
defer idx0.Delete()
n0, _ := db.CreateNode(Props{})
n1, _ := db.CreateNode(Props{})
n2, _ := db.CreateNode(Props{})
// These two will be located by Find() below
idx0.Add(n0, key0, value0)
idx0.Add(n1, key0, value0)
// These two will NOT be located by Find() below
idx0.Add(n2, key1, value0)
idx0.Add(n2, key0, value1)
//
nodes, err := idx0.Find(key0, value0)
if err != nil {
t.Error(err)
}
// This query should have returned a map containing just two nodes, n1 and n0.
assert.Equal(t, len(nodes), 2)
_, present := nodes[n0.Id()]
assert.True(t, present, "Find() failed to return node with id "+strconv.Itoa(n0.Id()))
_, present = nodes[n1.Id()]
assert.True(t, present, "Find() failed to return node with id "+strconv.Itoa(n1.Id()))
}
// 18.9.10. Find node by query
func TestFindNodeByQuery(t *testing.T) {
db := connectTest(t)
defer cleanup(t, db)
// Create
idx0, _ := db.CreateLegacyNodeIndex("test index", "", "")
defer idx0.Delete()
key0 := rndStr(t)
key1 := rndStr(t)
value0 := rndStr(t)
value1 := rndStr(t)
n0, _ := db.CreateNode(Props{})
idx0.Add(n0, key0, value0)
idx0.Add(n0, key1, value1)
n1, _ := db.CreateNode(Props{})
idx0.Add(n1, key0, value0)
n2, _ := db.CreateNode(Props{})
idx0.Add(n2, rndStr(t), rndStr(t))
// Retrieve
luceneQuery0 := fmt.Sprintf("%v:%v AND %v:%v", key0, value0, key1, value1) // Retrieve n0 only
luceneQuery1 := fmt.Sprintf("%v:%v", key0, value0) // Retrieve n0 and n1
nodes0, err := idx0.Query(luceneQuery0)
if err != nil {
t.Error(err)
}
nodes1, err := idx0.Query(luceneQuery1)
if err != nil {
t.Error(err)
}
// Confirm
assert.Equal(t, len(nodes0), 1, "Query should have returned only one Node.")
_, present := nodes0[n0.Id()]
assert.True(t, present, "Query() failed to return node with id "+strconv.Itoa(n0.Id()))
assert.Equal(t, len(nodes1), 2, "Query should have returned exactly 2 Nodes.")
_, present = nodes1[n0.Id()]
assert.True(t, present, "Query() failed to return node with id "+strconv.Itoa(n0.Id()))
_, present = nodes1[n1.Id()]
assert.True(t, present, "Query() failed to return node with id "+strconv.Itoa(n1.Id()))
}