diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-05 10:38:34 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-05 10:38:34 +0000 |
commit | e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0 (patch) | |
tree | 34512072a667ae716fd262e7b37e733e60fe4d89 /docs/docs/testing.md | |
parent | Adding upstream version 2.1.2. (diff) | |
download | pendulum-e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0.tar.xz pendulum-e3bdad36cc3a1a00c1e6772ca1c1898085ab73e0.zip |
Adding upstream version 3.0.0~a1.upstream/3.0.0_a1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'docs/docs/testing.md')
-rw-r--r-- | docs/docs/testing.md | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/docs/docs/testing.md b/docs/docs/testing.md new file mode 100644 index 0000000..dfca054 --- /dev/null +++ b/docs/docs/testing.md @@ -0,0 +1,59 @@ +# Testing + +The testing methods allow you to set a `DateTime` instance (real or mock) to be returned +when a "now" instance is created. +The provided instance will be returned specifically under the following conditions: + +* A call to the `now()` method, ex. `pendulum.now()`. +* When the string "now" is passed to the `parse()` method, ex. `pendulum.parse('now')` + +```python +>>> import pendulum + +# Create testing datetime +>>> known = pendulum.datetime(2001, 5, 21, 12) + +# Set the mock +>>> pendulum.set_test_now(known) + +>>> print(pendulum.now()) +'2001-05-21T12:00:00+00:00' + +>>> print(pendulum.parse('now')) +'2001-05-21T12:00:00+00:00' + +# Clear the mock +>>> pendulum.set_test_now() + +>>> print(pendulum.now()) +'2016-07-10T22:10:33.954851-05:00' +``` + +Related methods will also return values mocked according to the **now** instance. + +```python +>>> print(pendulum.today()) +'2001-05-21T00:00:00+00:00' + +>>> print(pendulum.tomorrow()) +'2001-05-22T00:00:00+00:00' + +>>> print(pendulum.yesterday()) +'2001-05-20T00:00:00+00:00' +``` + +If you don't want to manually clear the mock (or you are afraid of forgetting), +you can use the provided `test()` contextmanager. + +```python +>>> import pendulum + +>>> known = pendulum.datetime(2001, 5, 21, 12) + +>>> with pendulum.test(known): +>>> print(pendulum.now()) +'2001-05-21T12:00:00+00:00' + +>>> print(pendulum.now()) +'2016-07-10T22:10:33.954851-05:00' +``` |