-
Notifications
You must be signed in to change notification settings - Fork 16
/
load_into_stream_test.go
135 lines (108 loc) · 3.07 KB
/
load_into_stream_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
package tests
import (
"bytes"
"encoding/json"
"io/ioutil"
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func loadIntoStreamCanLoadByIdsIntoStream(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
insertData(t, store)
{
session := openSessionMust(t, store)
stream := bytes.NewBuffer(nil)
ids := []string{"employee2s/1-A", "employee2s/4-A", "employee2s/7-A"}
err = session.Advanced().LoadIntoStream(ids, stream)
assert.NoError(t, err)
d, err := ioutil.ReadAll(stream)
assert.NoError(t, err)
var jsonNode map[string]interface{}
err = json.Unmarshal(d, &jsonNode)
assert.NoError(t, err)
res := jsonNode["Results"]
a := res.([]interface{})
assert.Equal(t, len(a), 3)
names := []string{"Aviv", "Maxim", "Michael"}
for _, v := range a {
v2 := v.(map[string]interface{})
s, _ := jsonGetAsText(v2, "firstName")
assert.True(t, stringArrayContains(names, s))
}
session.Close()
}
}
func loadIntoStreamCanLoadStartingWithIntoStream(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
insertData(t, store)
{
session := openSessionMust(t, store)
stream := bytes.NewBuffer(nil)
args := &ravendb.StartsWithArgs{
StartsWith: "employee2s/",
}
err = session.Advanced().LoadStartingWithIntoStream(stream, args)
assert.NoError(t, err)
d, err := ioutil.ReadAll(stream)
assert.NoError(t, err)
var jsonNode map[string]interface{}
err = json.Unmarshal(d, &jsonNode)
assert.NoError(t, err)
res := jsonNode["Results"]
a := res.([]interface{})
assert.Equal(t, len(a), 7)
names := []string{"Aviv", "Iftah", "Tal", "Maxim", "Karmel", "Grisha", "Michael"}
for _, v := range a {
v2 := v.(map[string]interface{})
s, _ := jsonGetAsText(v2, "firstName")
assert.True(t, stringArrayContains(names, s))
}
session.Close()
}
}
func insertData(t *testing.T, store *ravendb.DocumentStore) {
var err error
{
session := openSessionMust(t, store)
insertEmployee := func(name string) error {
employee := &Employee2{
FirstName: name,
}
return session.Store(employee)
}
err = insertEmployee("Aviv")
assert.NoError(t, err)
err = insertEmployee("Iftah")
assert.NoError(t, err)
err = insertEmployee("Tal")
assert.NoError(t, err)
err = insertEmployee("Maxim")
assert.NoError(t, err)
err = insertEmployee("Karmel")
assert.NoError(t, err)
err = insertEmployee("Grisha")
assert.NoError(t, err)
err = insertEmployee("Michael")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
}
// Note: conflicts with Employee in employee_test.go
type Employee2 struct {
FirstName string `json:"firstName"`
}
func TestLoadIntoStream(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// matches order of Java tests
loadIntoStreamCanLoadStartingWithIntoStream(t, driver)
loadIntoStreamCanLoadByIdsIntoStream(t, driver)
}