-
Notifications
You must be signed in to change notification settings - Fork 16
/
attachments_revisions_test.go
352 lines (301 loc) · 10.9 KB
/
attachments_revisions_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package tests
import (
"bytes"
"io/ioutil"
"runtime"
"sort"
"strings"
"testing"
"github.com/ravendb/ravendb-go-client"
"github.com/stretchr/testify/assert"
)
func attachmentsRevisionsPutAttachments(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
_, err = setupRevisions(store, false, 4)
assert.NoError(t, err)
names := createDocumentWithAttachments(t, store)
{
f := func(t *testing.T, session *ravendb.DocumentSession, revisions []*User) {
assertRevisionAttachments(t, names, 3, revisions[0], session)
assertRevisionAttachments(t, names, 2, revisions[1], session)
assertRevisionAttachments(t, names, 1, revisions[2], session)
assertNoRevisionAttachment(t, revisions[3], session, false)
}
assertRevisions(t, store, names, f, 9)
}
{
// Delete document should delete all the attachments
f := func(t *testing.T, session *ravendb.DocumentSession, revisions []*User) {
assertNoRevisionAttachment(t, revisions[0], session, true)
assertRevisionAttachments(t, names, 3, revisions[1], session)
assertRevisionAttachments(t, names, 2, revisions[2], session)
assertRevisionAttachments(t, names, 1, revisions[3], session)
}
cmd := ravendb.NewDeleteDocumentCommand("users/1", nil)
err = store.GetRequestExecutor("").ExecuteCommand(cmd, nil)
assert.NoError(t, err)
assertRevisions2(t, store, names, f, 6, 0, 3)
}
{
// Create another revision which should delete old revision
session := openSessionMust(t, store)
// This will delete the revision #1 which is without attachment
user := &User{}
user.setName("Fitzchak 2")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
f := func(t *testing.T, session *ravendb.DocumentSession, revisions []*User) {
// This will delete the revision #2 which is with attachment
assertNoRevisionAttachment(t, revisions[0], session, false)
assertNoRevisionAttachment(t, revisions[1], session, true)
assertRevisionAttachments(t, names, 3, revisions[2], session)
assertRevisionAttachments(t, names, 2, revisions[3], session)
}
assertRevisions2(t, store, names, f, 5, 1, 3)
}
{
session := openSessionMust(t, store)
// This will delete the revision #2 which is with attachment
user := &User{}
user.setName("Fitzchak 3")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
f := func(t *testing.T, session *ravendb.DocumentSession, revisions []*User) {
// This will delete the revision #2 which is with attachment
assertNoRevisionAttachment(t, revisions[0], session, false)
assertNoRevisionAttachment(t, revisions[1], session, false)
assertNoRevisionAttachment(t, revisions[2], session, true)
assertRevisionAttachments(t, names, 3, revisions[3], session)
}
assertRevisions2(t, store, names, f, 3, 1, 3)
}
{
session := openSessionMust(t, store)
// This will delete the revision #3 which is with attachment
user := &User{}
user.setName("Fitzchak 4")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
f := func(t *testing.T, session *ravendb.DocumentSession, revisions []*User) {
// This will delete the revision #2 which is with attachment
assertNoRevisionAttachment(t, revisions[0], session, false)
assertNoRevisionAttachment(t, revisions[1], session, false)
assertNoRevisionAttachment(t, revisions[2], session, false)
assertNoRevisionAttachment(t, revisions[3], session, true)
}
assertRevisions2(t, store, names, f, 0, 1, 0)
}
{
session := openSessionMust(t, store)
// This will delete the revision #4 which is with attachment
user := &User{}
user.setName("Fitzchak 5")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
f := func(t *testing.T, session *ravendb.DocumentSession, revisions []*User) {
// This will delete the revision #2 which is with attachment
assertNoRevisionAttachment(t, revisions[0], session, false)
assertNoRevisionAttachment(t, revisions[1], session, false)
assertNoRevisionAttachment(t, revisions[2], session, false)
assertNoRevisionAttachment(t, revisions[3], session, false)
}
assertRevisions2(t, store, names, f, 0, 1, 0)
}
}
}
func attachmentsRevisionsAttachmentRevision(t *testing.T, driver *RavenTestDriver) {
var err error
store := driver.getDocumentStoreMust(t)
defer store.Close()
{
_, err = setupRevisions(store, false, 4)
assert.NoError(t, err)
createDocumentWithAttachments(t, store)
{
session := openSessionMust(t, store)
bais := bytes.NewBuffer([]byte{5, 4, 3, 2, 1})
err = session.Advanced().Attachments().StoreByID("users/1", "profile.png", bais, "")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
{
session := openSessionMust(t, store)
var revisions []*User
err = session.Advanced().Revisions().GetFor(&revisions, "users/1")
assert.NoError(t, err)
rev := revisions[1]
changeVector, err := session.Advanced().GetChangeVectorFor(rev)
assert.NoError(t, err)
{
revision, err := session.Advanced().Attachments().GetRevision("users/1", "profile.png", changeVector)
assert.NoError(t, err)
r := revision.Data
bytes, err := ioutil.ReadAll(r)
assert.NoError(t, err)
assert.Equal(t, len(bytes), 3)
assert.Equal(t, bytes, []byte{1, 2, 3})
}
session.Close()
}
}
}
func createDocumentWithAttachments(t *testing.T, store *ravendb.DocumentStore) []string {
var err error
{
session := openSessionMust(t, store)
user := &User{}
user.setName("Fitzchak")
err = session.StoreWithID(user, "users/1")
assert.NoError(t, err)
err = session.SaveChanges()
assert.NoError(t, err)
session.Close()
}
names := []string{
"profile.png",
"background-photo.jpg",
"fileNAME_#$1^%_בעברית.txt",
}
{
profileStream := bytes.NewBuffer([]byte{1, 2, 3})
op := ravendb.NewPutAttachmentOperation("users/1", names[0], profileStream, "image/png", nil)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
result := op.Command.Result
s := *result.ChangeVector
assert.True(t, strings.Contains(s, "A:3"))
assert.Equal(t, result.Name, names[0])
assert.Equal(t, result.DocumentID, "users/1")
assert.Equal(t, result.ContentType, "image/png")
}
{
backgroundStream := bytes.NewReader([]byte{10, 20, 30, 40, 50})
op := ravendb.NewPutAttachmentOperation("users/1", names[1], backgroundStream, "ImGgE/jPeG", nil)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
result := op.Command.Result
s := *result.ChangeVector
assert.True(t, strings.Contains(s, "A:7"))
assert.Equal(t, result.Name, names[1])
assert.Equal(t, result.DocumentID, "users/1")
assert.Equal(t, result.ContentType, "ImGgE/jPeG")
}
{
fileStream := bytes.NewReader([]byte{1, 2, 3, 4, 5})
op := ravendb.NewPutAttachmentOperation("users/1", names[2], fileStream, "", nil)
err = store.Operations().Send(op, nil)
assert.NoError(t, err)
result := op.Command.Result
s := *result.ChangeVector
assert.True(t, strings.Contains(s, "A:12"))
assert.Equal(t, result.Name, names[2])
assert.Equal(t, result.DocumentID, "users/1")
assert.Equal(t, result.ContentType, "")
}
return names
}
func assertRevisions(t *testing.T, store *ravendb.DocumentStore, names []string, assertAction func(*testing.T, *ravendb.DocumentSession, []*User), expectedCountOfAttachments int64) {
assertRevisions2(t, store, names, assertAction, expectedCountOfAttachments, 1, 3)
}
func assertRevisions2(t *testing.T, store *ravendb.DocumentStore, names []string, assertAction func(*testing.T, *ravendb.DocumentSession, []*User), expectedCountOfAttachments int64, expectedCountOfDocuments int64, expectedCountOfUniqueAttachments int64) {
op := ravendb.NewGetStatisticsOperation("")
err := store.Maintenance().Send(op)
assert.NoError(t, err)
statistics := op.Command.Result
assert.Equal(t, statistics.CountOfAttachments, expectedCountOfAttachments)
assert.Equal(t, statistics.CountOfUniqueAttachments, expectedCountOfUniqueAttachments)
assert.Equal(t, statistics.CountOfRevisionDocuments, int64(4))
assert.Equal(t, statistics.CountOfDocuments, expectedCountOfDocuments)
assert.Equal(t, statistics.CountOfIndexes, 0)
{
session := openSessionMust(t, store)
var revisions []*User
err = session.Advanced().Revisions().GetFor(&revisions, "users/1")
assert.NoError(t, err)
assertAction(t, session, revisions)
session.Close()
}
}
func assertNoRevisionAttachment(t *testing.T, revision *User, session *ravendb.DocumentSession, isDeleteRevision bool) {
metadata, err := session.Advanced().GetMetadataFor(revision)
assert.NoError(t, err)
if isDeleteRevision {
v, _ := metadata.Get(ravendb.MetadataFlags)
s := v.(string)
assert.True(t, strings.Contains(s, "HasRevisions"))
assert.True(t, strings.Contains(s, "DeleteRevision"))
} else {
v, _ := metadata.Get(ravendb.MetadataFlags)
s := v.(string)
assert.True(t, strings.Contains(s, "HasRevisions"))
assert.True(t, strings.Contains(s, "Revision"))
}
hasIt := metadata.ContainsKey(ravendb.MetadataAttachments)
assert.False(t, hasIt)
}
func assertRevisionAttachments(t *testing.T, names []string, expectedCount int, revision *User, session *ravendb.DocumentSession) {
metadata, err := session.Advanced().GetMetadataFor(revision)
assert.NoError(t, err)
v, _ := metadata.Get(ravendb.MetadataFlags)
s := v.(string)
assert.True(t, strings.Contains(s, "HasRevisions"))
assert.True(t, strings.Contains(s, "Revision"))
assert.True(t, strings.Contains(s, "Revision"))
attachments := metadata.GetObjects(ravendb.MetadataAttachments)
assert.Equal(t, len(attachments), expectedCount)
// Note: unlike Java, compare them after sorting
attachmentNames := make([]string, expectedCount, expectedCount)
for i := 0; i < expectedCount; i++ {
attachment := attachments[i]
aname, ok := attachment.Get("Name")
assert.True(t, ok)
anameStr, ok := aname.(string)
assert.True(t, ok)
attachmentNames[i] = anameStr
}
orderedNames := append([]string{}, names...)
if len(orderedNames) > expectedCount {
orderedNames = orderedNames[:expectedCount]
}
sort.Strings(orderedNames)
sort.Strings(attachmentNames)
assert.Equal(t, orderedNames, attachmentNames)
}
func TestAttachmentsRevisions(t *testing.T) {
driver := createTestDriver(t)
destroy := func() { destroyDriver(t, driver) }
defer recoverTest(t, destroy)
// Those are flaky on mac and mac only. I suspect server issue
// see https://github.com/ravendb/ravendb-go-client/issues/92
if runtime.GOOS == "darwin" && !enableFlakyTests {
return
}
// matches order of Java tests
attachmentsRevisionsPutAttachments(t, driver)
attachmentsRevisionsAttachmentRevision(t, driver)
}