-
Notifications
You must be signed in to change notification settings - Fork 0
/
plan_test.go
168 lines (140 loc) · 4.09 KB
/
plan_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
/*
* go-leia
* Copyright (C) 2021 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 (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"go.etcd.io/bbolt"
)
func TestFullTableScanQueryPlan_execute(t *testing.T) {
t.Run("ok - returns nil when no globalDocument bucket exists", func(t *testing.T) {
_, c := testCollection(t)
queryPlan := fullTableScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
},
}
err := queryPlan.execute(func(key Reference, value []byte) error {
// should not be called
return errors.New("failed")
})
assert.NoError(t, err)
})
t.Run("error - when walker returns an error", func(t *testing.T) {
_, c := testCollection(t)
_ = c.Add([]Document{exampleDoc})
queryPlan := fullTableScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
},
}
err := queryPlan.execute(func(key Reference, value []byte) error {
// should not be called
return errors.New("failed")
})
assert.EqualError(t, err, "failed")
})
}
func TestIndexScanQueryPlan_Execute(t *testing.T) {
t.Run("error - query does not exactly match index", func(t *testing.T) {
_, _, i := testIndex(t)
queryPlan := indexScanQueryPlan{
queryPlanBase: queryPlanBase{
query: New(Eq(NewJSONPath("key"), valueAsScalar)).And(Eq(NewJSONPath("not_indexed"), valueAsScalar)),
},
index: i,
}
err := queryPlan.execute(func(key []byte, value []byte) error {
// should not be called
return errors.New("failed in loop")
})
assert.EqualError(t, err, "no index with exact match to query found")
})
t.Run("ok - nothing added", func(t *testing.T) {
_, c, i := testIndex(t)
queryPlan := indexScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
query: New(Eq(NewJSONPath("path.part"), valueAsScalar)),
},
index: i,
}
err := queryPlan.execute(func(key []byte, value []byte) error {
// should not be called
return errors.New("failed")
})
assert.NoError(t, err)
})
}
func TestResultScanQueryPlan_Execute(t *testing.T) {
t.Run("ok - nothing added", func(t *testing.T) {
_, c, i := testIndex(t)
queryPlan := resultScanQueryPlan{
queryPlanBase: queryPlanBase{
collection: c,
query: New(Eq(NewJSONPath("key"), valueAsScalar)),
},
index: i,
}
err := queryPlan.execute(func(key Reference, value []byte) error {
// should not be called
return errors.New("failed")
})
assert.NoError(t, err)
})
}
func TestDocumentFetcher(t *testing.T) {
t.Run("ok - nil bytes passed", func(t *testing.T) {
db, c := testCollection(t)
_ = c.Add([]Document{exampleDoc})
err := db.View(func(tx *bbolt.Tx) error {
fetcher := documentFetcher(tx.Bucket(documentCollectionByteRef()), func(_ []byte, _ []byte) error {
return errors.New("failed")
})
return fetcher(nil, nil)
})
assert.NoError(t, err)
})
}
func TestResultScanner(t *testing.T) {
json := `
{
"id": 1,
"main": {
"nesting": {
"type": "bird",
"nice": false
}
}
}
`
bytes := []byte(json)
t.Run("error - non comparable entry", func(t *testing.T) {
db, c := testCollection(t)
_ = c.Add([]Document{exampleDoc})
err := db.View(func(tx *bbolt.Tx) error {
scanner := resultScanner([]QueryPart{Eq(NewJSONPath("main.nesting"), valueAsScalar)}, func(_ Reference, _ []byte) error {
return errors.New("failed")
}, c)
return scanner(nil, bytes)
})
assert.EqualError(t, err, "type at path not supported for indexing: {\n\t\t\t\"type\": \"bird\",\n\t\t\t\"nice\": false\n\t\t}")
})
}