-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpk.test.js
executable file
·47 lines (40 loc) · 1.54 KB
/
dpk.test.js
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
const crypto = require("crypto");
const { deterministicPartitionKey } = require("./dpk");
describe("deterministicPartitionKey", () => {
it("Returns the literal '0' when given no input", () => {
const trivialKey = deterministicPartitionKey();
expect(trivialKey).toBe("0");
});
it("Returns '0' when event paritionKey is string '0'", () => {
const event = { partitionKey: '0' };
const trivialKey = deterministicPartitionKey(event);
expect(trivialKey).toBe("0");
});
it("Returns '0' when event paritionKey is number 0", () => {
const event = { partitionKey: 0 };
const trivialKey = deterministicPartitionKey(event);
expect(trivialKey).toBe("0");
});
it("Returns '1' when event paritionKey is number 1", () => {
const event = { partitionKey: 1 };
const trivialKey = deterministicPartitionKey(event);
expect(trivialKey).toBe("1");
});
it("Returns sha3 hash of event partitionKey if its length exceeds 256", () => {
const longString = new Array(266).fill(0).toString();
const event = {
partitionKey: longString
};
const hash = crypto.createHash("sha3-512").update(longString).digest("hex")
const trivialKey = deterministicPartitionKey(event);
expect(trivialKey).toBe(hash);
});
it("Returns sha3 hash of event if partitionKey not present", () => {
const event = {};
const hash = crypto.createHash("sha3-512").update(
JSON.stringify(event)
).digest("hex")
const trivialKey = deterministicPartitionKey(event);
expect(trivialKey).toBe(hash);
});
});