forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tableTransaction.spec.ts
89 lines (81 loc) · 3.02 KB
/
tableTransaction.spec.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import {
HttpClient,
PipelineResponse,
createHttpHeaders,
createPipelineRequest,
} from "@azure/core-rest-pipeline";
import { TableTransaction, parseTransactionResponse } from "../../src/TableTransaction";
import { TableClient } from "../../src/TableClient";
import { assert } from "chai";
describe("TableTransaction", () => {
describe("parseTransactionResponse", () => {
it("should handle error with no error info", () => {
const testResponse: PipelineResponse = {
headers: createHttpHeaders(),
request: createPipelineRequest({ url: "https://example.org" }),
status: 400,
};
try {
parseTransactionResponse(testResponse);
assert.fail("Expected error");
} catch (error: any) {
assert.equal(error.message, "Transaction Failed");
}
});
it("should handle error with odata error info", () => {
const testResponse: PipelineResponse = {
headers: createHttpHeaders(),
request: createPipelineRequest({ url: "https://example.org" }),
status: 400,
bodyAsText: JSON.stringify({
"odata.error": {
code: "123",
message: { value: "Test message" },
},
}),
};
try {
parseTransactionResponse(testResponse);
assert.fail("Expected error");
} catch (error: any) {
assert.equal(error.message, "Test message");
assert.equal(error.code, "123");
}
});
it("should honor the custom httpClient passed to the TableClient", async () => {
let isProxy = false;
const proxyHttpClient: HttpClient = {
sendRequest: async (request) => {
isProxy = true;
return { status: 200, headers: createHttpHeaders(), request };
},
};
const client = new TableClient("https://example.org", "TestTable", {
httpClient: proxyHttpClient,
});
const transaction = new TableTransaction();
transaction.createEntity({ partitionKey: "helper", rowKey: "1", value: "t1" });
transaction.createEntity({ partitionKey: "helper", rowKey: "2", value: "t2" });
await client.submitTransaction(transaction.actions);
assert.isTrue(isProxy);
});
});
describe("updateEntity", () => {
it("should have ergonomic overloads", () => {
const transaction = new TableTransaction();
const entity = { partitionKey: "1", rowKey: "1" };
transaction.updateEntity(entity);
transaction.updateEntity(entity, "Replace");
transaction.updateEntity(entity, { etag: "" });
transaction.updateEntity(entity, "Merge", { etag: "" });
assert.deepEqual(transaction.actions, [
["update", { partitionKey: "1", rowKey: "1" }, "Merge", {}],
["update", { partitionKey: "1", rowKey: "1" }, "Replace", {}],
["update", { partitionKey: "1", rowKey: "1" }, "Merge", { etag: "" }],
["update", { partitionKey: "1", rowKey: "1" }, "Merge", { etag: "" }],
]);
});
});
});