Skip to main content

Artifacts to test dependencies with AWS using boto3

Project description

Author:

Elvio Toccalino

Date:
2024/11/06
Version:
1.0.0

Testing artifacts to work with the boto3 library.

The focus is on python 3 and boto3. So far, utils cover working with SQS queues and SNS topics.

SQS

LiveTestQueue allows to quicky test code that depends on a SQS queue.

>>> with LiveTestQueue() as queue:
>>>   queue.send_message(MessageBody='some')
>>>   msgs = queue.receive_messages()
>>>   print(msgs[0].body)

The context manager takes care of creating and finally deleting the queue, as well as ensuring the queue has a unique name (prefixed, to be identified as a “test” queue).

SNS

LiveTestTopicQueue allows to test code on topics.

>>> with LiveTestTopicQueue() as topic, queue:
>>>     topic.publish(Message='some')
>>>     msgs = queue.receive_messages()
>>>     print(msgs[0].body)

The context manager creates (and finally deletes) a pair of objects, one topic and one queue, that work together. Messages published to the topic can be red back on the queue. The topic has the appropriate policy to publish to the queue, and the queue is subscribed to the topic to operate as its endpoint.

DynamoDB

LiveTestDynamoDBTable allows to test using live test DynamoDB tables:

>>> key_schema, attribute_definitions, provisioned_throughput = LiveTestDynamoDBTable.create_key_schema(
>>>     partition_key_name='my_partition_key', sorting_key_name='my_sorting_key',
>>>     partition_key_type='S', sorting_key_type='N', read_capacity_units=1, write_capacity_units=1)
>>> with LiveTestDynamoDBTable(key_schema_definition=key_schema,
>>>                            attribute_definitions=attribute_definitions,
>>>                            provisioned_throughput=provisioned_throughput) as table:

Note the helper function to create the key schemas. Upong exiting the context manager, the test table is deleted.

Miscs

reduce_logging_output()

Quicky reduces the amount of logging output from botocore to simplify debugging of other components.

cleanup()

Delete test topics and queues that might have been left behind. This function can also be invoked as a script, using python -m awstestutils.cleanup.

Tests

The package includes a set of integration tests. These test live objects against the AWS backend, so the network must be up and the boto3 must be correctly configured (as described here).

Examples

An example test can be found in examples.py.

Test an object that uses a topic to send data:

with LiveTestQueue() as queue:
    o = ObjectUnderTest(sqs_queue=queue)
    o.do_something()
    o.send_results_to_backend()

    msgs = queue.receive_messages()
self.assertEqual(len(msgs), 1)
expected = json.dumps(expected_output)
self.assertEqual(msgs[0].body, expected)

Testing an object that publishes to a topic, inspecting the message published:

with LiveTestTopicQueue() as (topic, queue):
    o = ObjectUnderTest(topic)
    o.do_something()
    o.send_results_to_aws()

    msgs = queue.receive_messages()
expected = json.dumps(expected_output)
self.assertEqual(msgs[0].body, expected)

Collaborators

  • Facundo Martinez (@fnmartinez) did an awesome job adding support for DynamoDB and its associated tests.

  • Ezequiel Pochiero (@epochiero) fixed the original blunder managing the region names.

Thank you both!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

awstestutils-1.0.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

awstestutils-1.0.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file awstestutils-1.0.0.tar.gz.

File metadata

File hashes

Hashes for awstestutils-1.0.0.tar.gz
Algorithm Hash digest
SHA256 bf70e835c3ce080456626096f1b65632eb7a49bcf551c7d0dc3fc8983db7f98e
MD5 988982f428045118828909c16da10ec1
BLAKE2b-256 a03c965512c1f6df268903c02f43ca9cdea3f8810277d0920e300794dd94f6d1

See more details on using hashes here.

File details

Details for the file awstestutils-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for awstestutils-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df2802ded77a26b701269dedd059b3f6da1eb06c614d69734438ebdfea465dd0
MD5 530d02f443c94342d76e553bc99bcf74
BLAKE2b-256 d53cf133b42231c5b4c6d4417254ebdaabce3ba51a328284a65f0ad96b9cd72d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page