forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch_test.go
178 lines (143 loc) · 4.28 KB
/
patch_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
package tests
import (
"reflect"
"strings"
"testing"
ravendb "github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func patchTestcanPatchSingleDocument(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user := &User{}
user.setName("RavenDB")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
patchRequest := &ravendb.PatchRequest{
Script: `this.name = "Patched"`,
}
patchOperation, err := ravendb.NewPatchOperation("users/1", nil, patchRequest, nil, false)
assert.NoError(t, err)
patchResult, err := store.Operations().SendPatchOperation(patchOperation, nil)
assert.NoError(t, err)
assert.Equal(t, patchResult.Status, ravendb.PatchStatusPatched)
{
session := openSessionMust(t, store)
var loadedUser *User
err = session.Load(&loadedUser, "users/1")
assert.NoError(t, err)
assert.Equal(t, *loadedUser.Name, "Patched")
session.Close()
}
}
func patchTestCanWaitForIndexAfterPatch(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
usersByName := NewUsers_ByName()
err = usersByName.Execute(store, nil, "")
assert.NoError(t, err)
{
session := openSessionMust(t, store)
user := &User{}
user.setName("RavenDB")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
builder := func(x *ravendb.IndexesWaitOptsBuilder) {
x.WaitForIndexes("Users/ByName")
}
session.Advanced().WaitForIndexesAfterSaveChanges(builder)
var user *User
err = session.Load(&user, "users/1")
assert.NoError(t, err)
err = session.Advanced().Patch(user, "name", "New Name")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
}
func patchTestcanPatchManyDocuments(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user := &User{}
user.setName("RavenDB")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
// TODO: we crash if it's Query() and not QueryType()
// (need to validate and not crash)
clazz := reflect.TypeOf(&User{})
q := session.QueryCollectionForType(clazz)
lazy, err := q.CountLazily()
assert.NoError(t, err)
var n int
err = lazy.GetValue(&n)
assert.NoError(t, err)
assert.Equal(t, n, 1)
session.Close()
}
operation := ravendb.NewPatchByQueryOperation("from Users update { this.name= \"Patched\" }")
op, err := store.Operations().SendAsync(operation, nil)
assert.NoError(t, err)
err = op.WaitForCompletion()
assert.NoError(t, err)
{
session := openSessionMust(t, store)
var loadedUser *User
err = session.Load(&loadedUser, "users/1")
assert.NoError(t, err)
assert.Equal(t, *loadedUser.Name, "Patched")
session.Close()
}
}
func patchTestthrowsOnInvalidScript(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
session := openSessionMust(t, store)
user := &User{}
user.setName("RavenDB")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
operation := ravendb.NewPatchByQueryOperation("from Users update { throw 5 }")
op, err := store.Operations().SendAsync(operation, nil)
assert.NoError(t, err)
err = op.WaitForCompletion()
assert.Error(t, err)
// TODO: make sure it's an instance of JavaScriptException ? Currently is RavenError
assert.True(t, strings.Contains(err.Error(), "Raven.Client.Exceptions.Documents.Patching.JavaScriptException"))
}
func TestPatch(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// order matches Java tests
patchTestcanPatchManyDocuments(t, driver)
patchTestthrowsOnInvalidScript(t, driver)
patchTestcanPatchSingleDocument(t, driver)
// TODO: not in order of Java
patchTestCanWaitForIndexAfterPatch(t, driver)
}