-
Notifications
You must be signed in to change notification settings - Fork 16
/
simple_multi_map_test.go
98 lines (76 loc) · 2.29 KB
/
simple_multi_map_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
package tests
import (
"testing"
"time"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func simpleMultiMap_canCreateMultiMapIndex(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
index := NewCatsAndDogs()
err = index.Execute(store, nil, "")
assert.NoError(t, err)
op := ravendb.NewGetIndexOperation("CatsAndDogs")
err = store.Maintenance().Send(op)
indexDefinition := op.Command.Result
assert.Equal(t, len(indexDefinition.Maps), 2)
}
func simpleMultiMap_canQueryUsingMultiMap(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
index := NewCatsAndDogs()
err = index.Execute(store, nil, "")
assert.NoError(t, err)
{
session := openSessionMust(t, store)
cat := &Cat{Name: "Tom"}
dog := &Dog{Name: "Oscar"}
err = session.Store(cat)
assert.NoError(t, err)
err = session.Store(dog)
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
// Note: Go doesn't support interfaces like Java. We can only
// query a single type, not an interface
var haveNames []*Dog
q := session.QueryIndex(index.IndexName)
q = q.WaitForNonStaleResults(time.Second * 10)
q = q.OrderBy("name")
err = q.GetResults(&haveNames)
assert.NoError(t, err)
assert.Equal(t, len(haveNames), 2)
assert.Equal(t, haveNames[0].Name, "Oscar")
assert.Equal(t, haveNames[1].Name, "Tom")
session.Close()
}
}
// Note: in Go IndexCreationTask covers functionality of
// AbstractMultiMapIndexCreationTask
func NewCatsAndDogs() *ravendb.IndexCreationTask {
res := ravendb.NewIndexCreationTask("CatsAndDogs")
res.Maps = []string{
"from cat in docs.Cats select new { cat.name }",
"from dog in docs.Dogs select new { dog.name }",
}
return res
}
type Cat struct {
Name string `json:"name"`
}
// Note: re-using a Dog structure from other test, which also has Name field
func TestSimpleMultiMap(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
simpleMultiMap_canCreateMultiMapIndex(t, driver)
simpleMultiMap_canQueryUsingMultiMap(t, driver)
}