Commit faf8860b authored by David Steele's avatar David Steele

New upstream version 0.3.11

parent 72e3341a
......@@ -14,3 +14,4 @@ Patches and Suggestions
- `Adam Johnson <https://github.com/adamchainz>`_
- `Alex Ehlke <https://github.com/aehlke>`_
- `James Lu <github.com/CrazyPython>`_
- `Dan Elkis <github.com/rinslow>`_
......@@ -4,12 +4,18 @@ Freezegun Changelog
Latest
------
0.3.11
------
* Performance improvements
* Fix nesting time.time
* Add nanosecond property
0.3.10
------
* Performance improvements
* Coroutine support
*
0.3.9
-----
......
Metadata-Version: 1.1
Metadata-Version: 1.2
Name: freezegun
Version: 0.3.10
Version: 0.3.11
Summary: Let your Python tests travel through time
Home-page: https://github.com/spulec/freezegun
Author: Steve Pulec
Author-email: spulec@gmail.com
License: Apache 2.0
Description-Content-Type: UNKNOWN
Description: UNKNOWN
Description: FreezeGun: Let your Python tests travel through time
====================================================
.. image:: https://secure.travis-ci.org/spulec/freezegun.svg?branch=master
:target: https://travis-ci.org/spulec/freezegun
.. image:: https://coveralls.io/repos/spulec/freezegun/badge.svg?branch=master
:target: https://coveralls.io/r/spulec/freezegun
FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
Usage
-----
Once the decorator or context manager have been invoked, all calls to datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), and time.strftime() will return the time that has been frozen.
Decorator
~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
import datetime
import unittest
@freeze_time("2012-01-14")
def test():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
# Or a unittest TestCase - freezes for every test, from the start of setUpClass to the end of tearDownClass
@freeze_time("1955-11-12")
class MyTests(unittest.TestCase):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(1955, 11, 12)
# Or any other class - freezes around each callable (may not work in every case)
@freeze_time("2012-01-14")
class Tester(object):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
Context manager
~~~~~~~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
def test():
assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
with freeze_time("2012-01-14"):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
Raw use
~~~~~~~
.. code-block:: python
from freezegun import freeze_time
freezer = freeze_time("2012-01-14 12:00:01")
freezer.start()
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14, 12, 0, 1)
freezer.stop()
Timezones
~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
@freeze_time("2012-01-14 03:21:34", tz_offset=-4)
def test():
assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34)
assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34)
# datetime.date.today() uses local time
assert datetime.date.today() == datetime.date(2012, 1, 13)
@freeze_time("2012-01-14 03:21:34", tz_offset=-datetime.timedelta(hours=3, minutes=30))
def test_timedelta_offset():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34)
Nice inputs
~~~~~~~~~~~
FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes.
.. code-block:: python
@freeze_time("Jan 14th, 2012")
def test_nice_datetime():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
Function and generator objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FreezeGun is able to handle function and generator objects.
.. code-block:: python
def test_lambda():
with freeze_time(lambda: datetime.datetime(2012, 1, 14)):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
def test_generator():
datetimes = (datetime.datetime(year, 1, 1) for year in range(2010, 2012))
with freeze_time(datetimes):
assert datetime.datetime.now() == datetime.datetime(2010, 1, 1)
with freeze_time(datetimes):
assert datetime.datetime.now() == datetime.datetime(2011, 1, 1)
# The next call to freeze_time(datetimes) would raise a StopIteration exception.
``tick`` argument
~~~~~~~~~~~~~~~~~
FreezeGun has an additional ``tick`` argument which will restart time at the given
value, but then time will keep ticking. This is alternative to the default
parameters which will keep time stopped.
.. code-block:: python
@freeze_time("Jan 14th, 2020", tick=True)
def test_nice_datetime():
assert datetime.datetime.now() > datetime.datetime(2020, 1, 14)
Manual ticks
~~~~~~~~~~~~
FreezeGun allows for the time to be manually forwarded as well.
.. code-block:: python
def test_manual_increment():
initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime
frozen_datetime.tick()
initial_datetime += datetime.timedelta(seconds=1)
assert frozen_datetime() == initial_datetime
frozen_datetime.tick(delta=datetime.timedelta(seconds=10))
initial_datetime += datetime.timedelta(seconds=10)
assert frozen_datetime() == initial_datetime
Moving time to specify datetime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FreezeGun allows moving time to specific dates.
.. code-block:: python
def test_move_to():
initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
other_datetime = datetime.datetime(year=2, month=8, day=13,
hour=14, minute=5, second=0)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime
frozen_datetime.move_to(other_datetime)
assert frozen_datetime() == other_datetime
frozen_datetime.move_to(initial_datetime)
assert frozen_datetime() == initial_datetime
@freeze_time("2012-01-14", as_arg=True)
def test(frozen_time):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
frozen_time.move_to("2014-02-12")
assert datetime.datetime.now() == datetime.datetime(2014, 2, 12)
Parameter for ``move_to`` can be any valid ``freeze_time`` date (string, date, datetime).
Default arguments
~~~~~~~~~~~~~~~~~
Note that FreezeGun will not modify default arguments. The following code will
print the current date. See `here <http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments>`_ for why.
.. code-block:: python
from freezegun import freeze_time
import datetime as dt
def test(default=dt.date.today()):
print(default)
with freeze_time('2000-1-1'):
test()
Installation
------------
To install FreezeGun, simply:
.. code-block:: bash
$ pip install freezegun
On Debian (Testing and Unstable) systems:
.. code-block:: bash
$ sudo apt-get install python-freezegun
Platform: UNKNOWN
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
......@@ -7,7 +7,7 @@ FreezeGun: Let your Python tests travel through time
.. image:: https://coveralls.io/repos/spulec/freezegun/badge.svg?branch=master
:target: https://coveralls.io/r/spulec/freezegun
FreezeGun is a library that allows your python tests to travel through time by mocking the datetime module.
FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
Usage
-----
......@@ -42,7 +42,7 @@ Decorator
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
Context Manager
Context manager
~~~~~~~~~~~~~~~
.. code-block:: python
......@@ -135,7 +135,7 @@ parameters which will keep time stopped.
Manual ticks
~~~~~~~~~~~~
Freezegun allows for the time to be manually forwarded as well.
FreezeGun allows for the time to be manually forwarded as well.
.. code-block:: python
......@@ -156,7 +156,7 @@ Freezegun allows for the time to be manually forwarded as well.
Moving time to specify datetime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Freezegun allows moving time to specific dates.
FreezeGun allows moving time to specific dates.
.. code-block:: python
......@@ -185,10 +185,10 @@ Freezegun allows moving time to specific dates.
Parameter for ``move_to`` can be any valid ``freeze_time`` date (string, date, datetime).
Default Arguments
Default arguments
~~~~~~~~~~~~~~~~~
Note that Freezegun will not modify default arguments. The following code will
Note that FreezeGun will not modify default arguments. The following code will
print the current date. See `here <http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments>`_ for why.
.. code-block:: python
......
Metadata-Version: 1.1
Metadata-Version: 1.2
Name: freezegun
Version: 0.3.10
Version: 0.3.11
Summary: Let your Python tests travel through time
Home-page: https://github.com/spulec/freezegun
Author: Steve Pulec
Author-email: spulec@gmail.com
License: Apache 2.0
Description-Content-Type: UNKNOWN
Description: UNKNOWN
Description: FreezeGun: Let your Python tests travel through time
====================================================
.. image:: https://secure.travis-ci.org/spulec/freezegun.svg?branch=master
:target: https://travis-ci.org/spulec/freezegun
.. image:: https://coveralls.io/repos/spulec/freezegun/badge.svg?branch=master
:target: https://coveralls.io/r/spulec/freezegun
FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
Usage
-----
Once the decorator or context manager have been invoked, all calls to datetime.datetime.now(), datetime.datetime.utcnow(), datetime.date.today(), time.time(), time.localtime(), time.gmtime(), and time.strftime() will return the time that has been frozen.
Decorator
~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
import datetime
import unittest
@freeze_time("2012-01-14")
def test():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
# Or a unittest TestCase - freezes for every test, from the start of setUpClass to the end of tearDownClass
@freeze_time("1955-11-12")
class MyTests(unittest.TestCase):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(1955, 11, 12)
# Or any other class - freezes around each callable (may not work in every case)
@freeze_time("2012-01-14")
class Tester(object):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
Context manager
~~~~~~~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
def test():
assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
with freeze_time("2012-01-14"):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
Raw use
~~~~~~~
.. code-block:: python
from freezegun import freeze_time
freezer = freeze_time("2012-01-14 12:00:01")
freezer.start()
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14, 12, 0, 1)
freezer.stop()
Timezones
~~~~~~~~~
.. code-block:: python
from freezegun import freeze_time
@freeze_time("2012-01-14 03:21:34", tz_offset=-4)
def test():
assert datetime.datetime.utcnow() == datetime.datetime(2012, 1, 14, 3, 21, 34)
assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 21, 34)
# datetime.date.today() uses local time
assert datetime.date.today() == datetime.date(2012, 1, 13)
@freeze_time("2012-01-14 03:21:34", tz_offset=-datetime.timedelta(hours=3, minutes=30))
def test_timedelta_offset():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 13, 23, 51, 34)
Nice inputs
~~~~~~~~~~~
FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes.
.. code-block:: python
@freeze_time("Jan 14th, 2012")
def test_nice_datetime():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
Function and generator objects
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FreezeGun is able to handle function and generator objects.
.. code-block:: python
def test_lambda():
with freeze_time(lambda: datetime.datetime(2012, 1, 14)):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
def test_generator():
datetimes = (datetime.datetime(year, 1, 1) for year in range(2010, 2012))
with freeze_time(datetimes):
assert datetime.datetime.now() == datetime.datetime(2010, 1, 1)
with freeze_time(datetimes):
assert datetime.datetime.now() == datetime.datetime(2011, 1, 1)
# The next call to freeze_time(datetimes) would raise a StopIteration exception.
``tick`` argument
~~~~~~~~~~~~~~~~~
FreezeGun has an additional ``tick`` argument which will restart time at the given
value, but then time will keep ticking. This is alternative to the default
parameters which will keep time stopped.
.. code-block:: python
@freeze_time("Jan 14th, 2020", tick=True)
def test_nice_datetime():
assert datetime.datetime.now() > datetime.datetime(2020, 1, 14)
Manual ticks
~~~~~~~~~~~~
FreezeGun allows for the time to be manually forwarded as well.
.. code-block:: python
def test_manual_increment():
initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime
frozen_datetime.tick()
initial_datetime += datetime.timedelta(seconds=1)
assert frozen_datetime() == initial_datetime
frozen_datetime.tick(delta=datetime.timedelta(seconds=10))
initial_datetime += datetime.timedelta(seconds=10)
assert frozen_datetime() == initial_datetime
Moving time to specify datetime
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FreezeGun allows moving time to specific dates.
.. code-block:: python
def test_move_to():
initial_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
other_datetime = datetime.datetime(year=2, month=8, day=13,
hour=14, minute=5, second=0)
with freeze_time(initial_datetime) as frozen_datetime:
assert frozen_datetime() == initial_datetime
frozen_datetime.move_to(other_datetime)
assert frozen_datetime() == other_datetime
frozen_datetime.move_to(initial_datetime)
assert frozen_datetime() == initial_datetime
@freeze_time("2012-01-14", as_arg=True)
def test(frozen_time):
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
frozen_time.move_to("2014-02-12")
assert datetime.datetime.now() == datetime.datetime(2014, 2, 12)
Parameter for ``move_to`` can be any valid ``freeze_time`` date (string, date, datetime).
Default arguments
~~~~~~~~~~~~~~~~~
Note that FreezeGun will not modify default arguments. The following code will
print the current date. See `here <http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments>`_ for why.
.. code-block:: python
from freezegun import freeze_time
import datetime as dt
def test(default=dt.date.today()):
print(default)
with freeze_time('2000-1-1'):
test()
Installation
------------
To install FreezeGun, simply:
.. code-block:: bash
$ pip install freezegun
On Debian (Testing and Unstable) systems:
.. code-block:: bash
$ sudo apt-get install python-freezegun
Platform: UNKNOWN
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.6
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*
......@@ -9,7 +9,7 @@ freezegun
from .api import freeze_time
__title__ = 'freezegun'
__version__ = '0.3.10'
__version__ = '0.3.11'
__author__ = 'Steve Pulec'
__license__ = 'Apache License 2.0'
__copyright__ = 'Copyright 2012 Steve Pulec'
......
This diff is collapsed.
......@@ -4,36 +4,42 @@ import sys
from setuptools import setup
requires = ['six']
tests_require = ['mock', 'nose']
if sys.version_info[0] == 2:
if sys.version_info.major == 2:
requires += ['python-dateutil>=1.0, != 2.0']
else:
# Py3k
requires += ['python-dateutil>=2.0']
with open('README.rst') as f:
readme = f.read()
setup(
name='freezegun',
version='0.3.10',
version='0.3.11',
description='Let your Python tests travel through time',
long_desciption=readme,
long_description=readme,
author='Steve Pulec',
author_email='spulec@gmail.com',
url='https://github.com/spulec/freezegun',
packages=['freezegun'],
install_requires=requires,
tests_require=tests_require,
include_package_data=True,
license='Apache 2.0',
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
],
)
......@@ -70,6 +70,28 @@ def test_isinstance_works():
freezer.stop()
def test_fake_uses_real_when_ignored():
real_time_before = time.time()
with freeze_time('2012-01-14', ignore=['tests.fake_module']):
real_time = fake_time_function()
real_time_after = time.time()
assert real_time_before <= real_time <= real_time_after
def test_can_ignore_email_module():
from email.utils import formatdate
with freeze_time('2012-01-14'):
faked_date_str = formatdate()
before_date_str = formatdate()
with freeze_time('2012-01-14', ignore=['email']):
date_str = formatdate()
after_date_str = formatdate()
assert date_str != faked_date_str
assert before_date_str <= date_str <= after_date_str
@freeze_time('2011-01-01')
def test_avoid_replacing_equal_to_anything():
assert fake_module.equal_to_anything.description == 'This is the equal_to_anything object'
......
......@@ -9,7 +9,13 @@ from nose.tools import assert_raises
from tests import utils
from freezegun import freeze_time
from freezegun.api import FakeDatetime, FakeDate
from freezegun.api import FakeDatetime, FakeDate, BaseFakeTime
try:
import maya
except ImportError:
maya = None
class temp_locale(object):
......@@ -201,6 +207,17 @@ def test_time_gmtime():
assert time_struct.tm_isdst == -1
def test_time_clock():
with freeze_time('2012-01-14 03:21:34'):
assert time.clock() == 0
with freeze_time('2012-01-14 03:21:35'):
assert time.clock() == 1
with freeze_time('2012-01-14 03:21:36'):
assert time.clock() == 2
class modify_timezone(object):
def __init__(self, new_timezone):
......@@ -302,6 +319,19 @@ def test_generator_object():
assert_raises(StopIteration, freeze_time, frozen_datetimes)
def test_maya_datetimes():
if not maya:
raise skip.SkipTest("maya is optional since it's not supported for "
"enough python versions")
with freeze_time(maya.when("October 2nd, 1997")):
assert datetime.datetime.now() == datetime.datetime(
year=1997,
month=10,
day=2
)
def test_old_datetime_object():
frozen_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
......@@ -578,3 +608,37 @@ def test_freeze_with_timezone_aware_datetime_in_non_utc():
utc_now = datetime.datetime.utcnow()
assert utc_now.tzinfo is None
assert utc_now == datetime.datetime(1970, 1, 1, 4)
@freeze_time('2015-01-01')
def test_time_with_nested():
from time import time
first = 1420070400.0
second = 1420070760.0
assert time() == first
with freeze_time('2015-01-01T00:06:00'):
assert time() == second
def test_should_use_real_time():
frozen = datetime.datetime(2015, 3, 5)
expected_frozen = 1425513600.0
# TODO: local time seems to leak the local timezone, so this test fails in CI
# expected_frozen_local = (2015, 3, 5, 1, 0, 0, 3, 64, -1)
expected_frozen_gmt = (2015, 3, 5, 0, 0, 0, 3, 64, -1)
expected_clock = 0
BaseFakeTime.call_stack_inspection_limit = 100 # just to increase coverage
with freeze_time(frozen):
assert time.time() == expected_frozen
# assert time.localtime() == expected_frozen_local
assert time.gmtime() == expected_frozen_gmt
assert time.clock() == expected_clock
with freeze_time(frozen, ignore=['_pytest', 'nose']):
assert time.time() != expected_frozen
# assert time.localtime() != expected_frozen_local
assert time.gmtime() != expected_frozen_gmt
assert time.clock() != expected_clock
......@@ -13,6 +13,38 @@ def test_ticking_datetime():
assert datetime.datetime.now() > datetime.datetime(2012, 1, 14)
@utils.cpython_only
def test_ticking_time_clock():
with freeze_time('2012-01-14 03:21:34', tick=True):
first = time.clock()
time.sleep(0.001) # Deal with potential clock resolution problems
with freeze_time('2012-01-14 03:21:35', tick=True):
second = time.clock()
time.sleep(0.001) # Deal with potential clock resolution problems
with freeze_time('2012-01-14 03:21:36', tick=True):
third = time.clock()
time.sleep(0.001)
# Rewind time backwards
with freeze_time('2012-01-14 03:20:00', tick=True):
fourth = time.clock()
time.sleep(0.001)
fifth = time.clock()
assert first > 0
assert second > first
assert second > 1
assert third > second
assert third > 2
assert third > fourth
assert second > fourth
assert first > fourth