Wraps the Stripe Test Clock object for easier async/await-based test functions.
Project description
Stripe Traveller (python)
A significant part of Stripe's functionality is handled via lifecycle events, e.g. an invoice being generated when a subscription reaches the end of its billing cycle. There are two ways to test code that deals with these lifecycle events:
- Create fixtures that replicate Stripe objects (e.g.
stripe.Subscription) or events (e.g.stripe.WebhookEvent), then pass those fixtures to the end code. - Use Test Clocks to simulate the flow of time in a test environment and allow Stripe to trigger actual lifecycle events.
The former is great when you need/want to test locally and/or quickly. The latter is great when you want a simpler test framework and want to rely on Stripe to generate objects and events, at the expense of longer test cycles and the inability to receive webhook events locally.
This library is intended to be used as a test helper for the latter scenario.
Working with Test Clocks
Test clocks can be difficult to work with directly, in part because you must ensure you are disposing of test clocks when your code ends, otherwise you could get stuck with a lot of junk resources in your Stripe test environment.
Traveller can be used alongside with to ensure test clocks are always disposed when the code block is exited.
Additionally, test clocks require you to pass in a set point in time to advance to and do not offer relative time advancement functions.
The t.advance function allows for relative time advancement while t.goto allows for absolute time advancement.
Finally, Stripe does not use async in their functions, meaning that you must call clock.advance and then manually monitor the test clock in a loop while waiting for advancement to complete.
You can simply await t.advance or await t.goto.
Working with Traveller
The best way to use the Traveller helper class is to use with Traveller().
with Traveller() as t:
customer = stripe.Customer.create(test_clock=t.clock_id, ...)
subscription = stripe.Subscription.create(customer=customer.id, items=items, ..)
await t.advance(days=1)
Because we use with Traveller(), we are guaranteed that our test clock will be disposed of no matter how this block termiantes. Whether we fail an assert, invoke exit, have an uncaught exception, it doesn't matter; __exit__ will always get called and ensure our test clock is deleted.
Additionally, we can use relative or absolute time advancement functions via advance and goto, respectively.
Finally, we can simply await either of those functions to ensure that the following lines of code do not execute until the test clock has completed its advancement of time.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stripe_traveller-2.0.0.tar.gz.
File metadata
- Download URL: stripe_traveller-2.0.0.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2da9a9a37a7b46875ff63ff31730e2532ef99dd9578fb11ca64a968a6b0e9903
|
|
| MD5 |
e4cd57fa2b6a3b7433cefc60552ce867
|
|
| BLAKE2b-256 |
cda23ff5d68e1dd2fa9fb40ec54236897f8b4f1f9e1389f77d8415eac3769351
|
File details
Details for the file stripe_traveller-2.0.0-py3-none-any.whl.
File metadata
- Download URL: stripe_traveller-2.0.0-py3-none-any.whl
- Upload date:
- Size: 14.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.12 Linux/5.15.133.1-microsoft-standard-WSL2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99b327fefdd1cd3a217c588c0913a3c29fe5099e79735bd774abd3dd2800553f
|
|
| MD5 |
1f0cb0f3ec8aeb15ef3d6734ddfd340e
|
|
| BLAKE2b-256 |
e6af33b1e5fd6ecec34e7bd9a560196b53d9f6011e798e966cefbd89f7538c61
|