blob: dfca0547046451b2096326715692831d2b3c7760 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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'
```
|