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

tests(idempotency): add utility to workspace unit tests and CI #1160

Merged
merged 13 commits into from
Nov 24, 2022

Conversation

dreamorosi
Copy link
Contributor

Description of your changes

As described in #1159, this PR makes it so that the existing unit tests for the Idempotency utility are now run as part of the PR checks as well as on merge & pre-push hooks.

Additionally, this PR introduces the following changes:

  • Minor styling changes to package structure and exports
  • Introduction of one additional type
  • Minor refactor of function that creates the idempotencyKey prefix for DynamoDB storage
  • Rename DynamoDbPersistenceLayer to DynamoDBPersistenceLayer (to follow product name)
  • Added missing unit test cases for implemented logic to reach 100% coverage
  • Temporarily excluded makeFunctionIdempotent from test coverage (to be reintroduced once implemented)

How to verify this change

Check the results of the automated checks below this PR.

Related issues, RFCs

Issue number: #1159

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding changes to the examples
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

Breaking change checklist

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@dreamorosi dreamorosi added this to the Idempotency - Beta release milestone Nov 11, 2022
@dreamorosi dreamorosi self-assigned this Nov 11, 2022
@dreamorosi dreamorosi requested a review from ijemmy November 11, 2022 19:17
@dreamorosi dreamorosi added the idempotency This item relates to the Idempotency Utility label Nov 11, 2022
@dreamorosi dreamorosi linked an issue Nov 13, 2022 that may be closed by this pull request
@pull-request-size pull-request-size bot added the size/XL PRs between 500-999 LOC, often PRs that grown with feedback label Nov 14, 2022
packages/idempotency/src/persistence/PersistenceLayer.ts Outdated Show resolved Hide resolved
Comment on lines +2 to +7
process.env._X_AMZN_TRACE_ID = '1-abcdef12-3456abcdef123456abcdef12';
process.env.AWS_LAMBDA_FUNCTION_NAME = 'my-lambda-function';
process.env.AWS_EXECUTION_ENV = 'nodejs16.x';
process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE = '128';
process.env.AWS_REGION = 'eu-west-1';
process.env._HANDLER = 'index.handler';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Question) Do we really need all of these env vars? Wouldn't only AWS_LAMBDA_FUNCTION_NAME be enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use only the name at the moment, and we'll be using also the region in the e2e tests.

I have added them mostly for consistency so that all the unit tests in the repo have the same environment. Mostly for cognitive load.

What do you think?

Comment on lines +44 to +89
describe('Method: constructor', () => {

test('when instantiated with minimum options it creates an instance with default values', () => {

// Prepare
const persistenceLayer = new TestDynamoDBPersistenceLayer({ tableName: dummyTableName });

// Act / Assess
expect(persistenceLayer).toEqual(expect.objectContaining({
tableName: dummyTableName,
keyAttr: 'id',
statusAttr: 'status',
expiryAttr: 'expiration',
inProgressExpiryAttr: 'in_progress_expiry_attr',
dataAttr: 'data'
}));

});

test('when instantiated with specific options it creates an instance with correct values', () => {

// Prepare
const testDynamoDBPersistenceLayerOptions: DynamoPersistenceConstructorOptions = {
tableName: dummyTableName,
keyAttr: dummyKey,
statusAttr: 'someStatusAttr',
expiryAttr: 'someExpiryAttr',
inProgressExpiryAttr: 'someInProgressExpiryAttr',
dataAttr: 'someDataAttr'
};
const persistenceLayer = new TestDynamoDBPersistenceLayer(testDynamoDBPersistenceLayerOptions);

// Act / Assess
expect(persistenceLayer).toEqual(expect.objectContaining({
tableName: dummyTableName,
keyAttr: dummyKey,
statusAttr: testDynamoDBPersistenceLayerOptions.statusAttr,
expiryAttr: testDynamoDBPersistenceLayerOptions.expiryAttr,
inProgressExpiryAttr: testDynamoDBPersistenceLayerOptions.inProgressExpiryAttr,
dataAttr: testDynamoDBPersistenceLayerOptions.dataAttr
}));

});

});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All member variables in this class are private. Should we break the abstraction here?

image

The private value are indirectly tested in the _putRecord() and other protected fields. Those test cases would detect an issue if the private values are incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point.

The reason why I introduced these tests is primarily to reach 100% coverage.

If we remove this test, coverage drops:
image

and the reason, I think, is because we have conditional statements in the constructor:

this.tableName = constructorOptions.tableName;
this.keyAttr = constructorOptions.keyAttr ?? 'id';
this.statusAttr = constructorOptions.statusAttr ?? 'status';
this.expiryAttr = constructorOptions.expiryAttr ?? 'expiration';
this.inProgressExpiryAttr = constructorOptions.inProgressExpiryAttr ?? 'in_progress_expiry_attr';
this.dataAttr = constructorOptions.dataAttr ?? 'data';

Since in all other test cases, including _putRecord we are always using the defaults, we never testing the left-side of the ?? (except for tableName).

Now, I could have an additional test to one of the other methods (i.e. _putRecord) and pass some values when initializing the class, however I saw that test as really testing the constructor, and so I added its own case.

If there's another way I'm happy to change it though, I have to admit I went this way also because this is how I've done it in Tracer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. I'll prefer to have additional _putRecord. But let's keep it as it is for now. I would like us to have unit test run in workflow early. We could refactor the tests afterward.

@dreamorosi dreamorosi requested a review from ijemmy November 17, 2022 16:35
ijemmy
ijemmy previously approved these changes Nov 18, 2022
Copy link
Contributor

@ijemmy ijemmy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be beneficial to have someone from who's working on Idempotency feature review this as it might affect them. I'll talk with them.

@dreamorosi
Copy link
Contributor Author

I will be merging this PR so that it can be included in this week's release.

In case of any comment please feel free to either leave one here or open an issue, we'll address any change in a new PR.

@dreamorosi dreamorosi merged commit 34ef180 into main Nov 24, 2022
@dreamorosi dreamorosi deleted the refactor/idempotency_tests branch November 24, 2022 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
idempotency This item relates to the Idempotency Utility size/XL PRs between 500-999 LOC, often PRs that grown with feedback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maintenance: integrate utility with CI/CD unit test processes
2 participants