-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathAttachmentsSessionTest.ts
336 lines (271 loc) · 12.3 KB
/
AttachmentsSessionTest.ts
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
import * as assert from "assert";
import { testContext, disposeTestDocumentStore } from "../../Utils/TestUtil";
import {
AttachmentName,
IDocumentStore,
DeleteAttachmentOperation,
DeleteCommandData
} from "../../../src";
import * as stream from "readable-stream";
import { User } from "../../Assets/Entities";
import { CONSTANTS } from "../../../src/Constants";
import * as StreamUtil from "../../../src/Utility/StreamUtil";
describe("Attachments Session", function () {
let store: IDocumentStore;
beforeEach(async function () {
store = await testContext.getDocumentStore();
});
afterEach(async () =>
await disposeTestDocumentStore(store));
it("can put attachments", async () => {
const attachmentsInfo = [
{ name: "profile.png", contentType: "image/png" },
{ name: "background-photo.jpg", contentType: "ImGge/jPeg" },
{ name: "fileNAME_#$1^%_בעברית.txt", contentType: undefined }
];
const profileStream = Buffer.from([1, 2, 3]);
const backgroundStream = Buffer.from([10, 20, 30, 40, 50]);
const fileStream = new stream.Readable();
[1, 2, 3, 4, 5].forEach(x => fileStream.push(Buffer.from(x.toString())));
fileStream.push(null);
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
session.advanced.attachments.store(
"users/1", attachmentsInfo[0].name, profileStream, attachmentsInfo[0].contentType);
session.advanced.attachments.store(
user, attachmentsInfo[1].name, backgroundStream, attachmentsInfo[1].contentType);
session.advanced.attachments.store(
user, attachmentsInfo[2].name, fileStream);
await session.saveChanges();
}
{
const session = store.openSession();
const user = await session.load<User>("users/1");
const metadata = session.advanced.getMetadataFor(user);
assert.strictEqual(metadata[CONSTANTS.Documents.Metadata.FLAGS], "HasAttachments");
const attachments = metadata[CONSTANTS.Documents.Metadata.ATTACHMENTS] as AttachmentName[];
assert.strictEqual(attachments.length, 3);
const orderedNames = [...attachments];
orderedNames.sort((a, b) => a.name > b.name ? 1 : -1);
for (let i = 0; i < attachmentsInfo.length; i++) {
const { name, contentType } = orderedNames[i];
const attachment = attachments[i];
assert.strictEqual(name, attachment.name);
assert.strictEqual(contentType, attachment.contentType);
assert.strictEqual(typeof attachment.size, "number");
assert.ok(attachment.hash);
}
}
});
it("throws if stream is used twice", async () => {
const session = store.openSession();
const stream = Buffer.from([1, 2, 3]);
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
session.advanced.attachments.store(user, "profile", stream, "image/png");
session.advanced.attachments.store(user, "other", stream);
try {
await session.saveChanges();
assert.fail("it should have thrown");
} catch (err) {
assert.strictEqual(err.name, "InvalidOperationException");
}
});
it("throws when two attachments with the same name in session", async () => {
const session = store.openSession();
const stream = Buffer.from([1, 2, 3]);
const stream2 = Buffer.from([1, 2, 3, 4, 5]);
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
session.advanced.attachments.store(user, "profile", stream, "image/png");
try {
session.advanced.attachments.store(user, "profile", stream2);
assert.fail("it should have thrown");
} catch (err) {
assert.strictEqual(err.name, "InvalidOperationException");
}
});
it("should throw when deleting attachment after put document and attachment", async () => {
const session = store.openSession();
const stream = Buffer.from([1, 2, 3]);
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
session.advanced.attachments.store(user, "profile", stream, "image/png");
await session.delete(user);
try {
await session.saveChanges();
assert.fail("it should have thrown");
} catch (err) {
assert.strictEqual(err.name, "InvalidOperationException");
}
});
it("can get & delete attachments", async () => {
const stream1 = Buffer.from([1, 2, 3]);
const stream2 = Buffer.from([1, 2, 3, 4, 5, 6]);
const stream3 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const stream4 = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
session.advanced.attachments.store(user, "file1", stream1, "image/png");
session.advanced.attachments.store(user, "file2", stream2, "image/png");
session.advanced.attachments.store(user, "file3", stream3, "image/png");
session.advanced.attachments.store(user, "file4", stream4, "image/png");
await session.saveChanges();
}
{
const session = store.openSession();
const user = await session.load<User>("users/1");
// test get attachment by its name
{
const attachmentResult = await session.advanced.attachments.get("users/1", "file2");
assert.strictEqual(attachmentResult.details.name, "file2");
attachmentResult.dispose();
}
session.advanced.attachments.delete("users/1", "file2");
session.advanced.attachments.delete(user, "file4");
await session.saveChanges();
}
{
const session = store.openSession();
const user = await session.load<User>("users/1");
const metadata = session.advanced.getMetadataFor(user);
assert.strictEqual(metadata[CONSTANTS.Documents.Metadata.FLAGS], "HasAttachments");
const attachments = metadata[CONSTANTS.Documents.Metadata.ATTACHMENTS] as AttachmentName[];
assert.strictEqual(attachments.length, 2);
const result = await session.advanced.attachments.get("users/1", "file1");
assert.strictEqual(result.details.name, "file1");
assert.ok(result.details.hash);
assert.ok(result.details.changeVector);
assert.ok(result.details.size);
assert.ok(result.data);
assert.strictEqual(result.data.listenerCount("data"), 0);
assert.ok(result.data.readableLength);
assert.ok(result.data.isPaused());
// AttachmentResult data stream is paused until you resume() or pipe() it
// result.data.resume();
let bufResult = Buffer.from([]);
result.data.pipe(new stream.Writable({
write(chunk, enc, cb) {
bufResult = Buffer.concat([bufResult, chunk]);
cb();
}
}));
await StreamUtil.finishedAsync(result.data);
result.dispose();
assert.ok(Buffer.compare(bufResult, stream1) === 0);
}
});
it("can delete attachment using command", async () => {
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
const stream1 = Buffer.from([1, 2, 3]);
const stream2 = Buffer.from([1, 2, 3, 4, 5, 6]);
session.advanced.attachments.store(user, "file1", stream1, "image/png");
session.advanced.attachments.store(user, "file2", stream2, "image/png");
await session.saveChanges();
}
await store.operations.send(new DeleteAttachmentOperation("users/1", "file2"));
{
const session = store.openSession();
const user = await session.load<User>("users/1");
const metadata = session.advanced.getMetadataFor(user);
assert.ok(metadata["@flags"].includes("HasAttachments"));
const attachments = metadata["@attachments"];
assert.strictEqual(attachments.length, 1);
const result = await session.advanced.attachments.get("users/1", "file1");
try {
assert.strictEqual(result.data.readableLength, 3);
} finally {
result.dispose();
}
}
});
it("delete document and then its attachments. This is no op but should be supported.", async () => {
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
const stream = Buffer.from([1, 2, 3]);
session.advanced.attachments.store(user, "file", stream, "image/png");
await session.saveChanges();
}
{
const session = store.openSession();
const user = await session.load<User>("users/1");
await session.delete(user);
session.advanced.attachments.delete(user, "file");
session.advanced.attachments.delete(user, "file"); // this should be no-op
await session.saveChanges();
}
});
it("delete document by command and then its attachments. This is no op but should be supported.", async () => {
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
const stream = Buffer.from([1, 2, 3]);
session.advanced.attachments.store(user, "file", stream, "image/png");
await session.saveChanges();
}
{
const session = store.openSession();
session.advanced.defer(new DeleteCommandData("users/1", null));
session.advanced.attachments.delete("users/1", "file");
session.advanced.attachments.delete("users/1", "file");
await session.saveChanges();
}
});
it("get attachment names", async () => {
const names = ["profile.png"];
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
const stream = Buffer.from([1, 2, 3]);
session.advanced.attachments.store(user, names[0], stream, "image/png");
await session.saveChanges();
}
{
const session = store.openSession();
const user = await session.load<User>("users/1");
const attachments = session.advanced.attachments.getNames(user);
assert.strictEqual(attachments.length, 1);
const attachment = attachments[0];
assert.strictEqual(attachment.contentType, "image/png");
assert.strictEqual(attachment.name, names[0]);
assert.strictEqual(attachment.size, 3);
}
});
it("attachment exists", async () => {
{
const session = store.openSession();
const user = new User();
user.name = "Fitzchak";
await session.store(user, "users/1");
const attachmentStream = Buffer.from([1, 2, 3]);
session.advanced.attachments.store(user, "profile", attachmentStream, "image/png");
await session.saveChanges();
}
{
const session = store.openSession();
assert.ok(await session.advanced.attachments.exists("users/1", "profile"));
assert.ok(!(await session.advanced.attachments.exists("users/1", "background-photo")));
assert.ok(!(await session.advanced.attachments.exists("users/2", "profile")));
}
});
});