Skip to content

Update lambda test tool tests to use snapshots #2045

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

Merged
merged 9 commits into from
Apr 21, 2025
Merged

Conversation

GarrettBeatty
Copy link
Contributor

@GarrettBeatty GarrettBeatty commented Apr 15, 2025

Issue #, if available:

Description of changes:
We previously had integration tests for lambda test tool which did the following:

  1. Call an extension method (toApiGatewayRequest/toHttpResponse,etc)
  2. Deploy and Invoke a real api gateway and compare the actual api gateway request/response with the expected request/response

This process was overkill because the real api gateway request and response will not change. We can instead use "snapshots" (static files) to validate that our extension methods produce the correct request and response. This will reduce the amount of time it takes for our tests to run too.

The tests now take 5 seconds to run instead of 15 minutes after this change.

Code Changes
The actual changes/steps that I have done in this PR are:

  1. I created snapshots automatically by running the existing tests, but then additionally calling a saveSnapshot method that i have created. This produced the snapshot json files you see.
  2. I then modified the tests to read these snapshot files instead of invoking the real api gateway
  3. I then deleted all of the code responsible for creating api gateways, cloudformation stacks, etc.
  4. After I did this, I realized that our integration tests and unit tests for extension methods are essentially identical because they are testing the same thing (validating the extension method works by comparing it to some static value). Because of this I moved the "integration" tests for these functions into the unit test project, and replaced the existing unit tests with them (because they are now duplicative).
  5. I also removed the additional Assertions in the ApiGatewayResponseTestCases class because these assertions are duplicate/not needed. The reason that they are not needed is because they are checking things that are already checked via reading the snapshot file.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@GarrettBeatty GarrettBeatty changed the title Gcbeatty/snapshots Update lambda test tool tests to use snapshots Apr 15, 2025
@GarrettBeatty GarrettBeatty added the Release Not Needed Add this label if a PR does not need to be released. label Apr 15, 2025

namespace Amazon.Lambda.TestTool.UnitTests.SnapshotHelper;

public class HttpResponseMessageConverter : JsonConverter<HttpResponseMessage>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

have to use a cusotm json serializer here instead of default jsonconverter because HttpResponseMessage is cyclic in nature with some of its attributes so json cannot be easily created

using var document = JsonDocument.ParseValue(ref reader);
var element = document.RootElement;

var response = new HttpResponseMessage((HttpStatusCode)element.GetProperty("StatusCode").GetInt32());
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 only care about status code, content, and headers in our test


public static class ApiGatewayResponseTestCases
{
public static IEnumerable<object[]> V1TestCases()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are all the same. the only minor change that i did (as mentioned in the pr description) is remove the unneeded "Assertions" functions

@GarrettBeatty GarrettBeatty marked this pull request as ready for review April 15, 2025 14:42
@GarrettBeatty GarrettBeatty requested a review from philasmar April 15, 2025 14:42
@muhammad-othman muhammad-othman self-requested a review April 15, 2025 17:30
Comment on lines +23 to +50
public async Task VerifyApiGatewayResponseAsync(
APIGatewayProxyResponse response,
ApiGatewayEmulatorMode emulatorMode,
string snapshotName)
{
// Convert response to HttpResponse (simulates what API Gateway would do)
var convertedResponse = await ConvertToHttpResponseAsync(response, emulatorMode);

// Load the expected response from snapshot
var expectedResponse = await _snapshots.LoadSnapshot<HttpResponseMessage>(snapshotName);

// Compare the responses
await AssertResponsesEqual(expectedResponse, convertedResponse);
}

public async Task VerifyHttpApiV2ResponseAsync(
APIGatewayHttpApiV2ProxyResponse response,
string snapshotName)
{
// Convert response to HttpResponse (simulates what API Gateway would do)
var convertedResponse = await ConvertToHttpResponseAsync(response);

// Load the expected response from snapshot
var expectedResponse = await _snapshots.LoadSnapshot<HttpResponseMessage>(snapshotName);

// Compare the responses
await AssertResponsesEqual(expectedResponse, convertedResponse);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: these 2 functions are very close, they can be combined into a single function

Copy link
Contributor Author

Choose a reason for hiding this comment

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

although they are similar, i think this is simplified enough. they have different function signatures and call ConvertToHttpResponseAsync in slightly different ways.


namespace Amazon.Lambda.TestTool.UnitTests.SnapshotHelper;

public class HttpResponseMessageConverter : JsonConverter<HttpResponseMessage>
Copy link
Contributor

Choose a reason for hiding this comment

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

add docs explaining what this is for and what is is doing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added docs in latest commit

@GarrettBeatty GarrettBeatty merged commit f5e639f into dev Apr 21, 2025
5 checks passed
@GarrettBeatty GarrettBeatty deleted the gcbeatty/snapshots branch April 21, 2025 17:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Release Not Needed Add this label if a PR does not need to be released.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants