-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathtable.batch.errorhandling.test.ts
649 lines (566 loc) · 20.1 KB
/
table.batch.errorhandling.test.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
// Tests in this file are using @azure/data-tables
import * as assert from "assert";
import { TableClient, TableTransaction } from "@azure/data-tables";
import { AzureNamedKeyCredential } from "@azure/core-auth";
import { configLogger } from "../../../src/common/Logger";
import TableServer from "../../../src/table/TableServer";
import {
EMULATOR_ACCOUNT_KEY,
EMULATOR_ACCOUNT_NAME,
getUniqueName
} from "../../testutils";
import {
AzureDataTablesTestEntityFactory,
TableTestEntity
} from "../models/AzureDataTablesTestEntityFactory";
import {
createAzureDataTablesClient,
createTableServerForTestHttps,
createUniquePartitionKey,
HOST,
PORT
} from "../utils/table.entity.test.utils";
import { RestError } from "@azure/core-http";
// Set true to enable debug log
configLogger(false);
// For convenience, we have a switch to control the use
// of a local Azurite instance, otherwise we need an
// ENV VAR added to mocha
// script or launch.json containing
// Azure Storage Connection String (using SAS or Key).
const testLocalAzuriteInstance = true;
const entityFactory = new AzureDataTablesTestEntityFactory();
describe("table Entity APIs test", () => {
let server: TableServer;
const requestOverride = { headers: {} };
before(async () => {
server = createTableServerForTestHttps();
await server.start();
requestOverride.headers = {
Prefer: "return-content",
accept: "application/json;odata=fullmetadata"
};
});
after(async () => {
await server.close();
});
it("01. Batch API should serialize errors according to group transaction spec, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const sharedKeyCredential = new AzureNamedKeyCredential(
EMULATOR_ACCOUNT_NAME,
EMULATOR_ACCOUNT_KEY
);
const badTableClient = new TableClient(
`https://${HOST}:${PORT}/${EMULATOR_ACCOUNT_NAME}`,
"noExistingTable",
sharedKeyCredential
);
// await badTableClient.create(); // deliberately do not create table
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
await badTableClient.submitTransaction(transaction.actions);
} catch (err: any) {
assert.strictEqual(err.statusCode, 404);
assert.strictEqual(err.code, "TableNotFound");
}
});
it("02. Batch API should reject request with more than 100 transactions, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableName100: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [];
const TOO_MANY_REQUESTS = 101;
while (testEntities.length < TOO_MANY_REQUESTS) {
testEntities.push(entityFactory.createBasicEntityForTest(partitionKey));
}
const tooManyRequestsClient = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableName100
);
await tooManyRequestsClient.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
await tooManyRequestsClient.submitTransaction(transaction.actions);
} catch (err: any) {
assert.strictEqual(err.statusCode, 400);
assert.strictEqual(err.code, "InvalidInput");
}
});
it("03. Batch API should rollback insert Entity transactions, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameBatchError: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameBatchError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
202,
`Should not have got status code ${restErr.statusCode} on first transaction.`
);
}
testEntities[1].myValue = "ShouldNotHaveChanged";
const transaction2 = new TableTransaction();
for (const testEntity of testEntities) {
transaction2.createEntity(testEntity);
}
try {
const result2 = await tableClientrollback.submitTransaction(
transaction2.actions
);
assert.ok(result2.subResponses[0].rowKey);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
409,
"Did not get expected 409 (EntityAlreadyExists) error."
);
}
try {
const shouldNotExist =
await tableClientrollback.getEntity<TableTestEntity>(
testEntities[1].partitionKey,
testEntities[1].rowKey
);
assert.strictEqual(
shouldNotExist.myValue,
"value1",
"We should not have changed the value!"
);
} catch (err: any) {
const restErr2 = err as RestError;
assert.strictEqual(
restErr2.statusCode,
202,
"We did not expect the entity to have been changed."
);
}
await tableClientrollback.deleteTable();
});
it("04. Batch API should rollback delete Entity transactions, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameDeleteError: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameDeleteError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
assert.ifError(err); // should not have an error here
}
const transactionDelete = new TableTransaction();
transactionDelete.deleteEntity(
testEntities[0].partitionKey,
testEntities[0].rowKey
);
transactionDelete.deleteEntity(
testEntities[1].partitionKey,
testEntities[1].rowKey
);
transactionDelete.createEntity(testEntities[2]);
try {
const resultDelete = await tableClientrollback.submitTransaction(
transactionDelete.actions
);
assert.strictEqual(resultDelete.status, 202);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
409,
"Did not get expected entity already exists error."
);
}
try {
const shouldExist = await tableClientrollback.getEntity<TableTestEntity>(
testEntities[0].partitionKey,
testEntities[0].rowKey
);
assert.notStrictEqual(shouldExist, null, "We have an entity.");
} catch (err: any) {
const restErr2 = err as RestError;
assert.strictEqual(
restErr2.statusCode,
404,
"We expected an entity not found error."
);
}
await tableClientrollback.deleteTable();
});
it("05. Batch API should rollback update Entity transactions, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameDeleteError: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameDeleteError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
assert.ifError(err); // should not have an error here
}
const transactionUpdateThenError = new TableTransaction();
testEntities[0].myValue = "a new value";
testEntities[1].myValue = "a new value";
transactionUpdateThenError.updateEntity(testEntities[0]);
transactionUpdateThenError.updateEntity(testEntities[1]);
transactionUpdateThenError.createEntity(testEntities[2]);
try {
const resultDelete = await tableClientrollback.submitTransaction(
transactionUpdateThenError.actions
);
assert.strictEqual(resultDelete.status, 202);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
409,
"Did not get expected entity already exists error."
);
}
try {
const shouldExist = await tableClientrollback.getEntity<TableTestEntity>(
testEntities[0].partitionKey,
testEntities[0].rowKey
);
assert.notStrictEqual(shouldExist, null, "We have an entity.");
assert.notStrictEqual(
shouldExist.myValue,
"a new value",
"Update entity action was not rolled back!"
);
} catch (err: any) {
const restErr2 = err as RestError;
assert.strictEqual(
restErr2.statusCode,
404,
"We expected an entity not found error."
);
}
await tableClientrollback.deleteTable();
});
it("06. Batch API should rollback upsert Entity transactions, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameDeleteError: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameDeleteError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
assert.ifError(err); // should not have an error here
}
const transactionUpdateThenError = new TableTransaction();
testEntities[0].myValue = "a new value";
testEntities[1].myValue = "a new value";
const newUpsertEntity =
entityFactory.createBasicEntityForTest(partitionKey);
newUpsertEntity.myValue = "ephemeral";
transactionUpdateThenError.upsertEntity(testEntities[0]);
transactionUpdateThenError.upsertEntity(testEntities[1]);
transactionUpdateThenError.upsertEntity(newUpsertEntity);
transactionUpdateThenError.createEntity(testEntities[2]);
try {
const resultDelete = await tableClientrollback.submitTransaction(
transactionUpdateThenError.actions
);
assert.strictEqual(resultDelete.status, 202);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
409,
"Did not get expected entity already exists error."
);
}
try {
const shouldExist = await tableClientrollback.getEntity<TableTestEntity>(
testEntities[0].partitionKey,
testEntities[0].rowKey
);
assert.notStrictEqual(shouldExist, null, "We have an entity.");
assert.notStrictEqual(
shouldExist.myValue,
"a new value",
"Update entity action was not rolled back!"
);
} catch (err: any) {
const restErr2 = err as RestError;
assert.strictEqual(
restErr2.statusCode,
404,
"We expected an entity not found error."
);
}
try {
const shouldNotExist =
await tableClientrollback.getEntity<TableTestEntity>(
newUpsertEntity.partitionKey,
newUpsertEntity.rowKey
);
assert.strictEqual(shouldNotExist, null, "We should not have an entity.");
} catch (err: any) {
const restErr2 = err as RestError;
assert.strictEqual(
restErr2.statusCode,
404,
"We expected an entity not found error."
);
}
await tableClientrollback.deleteTable();
});
it("07. Batch API should return valid batch failure index for Azure.Data.Tables, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameDeleteError: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameDeleteError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
transaction.deleteEntity(partitionKey, getUniqueName("ThisShouldNotExist"));
let errorCaught = false;
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
errorCaught = true;
assert.strictEqual(
err.message[0],
"3",
"We did not get the expected entity ID in the error response"
);
}
assert.strictEqual(
errorCaught,
true,
"Did not catch the expected error, test should not pass!"
);
await tableClientrollback.deleteTable();
});
it("08. Batch API Etag should be rolled back after transaction failure on update, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameDeleteError: string = getUniqueName("datatables");
const singleTestEntity =
entityFactory.createBasicEntityForTest(partitionKey);
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameDeleteError
);
await tableClientrollback.createTable();
const singleEntityResult = await tableClientrollback.createEntity(
singleTestEntity
);
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey)
];
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
assert.ifError(err); // should not have an error here
}
const transactionUpdateThenError = new TableTransaction();
testEntities[0] = singleTestEntity;
testEntities[0].myValue = "a new value";
testEntities[1].myValue = "a new value";
transactionUpdateThenError.updateEntity(testEntities[0]);
transactionUpdateThenError.updateEntity(testEntities[1]);
transactionUpdateThenError.createEntity(testEntities[2]);
try {
const resultDelete = await tableClientrollback.submitTransaction(
transactionUpdateThenError.actions
);
assert.strictEqual(resultDelete.status, 202);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
409,
"Did not get expected entity already exists error."
);
}
try {
const shouldExist = await tableClientrollback.getEntity<TableTestEntity>(
testEntities[0].partitionKey,
testEntities[0].rowKey
);
assert.notStrictEqual(shouldExist, null, "We have an entity.");
assert.notStrictEqual(
shouldExist.myValue,
"a new value",
"Update entity action was not rolled back!"
);
assert.strictEqual(
shouldExist.etag,
singleEntityResult.etag,
"Update entity did not roll back eTag!"
);
} catch (err: any) {
const restErr2 = err as RestError;
assert.strictEqual(
restErr2.statusCode,
404,
`We expected an entity not found error, but got \"${restErr2.message}\"`
);
}
await tableClientrollback.deleteTable();
});
it("09. Batch API should fail to insert duplicate Entity with correct 400 Status and InvalidDuplicateRow error, @loki", async () => {
const partitionKey = createUniquePartitionKey("");
const tableNameBatchError: string = getUniqueName("datatables");
const myDupTestEntity = entityFactory.createBasicEntityForTest(partitionKey);
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
myDupTestEntity,
myDupTestEntity
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameBatchError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
const result = await tableClientrollback.submitTransaction(
transaction.actions
);
assert.ok(result.subResponses[0].rowKey);
} catch (err: any) {
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
400,
"Did not get expected 409 (InvalidDuplicateRow) error."
);
}
await tableClientrollback.deleteTable();
});
//Skips this test because it requires modifying the Azure Data Tables client so that it does not perform a check for partition key consistency before submitting the batch.
it.skip("10. Batch API should fail when attempting to insert elements with different partitionkeys in same batch, @loki", async () => {
const partitionKey = createUniquePartitionKey("1");
const partitionKey2 = createUniquePartitionKey("2");
console.log(`PartitionKey: ${partitionKey} partitionKey2: ${partitionKey2}`);
const tableNameBatchError: string = getUniqueName("datatables");
const testEntities: TableTestEntity[] = [
entityFactory.createBasicEntityForTest(partitionKey),
entityFactory.createBasicEntityForTest(partitionKey2)
];
const tableClientrollback = createAzureDataTablesClient(
testLocalAzuriteInstance,
tableNameBatchError
);
await tableClientrollback.createTable();
const transaction = new TableTransaction();
for (const testEntity of testEntities) {
transaction.createEntity(testEntity);
}
try {
await tableClientrollback.submitTransaction(
transaction.actions
);
assert.fail("Did not get expected 400 (InvalidInput) error.");
} catch (err: any) {
if (err.code === "ERR_ASSERTION") {
throw err;
}
const restErr = err as RestError;
assert.strictEqual(
restErr.statusCode,
400,
"Did not get expected 400 (InvalidInput) error."
);
}
await tableClientrollback.deleteTable();
});
});