-
Notifications
You must be signed in to change notification settings - Fork 33
/
BulkInsertsTest.ts
308 lines (255 loc) · 10.8 KB
/
BulkInsertsTest.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
import * as assert from "assert";
import { testContext, disposeTestDocumentStore } from "../../Utils/TestUtil";
import DocumentStore, {
IDocumentStore, BulkInsertOperation, IMetadataDictionary,
} from "../../../src";
import { createMetadataDictionary } from "../../../src/Mapping/MetadataAsDictionary";
import { CONSTANTS } from "../../../src/Constants";
import * as BluebirdPromise from "bluebird";
import { DateUtil } from "../../../src/Utility/DateUtil";
describe("bulk insert", function () {
let store: IDocumentStore;
beforeEach(async function () {
store = await testContext.getDocumentStore();
});
afterEach(async () =>
await disposeTestDocumentStore(store));
it("simple bulk insert should work", async () => {
const fooBar1 = new FooBar();
fooBar1.name = "John Doe";
const fooBar2 = new FooBar();
fooBar2.name = "Jane Doe";
const fooBar3 = new FooBar();
fooBar3.name = "Mega John";
const fooBar4 = new FooBar();
fooBar4.name = "Mega Jane";
const bulkInsert = store.bulkInsert();
await bulkInsert.store(fooBar1);
await bulkInsert.store(fooBar2);
await bulkInsert.store(fooBar3);
await bulkInsert.store(fooBar4);
await bulkInsert.finish();
const session = store.openSession();
try {
const doc1 = await session.load<FooBar>("FooBars/1-A");
const doc2 = await session.load<FooBar>("FooBars/2-A");
const doc3 = await session.load<FooBar>("FooBars/3-A");
const doc4 = await session.load<FooBar>("FooBars/4-A");
assert.ok(doc1);
assert.ok(doc2);
assert.ok(doc3);
assert.ok(doc4);
assert.strictEqual(doc1.name, "John Doe");
assert.strictEqual(doc2.name, "Jane Doe");
assert.strictEqual(doc3.name, "Mega John");
assert.strictEqual(doc4.name, "Mega Jane");
const docsInsertedCount = await session
.query({ collection: "fooBars" })
.count();
assert.strictEqual(docsInsertedCount, 4);
} finally {
session.dispose();
}
});
it("can be killed early before making connection", async () => {
try {
const bulkInsert = store.bulkInsert();
await bulkInsert.store(new FooBar());
await bulkInsert.abort();
await bulkInsert.store(new FooBar());
assert.fail("Should have thrown.");
} catch (error) {
assert.strictEqual(error.name, "BulkInsertAbortedException", error.message);
const bulkInsertCanceled = /TaskCanceledException/i.test(error.message);
const bulkInsertNotRegisteredYet =
/Unable to kill bulk insert operation, because it was not found on the server./i.test(error.message);
const bulkInsertSuccessfullyKilled =
/Bulk insert was aborted by the user./i.test(error.message);
// this one's racy, so it's one or the other
assert.ok(
bulkInsertCanceled
|| bulkInsertNotRegisteredYet
|| bulkInsertSuccessfullyKilled,
"Unexpected error message:" + error.message);
}
});
it("can be aborted after a while", async () => {
try {
const bulkInsert = store.bulkInsert();
await bulkInsert.store(new FooBar());
await bulkInsert.store(new FooBar());
await bulkInsert.store(new FooBar());
await bulkInsert.store(new FooBar());
await BluebirdPromise.delay(500);
await bulkInsert.abort();
await bulkInsert.store(new FooBar());
assert.fail("Should have thrown.");
} catch (error) {
assert.strictEqual(error.name, "BulkInsertAbortedException", error.message);
const bulkInsertCanceled = /TaskCanceledException/i.test(error.message);
const bulkInsertNotRegisteredYet =
/Unable to kill bulk insert operation, because it was not found on the server./i.test(error.message);
const bulkInsertSuccessfullyKilled =
/Bulk insert was aborted by the user./i.test(error.message);
// this one's racy, so it's one or the other
assert.ok(
bulkInsertCanceled
|| bulkInsertNotRegisteredYet
|| bulkInsertSuccessfullyKilled,
"Unexpected error message:" + error.message);
}
});
it("should not accept ids ending with pipeline", async () => {
try {
const bulkInsert = store.bulkInsert();
await bulkInsert.store(new FooBar(), "foobars|");
assert.fail("Should have thrown.");
} catch (error) {
assert.strictEqual(error.name, "NotSupportedException");
assert.strictEqual(error.message, "Document ids cannot end with '|', but was called with foobars|");
}
});
it("can modify metadata with bulk insert", async () => {
const date = DateUtil.default.stringify(new Date());
const fooBar = new FooBar();
fooBar.name = "John Snow";
const metadata = createMetadataDictionary({
raw: {}
});
metadata[CONSTANTS.Documents.Metadata.EXPIRES] = date;
const bulkInsert = store.bulkInsert();
await bulkInsert.store(fooBar, metadata);
await bulkInsert.finish();
const session = store.openSession();
try {
const entity = await session.load<FooBar>("FooBars/1-A");
const metadataExpirationDate
= session.advanced.getMetadataFor(entity)[CONSTANTS.Documents.Metadata.EXPIRES];
assert.strictEqual(date, metadataExpirationDate);
} finally {
session.dispose();
}
});
it("can handle nested types properly", async () => {
class BulkTestItem {
public name: string;
public created: Date;
public constructor(name: string) {
this.name = name;
this.created = new Date();
}
}
class BulkTestItemCollection {
public id: string;
public items: BulkTestItem[];
public constructor(...names: string[]) {
this.items = names.map(name => {
return new BulkTestItem(name);
});
}
}
store.conventions.registerEntityType(BulkTestItem);
store.conventions.registerEntityType(BulkTestItemCollection);
const entity = new BulkTestItemCollection("jon", "dany", "imp");
{
const bulk = store.bulkInsert();
await bulk.store(entity);
await bulk.finish();
}
{
const session = store.openSession();
const loaded = await session.load(entity["id"]);
assert.ok(loaded);
const metadata = loaded["@metadata"];
assert.ok(metadata["@id"], entity["id"]);
const nestedObjectTypes = metadata[CONSTANTS.Documents.Metadata.NESTED_OBJECT_TYPES];
assert.ok(nestedObjectTypes);
assert.strictEqual(Object.keys(nestedObjectTypes).length, 2);
assert.strictEqual(nestedObjectTypes["items[]"], BulkTestItem.name);
assert.strictEqual(nestedObjectTypes["items[].created"], "date");
}
});
it.skip("[RDBC-226] can insert object literals with default conventions", async () => {
const bulk = store.bulkInsert();
const obj = { id: null, name: "blabli" };
await bulk.store(obj);
await bulk.finish();
assert.ok(obj["id"]);
});
it("can handle custom entity naming conventions + object literals when findCollectionNameForObjectLiteral is specified", async () => {
const store2 = new DocumentStore(store.urls, store.database);
store2.conventions.entityFieldNameConvention = "camel";
store2.conventions.remoteEntityFieldNameConvention = "pascal";
store2.conventions.findCollectionNameForObjectLiteral = () => "test";
store2.initialize();
const registeredAt = new Date();
const camelCasedObj = {
id: null,
name: "Jon",
job: "white walker killer",
fathersName: "Rhaegar",
canUseSword: true,
equipment: ["sword", "bow", "direwolf"],
registeredAt
};
try {
const bulk = store2.bulkInsert();
await bulk.store(camelCasedObj);
await bulk.finish();
} finally {
store2.dispose();
}
{
// use case transformless store to verify doc
const session = store.openSession();
const loaded = await session.load(camelCasedObj["id"]);
assert.ok(loaded);
assert.ok("Name" in loaded);
assert.strictEqual(loaded["Name"], camelCasedObj.name);
assert.ok("Job" in loaded);
assert.ok("CanUseSword" in loaded);
assert.ok("Equipment" in loaded);
assert.ok("RegisteredAt" in loaded);
assert.ok("FathersName" in loaded);
assert.strictEqual(loaded["Equipment"].length, 3);
assert.ok("Raven-Node-Type" in loaded["@metadata"]);
assert.ok("@nested-object-types" in loaded["@metadata"]);
assert.ok("@collection" in loaded["@metadata"]);
}
});
});
describe("BulkInsertOperation._typeCheckStoreArgs() properly parses arguments", () => {
const typeCheckStoreArgs = BulkInsertOperation["_typeCheckStoreArgs"];
// eslint-disable-next-line @typescript-eslint/no-empty-function
const expectedCallback = () => {
};
const expectedId = "id";
const expectedMetadata = {} as IMetadataDictionary;
const expectedNullId = null;
it("accepts id", () => {
const { id, getId } = typeCheckStoreArgs(expectedId);
assert.strictEqual(id, expectedId);
assert.ok(!getId);
});
it("accepts metadata", () => {
const { id, getId, metadata } = typeCheckStoreArgs(expectedMetadata);
assert.strictEqual(metadata, expectedMetadata);
assert.ok(!id);
assert.ok(getId);
});
it("accepts id, metadata", () => {
const { id, getId, metadata } = typeCheckStoreArgs(expectedId, expectedMetadata);
assert.strictEqual(metadata, expectedMetadata);
assert.strictEqual(id, expectedId);
assert.ok(!getId);
});
it("accepts null id, metadata returns getId true", () => {
const { id, getId, metadata } = typeCheckStoreArgs(expectedNullId, expectedMetadata);
assert.strictEqual(metadata, expectedMetadata);
assert.ok(!id);
assert.ok(getId);
});
});
export class FooBar {
public name: string;
}