-
Notifications
You must be signed in to change notification settings - Fork 0
/
nested_structs_test.go
207 lines (195 loc) · 3.47 KB
/
nested_structs_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
// Copyright 2019 Tim Shannon. All rights reserved.
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package badgerhold_test
import (
"reflect"
"testing"
badgerhold "github.com/inits/badgerholdv2"
)
type Nested struct {
Key int
Embed
L1 Nest
L2 Level2
Pointer *Nest
}
type Embed struct {
Color string
}
type Nest struct {
Name string
}
type Level2 struct {
Name string
L3 Nest
}
var nestedData = []Nested{
{
Key: 0,
Embed: Embed{
Color: "red",
},
L1: Nest{
Name: "Joe",
},
L2: Level2{
Name: "Joe",
L3: Nest{
Name: "Joe",
},
},
Pointer: &Nest{
Name: "Joe",
},
},
{
Key: 1,
Embed: Embed{
Color: "red",
},
L1: Nest{
Name: "Jill",
},
L2: Level2{
Name: "Jill",
L3: Nest{
Name: "Jill",
},
},
Pointer: &Nest{
Name: "Jill",
},
},
{
Key: 2,
Embed: Embed{
Color: "orange",
},
L1: Nest{
Name: "Jill",
},
L2: Level2{
Name: "Jill",
L3: Nest{
Name: "Jill",
},
},
Pointer: &Nest{
Name: "Jill",
},
},
{
Key: 3,
Embed: Embed{
Color: "orange",
},
L1: Nest{
Name: "Jill",
},
L2: Level2{
Name: "Jill",
L3: Nest{
Name: "Joe",
},
}, Pointer: &Nest{
Name: "Jill",
},
},
{
Key: 4,
Embed: Embed{
Color: "blue",
},
L1: Nest{
Name: "Abner",
},
L2: Level2{
Name: "Abner",
L3: Nest{
Name: "Abner",
},
}, Pointer: &Nest{
Name: "Abner",
},
},
}
var nestedTests = []test{
{
name: "Nested",
query: badgerhold.Where("L1.Name").Eq("Joe"),
result: []int{0},
},
{
name: "Embedded",
query: badgerhold.Where("Color").Eq("red"),
result: []int{0, 1},
},
{
name: "Embedded Explicit",
query: badgerhold.Where("Embed.Color").Eq("red"),
result: []int{0, 1},
},
{
name: "Nested Multiple Levels",
query: badgerhold.Where("L2.L3.Name").Eq("Joe"),
result: []int{0, 3},
},
{
name: "Pointer",
query: badgerhold.Where("Pointer.Name").Eq("Jill"),
result: []int{1, 2, 3},
},
{
name: "Sort",
query: badgerhold.Where("Key").Ge(0).SortBy("L2.L3.Name"),
result: []int{4, 1, 2, 0, 3},
},
{
name: "Sort On Pointer",
query: badgerhold.Where("Key").Ge(0).SortBy("Pointer.Name"),
result: []int{4, 1, 2, 0, 3},
},
}
func TestNested(t *testing.T) {
testWrap(t, func(store *badgerhold.Store, t *testing.T) {
for i := range nestedData {
err := store.Insert(nestedData[i].Key, nestedData[i])
if err != nil {
t.Fatalf("Error inserting nested test data for nested find test: %s", err)
}
}
for _, tst := range nestedTests {
t.Run(tst.name, func(t *testing.T) {
var result []Nested
err := store.Find(&result, tst.query)
if err != nil {
t.Fatalf("Error finding data from badgerhold: %s", err)
}
if len(result) != len(tst.result) {
if testing.Verbose() {
t.Fatalf("Find result count is %d wanted %d. Results: %v", len(result),
len(tst.result), result)
}
t.Fatalf("Find result count is %d wanted %d.", len(result), len(tst.result))
}
for i := range result {
found := false
for k := range tst.result {
if reflect.DeepEqual(result[i], nestedData[tst.result[k]]) {
found = true
break
}
}
if !found {
if testing.Verbose() {
t.Fatalf("%v should not be in the result set! Full results: %v",
result[i], result)
}
t.Fatalf("%v should not be in the result set!", result[i])
}
}
})
}
})
}