Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(idempotency): add support for custom key prefix #3532

Merged
Prev Previous commit
Next Next commit
test(idempotency): add keyPrefix test for decorator
shdq committed Feb 1, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 4479fa6afd3e1e013a85498cf7ed1b1f0eb0fd07
43 changes: 41 additions & 2 deletions packages/idempotency/tests/unit/idempotencyDecorator.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
import context from '@aws-lambda-powertools/testing-utils/context';
import type { Context } from 'aws-lambda';
import { describe, expect, it } from 'vitest';
import { idempotent } from '../../src/index.js';
import { describe, expect, it, vi } from 'vitest';
import { idempotent, IdempotencyConfig } from '../../src/index.js';
import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils.js';
import { BasePersistenceLayer } from '../../src/persistence/BasePersistenceLayer.js';

describe('Given a class with a function to decorate', () => {
it('maintains the scope of the decorated function', async () => {
@@ -35,4 +36,42 @@ describe('Given a class with a function to decorate', () => {
// Assess
expect(result).toBe('private foo');
});

it('configure persistenceStore idempotency key with custom keyPrefix', async () => {
// Prepare
const configureSpy = vi.spyOn(BasePersistenceLayer.prototype, 'configure');
const idempotencyConfig = new IdempotencyConfig({});

class TestClass implements LambdaInterface {
@idempotent({
persistenceStore: new PersistenceLayerTestClass(),
config: idempotencyConfig,
keyPrefix: 'my-custom-prefix',
})
public async handler(
_event: unknown,
_context: Context
): Promise<boolean> {
return true;
}
}

const handlerClass = new TestClass();
const handler = handlerClass.handler.bind(handlerClass);

// Act
const result = await handler({}, context);


// Assert
expect(result).toBeTruthy();

expect(configureSpy).toHaveBeenCalled();
const configureCallArgs = configureSpy.mock.calls[0][0]; // Extract first call's arguments
expect(configureCallArgs.config).toBe(idempotencyConfig);
expect(configureCallArgs.keyPrefix).toBe('my-custom-prefix');

// Restore the spy
configureSpy.mockRestore();
});
});