-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpk.test.js
executable file
·41 lines (31 loc) · 1.39 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
const { deterministicPartitionKey } = require("./dpk");
const crypto = require('crypto');
describe('deterministicPartitionKey', () => {
it("Returns the literal '0' when given no input", () => {
const trivialKey = deterministicPartitionKey();
expect(trivialKey).toBe("0");
});
it('Returns event partition key when it exists', () => {
const event = { partitionKey: 'key' };
const result = deterministicPartitionKey(event);
expect(result).toBe('key');
});
it('Calculates partition key using hash function when no partition key is provided', () => {
const event = { name: 'lucy', age: 20 };
const data = JSON.stringify(event);
const hash = crypto.createHash('sha3-512').update(data).digest('hex');
const result = deterministicPartitionKey(event);
expect(result).toBe(hash);
});
it('Stringifies partitionKey when it is not a string', () => {
const event = { partitionKey: { name: 'lucy', age: 20 } };
const data = JSON.stringify(event.partitionKey);
const result = deterministicPartitionKey(event);
expect(result).toBe(data);
});
it("Creates a hash of the candidate if it's longer than the maximum partition key length", () => {
const longString = "a".repeat(256 + 1);
const hash = crypto.createHash("sha3-512").update(longString).digest("hex");
expect(deterministicPartitionKey({ partitionKey: longString })).toBe(hash);
});
});