forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy_aggregation_embedded_test.go
91 lines (74 loc) · 2.01 KB
/
lazy_aggregation_embedded_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
package tests
import (
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func lazyAggregationEmbeddedLazyTest(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
index := NewTaskIndex()
session := openSessionMust(t, store)
task1 := &Task{
AssigneeID: "users/1",
ID: "tasks/1",
}
task2 := &Task{
AssigneeID: "users/1",
ID: "tasks/2",
}
task3 := &Task{
AssigneeID: "users/2",
ID: "tasks/3",
}
err = session.Store(task1)
assert.NoError(t, err)
err = session.Store(task2)
assert.NoError(t, err)
err = session.Store(task3)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
err = index.Execute(store, nil, "")
assert.NoError(t, err)
err = driver.waitForIndexing(store, "", 0)
assert.NoError(t, err)
q := session.QueryIndex(index.IndexName)
f := ravendb.NewFacetBuilder()
f.ByField("AssigneeID").WithDisplayName("AssigneeID")
query := q.AggregateByFacet(f.GetFacet())
lazyOperation, err := query.ExecuteLazy()
assert.NoError(t, err)
facetValue := map[string]*ravendb.FacetResult{}
err = lazyOperation.GetValue(&facetValue)
assert.NoError(t, err)
values := facetValue["AssigneeID"].Values
userStats := map[string]int{}
for _, value := range values {
r := value.Range
c := value.Count
userStats[r] = c
}
assert.Equal(t, userStats["users/1"], 2)
assert.Equal(t, userStats["users/2"], 1)
session.Close()
}
}
func NewTaskIndex() *ravendb.IndexCreationTask {
res := ravendb.NewIndexCreationTask("TaskIndex")
res.Map = "from task in docs.Tasks select new { task.AssigneeID } "
return res
}
type Task struct {
ID string
AssigneeID string
}
func TestLazyAggregationEmbeddedLazy(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
lazyAggregationEmbeddedLazyTest(t, driver)
}