GraphQL APIs with AWS AppSync: Part three
This is the third of a series of posts describing my experimentation with AWS AppSync, Lambda, and DynamoDB. A public GitHub repository exists with this experiment so others can learn from my efforts. For this post, I will be describing the code that resides on the
v1 tag, so feel free to checkout to the point in time:
git checkout v1
Links to the other posts in this series are listed below:
Serverless and test-driven development
I am a big believer in test-driven development, so another driver in my learning from this experiment was serverless development and test-driven development — can they work well together? Are there impediments to writing code test-first? I discovered, not surprisingly, that test-driven development of AWS Lambda handlers is pretty easy. I’ll be using pytest for this experiment!
Testing the Lambda resolver
Below is a collection of pytest-based unit tests that exercise the Lambda handler/resolver:
This set of unit tests use a mocking fixture (
mocker fixture on lines 7 and 17) from pytest-mock, making it trivial to unit test the Lambda event handler. The first test verifies the direct output.
The second test verifies indirect outputs, the collaborations with the
simple_moving_average function invocations. This is where the mocker fixture really shines, as it captures any interactions with it during the test and can be verified for the presence or absence of interactions after exercise the system under test.
To run just the unit tests, type the following from the project root directory:
pytest -m "not integration" -s
The Alpha Vantage integration code
I created an integration test that actually hits the Alpha Vantage endpoint to retrieve data. Integration tests work well for those integration points in our application. Below is a pytest-based integration test that exercises the Alpha Vantage integration:
I’m using a pytest marker decorator on line 6 to mark the test as an integration test. To run just the integration tests, type the following from the project root directory:
pytest -m integration -s
This was a brief overview of the unit and integration testing that I will be using for the AWS AppSync and Lambda code that I build. The next post, part four in this journey, will detail the implementation of our Vue.js-based front-end.