-
Notifications
You must be signed in to change notification settings - Fork 488
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
Conversation
|
||
namespace Amazon.Lambda.TestTool.UnitTests.SnapshotHelper; | ||
|
||
public class HttpResponseMessageConverter : JsonConverter<HttpResponseMessage> |
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
...aTestTool-v2/tests/Amazon.Lambda.TestTool.UnitTests/Extensions/HttpContextExtensionsTests.cs
Outdated
Show resolved
Hide resolved
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); | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Issue #, if available:
Description of changes:
We previously had integration tests for lambda test tool which did the following:
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:
saveSnapshot
method that i have created. This produced the snapshot json files you see.Assertions
in theApiGatewayResponseTestCases
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.