Skip to content

Commit c637d69

Browse files
committed
Improves testing by providing a contextmanager
1 parent 00938ad commit c637d69

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

docs/index.rst

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,68 @@ It moves your instance to the middle date between itself and the provided Pendul
946946
# first_of(), last_of(), nth_of()
947947
948948
949+
Testing
950+
=======
951+
952+
The testing methods allow you to set a ``Pendulum`` instance (real or mock) to be returned
953+
when a "now" instance is created.
954+
The provided instance will be returned specifically under the following conditions:
955+
956+
* A call to the ``now()`` method, ex. ``pendulum.now()``.
957+
* When the string "now" is passed to the ``parse()``, ex. ``pendulum.parse('now')``
958+
959+
.. code-block:: python
960+
961+
import pendulum
962+
963+
# Create testing datetime
964+
known = pendulum.create(2001, 5, 21, 12)
965+
966+
# Set the mock
967+
pendulum.set_test_now(known)
968+
969+
print(pendulum.now())
970+
'2001-05-21T12:00:00+00:00'
971+
972+
print(pendulum.parse('now'))
973+
'2001-05-21T12:00:00+00:00'
974+
975+
# Clear the mock
976+
pendulum.set_test_now()
977+
978+
print(pendulum.now())
979+
'2016-07-10T22:10:33.954851-05:00'
980+
981+
Related methods will also returned values mocked according to the *now* instance.
982+
983+
.. code-block:: python
984+
985+
print(pendulum.today())
986+
'2001-05-21T00:00:00+00:00'
987+
988+
print(pendulum.tomorrow())
989+
'2001-05-22T00:00:00+00:00'
990+
991+
print(pendulum.yesterday())
992+
'2001-05-20T00:00:00+00:00'
993+
994+
If you don't want to manually clear the mock (or you are afraid of forgetting),
995+
you can use the provided ``test()`` contextmanager.
996+
997+
.. code-block:: python
998+
999+
import pendulum
1000+
1001+
known = pendulum.create(2001, 5, 21, 12)
1002+
1003+
with pendulum.test(known):
1004+
print(pendulum.now())
1005+
'2001-05-21T12:00:00+00:00'
1006+
1007+
print(pendulum.now())
1008+
'2016-07-10T22:10:33.954851-05:00'
1009+
1010+
9491011
Constants
9501012
=========
9511013

pendulum/pendulum.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
import tzlocal
1212
import datetime
1313

14+
from contextlib import contextmanager
1415
from pytz.tzinfo import BaseTzInfo, tzinfo
1516
from dateutil.relativedelta import relativedelta
1617
from dateutil import parser as dateparser
17-
from dateutil.tz import tzoffset
1818

1919
from .translator import Translator
2020

@@ -757,6 +757,20 @@ def set_weekend_days(cls, value):
757757

758758
# Testing aids
759759

760+
@classmethod
761+
@contextmanager
762+
def test(cls, mock):
763+
"""
764+
Context manager to temporarily set the test_now value.
765+
766+
:type mock: Pendulum or None
767+
"""
768+
cls.set_test_now(mock)
769+
770+
yield
771+
772+
cls.set_test_now()
773+
760774
@classmethod
761775
def set_test_now(cls, test_now=None):
762776
"""

tests/pendulum_tests/test_testing_aids.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,11 @@ def test_parse_with_test_value_set(self):
3838

3939
self.assertEqual(test_now, Pendulum.parse())
4040
self.assertEqual(test_now, Pendulum.parse('now'))
41+
42+
def test_context_manager(self):
43+
test_now = Pendulum.yesterday()
44+
45+
with Pendulum.test(test_now):
46+
self.assertEqual(test_now, Pendulum.now())
47+
48+
self.assertNotEqual(test_now, Pendulum.now())

0 commit comments

Comments
 (0)