Untitled

 avatar
unknown
plain_text
2 years ago
1.9 kB
7
Indexable
const { deterministicPartitionKey } = require('./your-refactored-file');

describe('deterministicPartitionKey', () => {
  const TRIVIAL_PARTITION_KEY = '0';
  const MAX_PARTITION_KEY_LENGTH = 256;

  test('returns TRIVIAL_PARTITION_KEY if event is falsy', () => {
    expect(deterministicPartitionKey(null)).toBe(TRIVIAL_PARTITION_KEY);
    expect(deterministicPartitionKey(undefined)).toBe(TRIVIAL_PARTITION_KEY);
    expect(deterministicPartitionKey(false)).toBe(TRIVIAL_PARTITION_KEY);
    expect(deterministicPartitionKey(0)).toBe(TRIVIAL_PARTITION_KEY);
    expect(deterministicPartitionKey('')).toBe(TRIVIAL_PARTITION_KEY);
    expect(deterministicPartitionKey([])).toBe(TRIVIAL_PARTITION_KEY);
    expect(deterministicPartitionKey({})).toBe(TRIVIAL_PARTITION_KEY);
  });

  test('returns partitionKey if provided in the event', () => {
    const event = { partitionKey: 'customKey' };
    expect(deterministicPartitionKey(event)).toBe('customKey');
  });

  test('computes hash for event if partitionKey is not provided', () => {
    const event = { data: 'example' };
    const hash = 'd2d6b42d64c3e8fb3e3d1d1ab3e1b3223a5f3fd1f4c10b2e3d8db6a17f17f8c6' +
      '3d1ef28e1485b5c1478a31d8a2cbe2d0823a10c4ed7a7cb7722eb82596cb5';
    expect(deterministicPartitionKey(event)).toBe(hash);
  });

  test('computes hash if candidate is not a string', () => {
    const event = { partitionKey: { key: 'value' } };
    const hash = '55a990d6febb7642826927db741f48c0d220775e4ce0a9fba5412d39ea00cb4e' +
      'f4478e719202f8be602d54b6350f4c4f3f3940e890ab42df29dd69f532bf1';
    expect(deterministicPartitionKey(event)).toBe(hash);
  });

  test('computes hash if candidate exceeds MAX_PARTITION_KEY_LENGTH', () => {
    const event = { partitionKey: 'a'.repeat(MAX_PARTITION_KEY_LENGTH + 1) };
    const hash = '3be7f2fc3c4ee3b733749a4a581cd74bb788305ddc51a32e597cb88
Editor is loading...