-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartitionKey.test.js
62 lines (48 loc) · 1.67 KB
/
partitionKey.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { extractPartitionKeyFromEvent } = require("./partitionKey");
describe("extractPartitionKeyFromEvent", () => {
it("Returns the literal '0' when given no input", () => {
// given, when
const trivialKey = extractPartitionKeyFromEvent();
// then
expect(trivialKey).toBe("0");
});
it("Returns the event partition key when it is present", () => {
// given
const event = { partitionKey: 10 };
// when
const trivialKey = extractPartitionKeyFromEvent(event);
// then
expect(trivialKey).toBe("10");
});
it.each([
["Int", 10, "10"],
["Float", 5.5, "5.5"],
["String", "Some text", "Some text"],
["Date", new Date(2022,8,11,23,0,0,0), "\"2022-09-11T21:00:00.000Z\""],
["Array", [0, "A", 2.5, {}], "[0,\"A\",2.5,{}]"],
["Object", {}, "{}"]
])("Correctly returns the event partition key when type is %s", (_, data, expectedData) => {
// given
const event = { partitionKey: data };
// when
const trivialKey = extractPartitionKeyFromEvent(event);
// then
expect(trivialKey).toBe(expectedData);
});
it("Returns a hashed value when event partition key length is bigger than 256 caharacters", () => {
// given
const trivialKey = extractPartitionKeyFromEvent({ partitionKey: 'z'.repeat(300) });
// when
expect(trivialKey.length).toBe(128);
// then
expect(trivialKey).not.toBe('z'.repeat(128));
});
it("Returns a hashed value when event has no partition key", () => {
// given
const event = { type: 'shift_accepted' };
// when
const trivialKey = extractPartitionKeyFromEvent(event);
// then
expect(trivialKey).not.toBe('{"type":"shift_accepted"}');
});
});