Commit 2bf9719f authored by Federico Ceratto's avatar Federico Ceratto

Imported Upstream version 0.3.6

parent 358fccbf
......@@ -10,3 +10,6 @@ Patches and Suggestions
- `Tye Wang <https://github.com/tyewang>`_
- `Andreas Pelme <https://github.com/pelme>`_
- `Jesse London <https://github.com/jesteria>`_
- `Zach Smith <https://github.com/zmsmith>`_
- `Adam Johnson <https://github.com/adamchainz>`_
- `Alex Ehlke <https://github.com/aehlke>`_
Metadata-Version: 1.1
Name: freezegun
Version: 0.3.5
Version: 0.3.6
Summary: Let your Python tests travel through time
Home-page: https://github.com/spulec/freezegun
Author: Steve Pulec
Author-email: spulec@gmail
License: UNKNOWN
License: Apache 2.0
Description: UNKNOWN
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
......
......@@ -19,25 +19,28 @@ Decorator
.. code-block:: python
from freezegun import freeze_time
from freezegun import freeze_time
import datetime
import unittest
@freeze_time("2012-01-14")
def test():
assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
# Or a unittest TestCase - freezes for every test, from the start of setUpClass to the end of tearDownClass
@freeze_time("2012-01-14")
def test():
assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
@freeze_time("1955-11-12")
class MyTests(unittest.TestCase):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
# Or a unittest TestCase - freezes for every test, from the start of setUpClass to the end of tearDownClass
# Or any other class - freezes around each callable (may not work in every case)
@freeze_time("1955-11-12")
class MyTests(unittest.TestCase):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(1955, 11, 12)
@freeze_time("2012-01-14")
class Tester(object):
def test_the_class(self):
assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
# 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, 01, 14)
Context Manager
~~~~~~~~~~~~~~~
......@@ -90,6 +93,41 @@ FreezeGun uses dateutil behind the scenes so you can have nice-looking datetimes
def test_nice_datetime():
assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
`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, 01, 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
Installation
------------
......
Metadata-Version: 1.1
Name: freezegun
Version: 0.3.5
Version: 0.3.6
Summary: Let your Python tests travel through time
Home-page: https://github.com/spulec/freezegun
Author: Steve Pulec
Author-email: spulec@gmail
License: UNKNOWN
License: Apache 2.0
Description: UNKNOWN
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
......
......@@ -13,6 +13,7 @@ freezegun.egg-info/requires.txt
freezegun.egg-info/top_level.txt
tests/__init__.py
tests/__init__.pyc
tests/another_module.py
tests/fake_module.py
tests/fake_module.pyc
tests/single_import_module.pyc
......@@ -31,6 +32,7 @@ tests/test_sqlite3.py
tests/test_sqlite3.pyc
tests/test_test.pyc
tests/test_ticking.py
tests/test_ticking.pyc
tests/utils.py
tests/utils.pyc
tests/__pycache__/__init__.cpython-33.pyc
......
# -*- coding: utf-8 -*-
"""
freezegun
~~~~~~~~
......@@ -8,13 +6,13 @@ freezegun
:copyright: (c) 2012 by Steve Pulec.
"""
from .api import freeze_time
__title__ = 'freezegun'
__version__ = '0.3.5'
__version__ = '0.3.6'
__author__ = 'Steve Pulec'
__license__ = 'Apache License 2.0'
__copyright__ = 'Copyright 2012 Steve Pulec'
from .api import freeze_time
__all__ = ["freeze_time"]
This diff is collapsed.
#!/usr/bin/env python
import sys
from setuptools import setup, find_packages
from setuptools import setup
requires = ['six']
......@@ -13,7 +13,7 @@ else:
setup(
name='freezegun',
version='0.3.5',
version='0.3.6',
description='Let your Python tests travel through time',
author='Steve Pulec',
author_email='spulec@gmail',
......@@ -21,7 +21,9 @@ setup(
packages=['freezegun'],
install_requires=requires,
include_package_data=True,
license='Apache 2.0',
classifiers=[
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
......
from datetime import date, datetime
from time import time, localtime, gmtime, strftime
from freezegun.api import (
FakeDatetime,
FakeDate,
FakeTime,
FakeLocalTime,
FakeGMTTime,
FakeStrfTime,
)
# Reals
def get_datetime():
return datetime
def get_date():
return date
def get_time():
return time
def get_localtime():
return localtime
def get_gmtime():
return gmtime
def get_strftime():
return strftime
# Fakes
def get_fake_datetime():
return FakeDatetime
def get_fake_date():
return FakeDate
def get_fake_time():
return FakeTime
def get_fake_localtime():
return FakeLocalTime
def get_fake_gmtime():
return FakeGMTTime
def get_fake_strftime():
return FakeStrfTime
import time
import sys
from .fake_module import (
equal_to_anything,
fake_date_function,
fake_datetime_function,
fake_gmtime_function,
......@@ -10,7 +10,14 @@ from .fake_module import (
)
from . import fake_module
from freezegun import freeze_time
from freezegun.api import FakeDatetime
from freezegun.api import (
FakeDatetime,
FakeDate,
FakeTime,
FakeLocalTime,
FakeGMTTime,
FakeStrfTime,
)
import datetime
......@@ -87,3 +94,53 @@ def test_fake_gmtime_function():
@freeze_time("2012-01-14")
def test_fake_strftime_function():
assert fake_strftime_function() == '2012'
def test_import_after_start():
with freeze_time('2012-01-14'):
assert 'tests.another_module' not in sys.modules.keys()
from tests import another_module
# Reals
assert another_module.get_datetime() is datetime.datetime
assert another_module.get_datetime() is FakeDatetime
assert another_module.get_date() is datetime.date
assert another_module.get_date() is FakeDate
assert another_module.get_time() is time.time
assert isinstance(another_module.get_time(), FakeTime)
assert another_module.get_localtime() is time.localtime
assert isinstance(another_module.get_localtime(), FakeLocalTime)
assert another_module.get_gmtime() is time.gmtime
assert isinstance(another_module.get_gmtime(), FakeGMTTime)
assert another_module.get_strftime() is time.strftime
assert isinstance(another_module.get_strftime(), FakeStrfTime)
# Fakes
assert another_module.get_fake_datetime() is FakeDatetime
assert another_module.get_fake_date() is FakeDate
assert another_module.get_fake_time() is FakeTime
assert another_module.get_fake_localtime() is FakeLocalTime
assert another_module.get_fake_gmtime() is FakeGMTTime
assert another_module.get_fake_strftime() is FakeStrfTime
# Reals
assert another_module.get_datetime() is datetime.datetime
assert not another_module.get_datetime() is FakeDatetime
assert another_module.get_date() is datetime.date
assert not another_module.get_date() is FakeDate
assert another_module.get_time() is time.time
assert not isinstance(another_module.get_time(), FakeTime)
assert another_module.get_localtime() is time.localtime
assert not isinstance(another_module.get_localtime(), FakeLocalTime)
assert another_module.get_gmtime() is time.gmtime
assert not isinstance(another_module.get_gmtime(), FakeGMTTime)
assert another_module.get_strftime() is time.strftime
assert not isinstance(another_module.get_strftime(), FakeStrfTime)
# Fakes
assert another_module.get_fake_datetime() is FakeDatetime
assert another_module.get_fake_date() is FakeDate
assert another_module.get_fake_time() is FakeTime
assert another_module.get_fake_localtime() is FakeLocalTime
assert another_module.get_fake_gmtime() is FakeGMTTime
assert another_module.get_fake_strftime() is FakeStrfTime
......@@ -114,12 +114,29 @@ def test_time_with_microseconds():
assert time.time() == 1.123456
freezer.stop()
def test_time_with_dst():
freezer = freeze_time(datetime.datetime(1970, 6, 1, 0, 0, 1, 123456))
freezer.start()
assert time.time() == 13046401.123456
freezer.stop()
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
def test_bad_time_argument():
try:
freeze_time("2012-13-14", tz_offset=-4)
......@@ -169,6 +186,7 @@ def test_time_localtime():
assert time_struct.tm_wday == 5
assert time_struct.tm_yday == 14
assert time_struct.tm_isdst == -1
assert time.localtime().tm_year != 2012
def test_strftime():
......@@ -183,17 +201,20 @@ def test_date_object():
regular_freezer = freeze_time('2012-11-10')
assert date_freezer.time_to_freeze == regular_freezer.time_to_freeze
def test_old_date_object():
frozen_date = datetime.date(year=1, month=1, day=1)
with freeze_time(frozen_date):
assert datetime.date.today() == frozen_date
def test_date_with_locale():
with temp_locale(*_dd_mm_yyyy_locales):
frozen_date = datetime.date(year=2012, month=1, day=2)
date_freezer = freeze_time(frozen_date)
assert date_freezer.time_to_freeze.date() == frozen_date
def test_invalid_type():
try:
freeze_time(int(4))
......@@ -210,18 +231,21 @@ def test_datetime_object():
regular_freezer = freeze_time('2012-11-10 04:15:30')
assert datetime_freezer.time_to_freeze == regular_freezer.time_to_freeze
def test_old_datetime_object():
frozen_datetime = datetime.datetime(year=1, month=7, day=12,
hour=15, minute=6, second=3)
with freeze_time(frozen_datetime):
assert datetime.datetime.now() == frozen_datetime
def test_datetime_with_locale():
with temp_locale(*_dd_mm_yyyy_locales):
frozen_datetime = datetime.datetime(year=2012, month=1, day=2)
date_freezer = freeze_time(frozen_datetime)
assert date_freezer.time_to_freeze == frozen_datetime
@freeze_time("2012-01-14")
def test_decorator():
assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
......@@ -322,7 +346,7 @@ def test_nested_context_manager_with_tz_offsets():
with freeze_time("2012-12-25 19:00:00", tz_offset=6):
assert datetime.datetime.now() == datetime.datetime(2012, 12, 26, 1)
assert datetime.date.today() == datetime.date(2012, 12, 26)
#no assertion for time.time() since it's not affected by tz_offset
# no assertion for time.time() since it's not affected by tz_offset
assert datetime.datetime.now() == datetime.datetime(2012, 1, 15, 1)
assert datetime.date.today() == datetime.date(2012, 1, 15)
assert datetime.datetime.now() > datetime.datetime(2013, 1, 1)
......@@ -450,7 +474,7 @@ def test_freeze_with_timezone_aware_datetime_in_utc():
utcnow() should always return a timezone naive datetime
"""
utc_now = datetime.datetime.utcnow()
assert utc_now.tzinfo == None
assert utc_now.tzinfo is None
@freeze_time("1970-01-01T00:00:00-04:00")
......@@ -460,5 +484,5 @@ def test_freeze_with_timezone_aware_datetime_in_non_utc():
at the beginning of the Epoch (wall clock should be 4 hrs late)
"""
utc_now = datetime.datetime.utcnow()
assert utc_now.tzinfo == None
assert utc_now.tzinfo is None
assert utc_now == datetime.datetime(1970, 1, 1, 4)
......@@ -5,7 +5,7 @@ from time import time as time_aliased
@freeze_time("1980-01-01")
def test_datetime_alias():
assert datetime_aliased.now() == datetime_aliased(1980,1,1)
assert datetime_aliased.now() == datetime_aliased(1980, 1, 1)
@freeze_time("1970-01-01")
......@@ -17,9 +17,9 @@ def test_time_alias():
class TestCallOtherFuncInTestClassDecoratorWithAlias(object):
def test_calls_other_method(self):
assert datetime_aliased(2013,4,9) == datetime_aliased.today()
assert datetime_aliased(2013, 4, 9) == datetime_aliased.today()
self.some_other_func()
assert datetime_aliased(2013,4,9) == datetime_aliased.today()
assert datetime_aliased(2013, 4, 9) == datetime_aliased.today()
def some_other_func(self):
pass
......@@ -46,11 +46,13 @@ def test_datetime_timezone_none():
class GMT5(tzinfo):
def utcoffset(self,dt):
def utcoffset(self, dt):
return timedelta(hours=5)
def tzname(self,dt):
def tzname(self, dt):
return "GMT +5"
def dst(self,dt):
def dst(self, dt):
return timedelta(0)
......
......@@ -9,34 +9,37 @@ from tests import utils
@utils.cpython_only
def test_ticking_datetime():
with freeze_time("Jan 14th, 2012", tick=True):
time.sleep(0.001) # Deal with potential clock resolution problems
assert datetime.datetime.now() > datetime.datetime(2012, 1, 14)
@utils.cpython_only
def test_ticking_date():
with freeze_time("Jan 14th, 2012, 23:59:59.9999999", tick=True):
time.sleep(0.001) # Deal with potential clock resolution problems
assert datetime.date.today() == datetime.date(2012, 1, 15)
@utils.cpython_only
def test_ticking_time():
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True):
time.sleep(0.001) # Deal with potential clock resolution problems
assert time.time() > 1326585599.0
@mock.patch('freezegun.api._is_cpython', lambda: False)
@mock.patch('freezegun.api._is_cpython', False)
def test_pypy_compat():
try:
freeze_time("Jan 14th, 2012, 23:59:59", tick=True)
except SystemError:
pass
else:
raise AssertionError("tick=True should not error on cpython")
raise AssertionError("tick=True should error on non-CPython")
@mock.patch('freezegun.api._is_cpython', lambda: True)
@mock.patch('freezegun.api._is_cpython', True)
def test_non_pypy_compat():
try:
freeze_time("Jan 14th, 2012, 23:59:59", tick=True)
except Exception:
raise AssertionError("tick=True should error on non cpython")
raise AssertionError("tick=True should not error on CPython")
......@@ -16,7 +16,7 @@ def is_fake_datetime(obj):
def cpython_only(func):
@wraps(func)
def wrapper(*args):
if not _is_cpython():
raise skip.SkipTest("tick=True on pypy")
if not _is_cpython:
raise skip.SkipTest("Requires CPython")
return func(*args)
return wrapper
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment