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

RFC: Testing Factories for AWS Data Objects #445

Open
simonireilly opened this issue Jan 8, 2022 · 5 comments
Open

RFC: Testing Factories for AWS Data Objects #445

simonireilly opened this issue Jan 8, 2022 · 5 comments
Labels
need-customer-feedback Requires more customers feedback before making or revisiting a decision on-hold This item is on-hold and will be revisited in the future RFC Technical design documents related to a feature request

Comments

@simonireilly
Copy link

simonireilly commented Jan 8, 2022

ref https://github.com/awslabs/aws-lambda-powertools-typescript/issues/26for strucutre of my RFC

Description of the proposal

There should be a consistent way to produce test fixtures for AWS lambda environments


Name of the core utility (and consequently the folders that contain the libraries):

  • packages/factories

Justification

When it comes to making effective use of AWS in unit and integration testing, developers will create data objects which have the interfaces of the 'aws-lambda' types package.

This is true, even within this project;

These untyped objects become a cause for churn, and often because a single source of truth is not present, they can misdiagnose behaviors through simple omission of fields.

Goals

  • Provide users of the nodejs lambda platform effective utilities for testing lambda handler
  • Provide consistent type-safe factories for AWS data structures and events
  • Support version of event formats to test utilities against different versions

Proposed API

There is an example here that utilizes faker and fishery to create type compliant factories that can be deep merged to allow for standard testing objects:

Installation:

yarn add --dev @aws-lambda-powertools/factories
npm i -d @aws-lambda-powertools/factories

Usage

Consider the following test that is written in #364

test('when called, it set the correct segment', () => {
  // Prepare
  const provider: ProviderService = new ProviderService();
  const record: SQSRecord = {
    messageId: 'fd95260b-1600-4028-b252-590cfcc9fe6d',
    receiptHandle: 'test',
    body: 'Information about current NY Times fiction bestseller for week of 12/11/2016.',
    attributes: {
      ApproximateReceiveCount: '1',
      AWSTraceHeader: 'Root=1-61cc1005-53ff3b575736e3c74eae6bfb;Parent=1f57c53badf96998;Sampled=1',
      SentTimestamp: '1640763398126',
      SenderId: 'AROAT26JIZQWSOCCOUNE5:sqsProducer',
      ApproximateFirstReceiveTimestamp: '1640763398127',
    },
    messageAttributes: {},
    md5OfBody: 'bbdc5fdb8be7251f5c910905db994bab',
    eventSource: 'aws:sqs',
    eventSourceARN:
      'arn:aws:sqs:eu-west-1:123456789012:queue',
    awsRegion: 'eu-west-1',
  };

  const context = ContextExamples.helloworldContext;

  // Act
  provider.continueSQSRecordTrace(record, context);

  // Assess
  expect(setSegment).toHaveBeenCalledTimes(1);
});

This test becomes:

import factories from '@aws-lambda-powertools/factories';


test('when called, it set the correct segment', () => {
  // Prepare
  const provider: ProviderService = new ProviderService();
  const record = factories.sqs.record.build();
  const context = factories.sqs.context.build();

  // Act
  provider.continueSQSRecordTrace(record, context);

  // Assess
  expect(setSegment).toHaveBeenCalledTimes(1);
});
@michaelbrewer
Copy link
Contributor

@heitorlessa it is worth thinking about this across both Python, Java and Typescript (aws-powertools/powertools-lambda-python#1169)

@saragerion
Copy link
Contributor

Thanks for opening this issue @simonireilly. As a former AWS customer, I would like to say that this is something I wanted myself as well, back then.
The team and I will get back to you on this one.

@saragerion saragerion added all triage This item has not been triaged by a maintainer, please wait labels Jan 10, 2022
@willfarrell
Copy link

I've been using @serverless/event-mocks for this, there are a few others, all are out of date and not maintained.

@michaelbrewer
Copy link
Contributor

For Python is have a lot of mocks and sample events. So whichever solution we have in mind it would be great to has the potential for reuse across python, typescript, Java and .Net.

@dreamorosi dreamorosi added on-hold This item is on-hold and will be revisited in the future RFC Technical design documents related to a feature request and removed triage This item has not been triaged by a maintainer, please wait labels Feb 28, 2022
@dreamorosi dreamorosi added the need-customer-feedback Requires more customers feedback before making or revisiting a decision label Nov 13, 2022
@driimus
Copy link

driimus commented May 14, 2024

If any ESM users are looking for an utility similar to what's being proposed here:

I've been shipping my own set of test data factories for Lambda events, built using the same combo (fishery + @faker-js/faker): https://www.npmjs.com/package/@driimus/aws-event-factory,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need-customer-feedback Requires more customers feedback before making or revisiting a decision on-hold This item is on-hold and will be revisited in the future RFC Technical design documents related to a feature request
Projects
Development

No branches or pull requests

6 participants