Example Python AWS SNS Consumer
Source Code​
https://github.com/pactflow/example-consumer-python-sns
This is an example of a Python AWS SNS consumer that uses Pact, Pactflow and GitHub Actions to ensure that it is compatible with the expectations its consumers have of it.
All examples in the series example-consumer-<language>-sns
provide the same functionality to be easily comparable across languages.
As such, please refer to https://docs.pactflow.io/docs/examples/aws/sns/consumer/ to avoid unnecessary duplication of details here.
Language specific sections which differ from the canonical example only can be found below.
How to write tests?​
We recommend that you split the code that is responsible for handling the protocol specific things - in this case the lambda and SNS input - and the piece of code that actually handles the payload.
You're probably familiar with layered architectures such as Ports and Adaptors (also referred to as a Hexagonal architecture). Following a modular architecture will allow you to do this much more easily:
This code base is setup with this modularity in mind:
- Lambda Handler
- Event Service
- Business Logic
The target of our consumer pact test is the Event Service, which is responsible for consuming a Product update event, and persisting it to a database (the Repository).
See also:
- https://dius.com.au/2017/09/22/contract-testing-serverless-and-asynchronous-applications/
- https://dius.com.au/2018/10/01/contract-testing-serverless-and-asynchronous-applications---part-2/
Usage​
Testing​
- Run the unit tests:
make test
- Run a (local) lambda integration test:
make integration