Skip to content

Instantly share code, notes, and snippets.

@fostergn
Created January 24, 2023 06:59
Show Gist options
  • Save fostergn/8b535bc13e4c629a0964c440634ef8cf to your computer and use it in GitHub Desktop.
Save fostergn/8b535bc13e4c629a0964c440634ef8cf to your computer and use it in GitHub Desktop.
const { deterministicPartitionKey } = require("./dpk");
// naive hash check
const isHash = (string) => string.length === 128;
const testEvent = {
partitionKey: 123,
}
describe("deterministicPartitionKey", () => {
it("Returns the literal '0' when given no input", () => {
const trivialKey = deterministicPartitionKey();
expect(trivialKey).toBe("0");
});
it(`Returns the literal '${testEvent.partitionKey}' when given the integer ${testEvent.partitionKey}`, () => {
const key = deterministicPartitionKey(testEvent);
expect(key).toBe(`${testEvent.partitionKey}`);
});
it(`Returns the literal '${testEvent.partitionKey}' when given the string '123'`, () => {
const stringTestEvent = {
partitionKey: '123'
}
const key = deterministicPartitionKey(stringTestEvent);
expect(key).toBe(stringTestEvent.partitionKey);
});
it(`Returns the literal '823' when given the string '823'`, () => {
const stringTestEvent = {
partitionKey: '823'
}
const key = deterministicPartitionKey(stringTestEvent);
expect(key).toBe(stringTestEvent.partitionKey);
});
it(`Returns hashedKey when given an event without a partition key`, () => {
const eventWithoutParitionKey = {}
const hashedKey = deterministicPartitionKey(eventWithoutParitionKey);
expect(isHash(hashedKey)).toBe(true);
});
it(`Returns hashedKey when given an event without a partition key`, () => {
const eventWithLongPartitionKey = {
partitionKey: 'adfads234a'.repeat(400)
}
const hashedKey = deterministicPartitionKey(eventWithLongPartitionKey);
expect(isHash(hashedKey)).toBe(true);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment