forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacet_paging_test.go
249 lines (201 loc) · 5.99 KB
/
facet_paging_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
package tests
import (
"sort"
"strings"
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
const (
numCameras = 1000
)
var (
_data []*Camera
)
func facetPaging_canPerformFacetedPagingSearchWithNoPageSizeNoMaxResults_HitsDesc(t *testing.T, driver *RavenTestDriver) {
facetOptions := ravendb.NewFacetOptions()
facetOptions.Start = 2
facetOptions.TermSortMode = ravendb.FacetTermSortModeCountDesc
facetOptions.IncludeRemainingTerms = true
facet := ravendb.NewFacet()
facet.FieldName = "manufacturer"
facet.Options = facetOptions
facets := []*ravendb.Facet{facet}
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
facetPaging_setup(t, store)
{
session := openSessionMust(t, store)
facetSetup := &ravendb.FacetSetup{}
facetSetup.ID = "facets/CameraFacets"
facetSetup.Facets = facets
err = session.Store(facetSetup)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
q := session.QueryIndex("CameraCost")
ag := q.AggregateUsing("facets/CameraFacets")
facetResults, err := ag.Execute()
assert.NoError(t, err)
cameraCounts := map[string]int{}
for _, camera := range _data {
cameraCounts[camera.Manufacturer]++
}
var camerasByHits []string
for c := range cameraCounts {
camerasByHits = append(camerasByHits, c)
}
sort.Slice(camerasByHits, func(i, j int) bool {
namei := camerasByHits[i]
namej := camerasByHits[j]
ci := cameraCounts[namei]
cj := cameraCounts[namej]
if ci != cj {
return cj < ci // reverse order
}
return namej > namei
})
camerasByHits = camerasByHits[2:]
for i, s := range camerasByHits {
camerasByHits[i] = strings.ToLower(s)
}
vals := facetResults["manufacturer"].Values
assert.Equal(t, len(vals), 3)
assert.Equal(t, vals[0].Range, camerasByHits[0])
assert.Equal(t, vals[1].Range, camerasByHits[1])
assert.Equal(t, vals[2].Range, camerasByHits[2])
for _, f := range vals {
fM := strings.ToLower(f.Range)
inMemoryCount := 0
for _, camera := range _data {
camM := strings.ToLower(camera.Manufacturer)
if camM == fM {
inMemoryCount++
}
}
assert.Equal(t, f.Count, inMemoryCount)
}
fr := facetResults["manufacturer"]
assert.Equal(t, fr.RemainingTermsCount, 0)
assert.Equal(t, len(fr.RemainingTerms), 0)
assert.Equal(t, fr.RemainingHits, 0)
session.Close()
}
}
func facetPaging_canPerformFacetedPagingSearchWithNoPageSizeWithMaxResults_HitsDesc(t *testing.T, driver *RavenTestDriver) {
facetOptions := ravendb.NewFacetOptions()
facetOptions.Start = 2
facetOptions.PageSize = 2
facetOptions.TermSortMode = ravendb.FacetTermSortModeCountDesc
facetOptions.IncludeRemainingTerms = true
facet := ravendb.NewFacet()
facet.FieldName = "manufacturer"
facet.Options = facetOptions
facets := []*ravendb.Facet{facet}
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
facetPaging_setup(t, store)
{
session := openSessionMust(t, store)
facetSetup := &ravendb.FacetSetup{}
facetSetup.ID = "facets/CameraFacets"
facetSetup.Facets = facets
err = session.Store(facetSetup)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
q := session.QueryIndex("CameraCost")
ag := q.AggregateUsing("facets/CameraFacets")
facetResults, err := ag.Execute()
assert.NoError(t, err)
cameraCounts := map[string]int{}
for _, camera := range _data {
cameraCounts[camera.Manufacturer]++
}
var camerasByHits []string
for c := range cameraCounts {
camerasByHits = append(camerasByHits, c)
}
sort.Slice(camerasByHits, func(i, j int) bool {
namei := camerasByHits[i]
namej := camerasByHits[j]
ci := cameraCounts[namei]
cj := cameraCounts[namej]
if ci != cj {
return cj < ci // reverse order
}
return namej > namei
})
camerasByHits = camerasByHits[2:]
if len(camerasByHits) > 2 {
camerasByHits = camerasByHits[:2]
}
for i, s := range camerasByHits {
camerasByHits[i] = strings.ToLower(s)
}
vals := facetResults["manufacturer"].Values
assert.Equal(t, len(vals), 2)
assert.Equal(t, vals[0].Range, camerasByHits[0])
assert.Equal(t, vals[1].Range, camerasByHits[1])
for _, f := range vals {
fM := strings.ToLower(f.Range)
inMemoryCount := 0
for _, camera := range _data {
camM := strings.ToLower(camera.Manufacturer)
if camM == fM {
inMemoryCount++
}
}
assert.Equal(t, f.Count, inMemoryCount)
}
// Note: Java does it inside the above loop, for no reason
fr := facetResults["manufacturer"]
assert.Equal(t, fr.RemainingTermsCount, 1)
assert.Equal(t, len(fr.RemainingTerms), 1)
var counts []int
for _, count := range cameraCounts {
counts = append(counts, count)
}
sort.Ints(counts)
assert.Equal(t, counts[0], fr.RemainingHits)
session.Close()
}
}
func facetPaging_setup(t *testing.T, store *ravendb.DocumentStore) {
s, err := store.OpenSession("")
assert.NoError(t, err)
defer s.Close()
indexDefinition := &ravendb.IndexDefinition{}
indexDefinition.Name = "CameraCost"
indexDefinition.Maps = []string{
"from camera in docs select new { camera.manufacturer, camera.model, camera.cost, camera.dateOfListing, camera.megapixels } ",
}
op := ravendb.NewPutIndexesOperation(indexDefinition)
err = store.Maintenance().Send(op)
assert.NoError(t, err)
counter := 0
for _, camera := range _data {
err = s.Store(camera)
assert.NoError(t, err)
counter++
if counter%(numCameras/25) == 0 {
err = s.SaveChanges()
assert.NoError(t, err)
}
}
err = s.SaveChanges()
assert.NoError(t, err)
err = waitForIndexing(store, "", 0)
assert.NoError(t, err)
}
func TestFacetPaging(t *testing.T) {
_data = facetTestBaseGetCameras(numCameras)
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
facetPaging_canPerformFacetedPagingSearchWithNoPageSizeNoMaxResults_HitsDesc(t, driver)
facetPaging_canPerformFacetedPagingSearchWithNoPageSizeWithMaxResults_HitsDesc(t, driver)
}