Skip to content

Commit

Permalink
Merge pull request #109 from adamchainz/tidy_ticking
Browse files Browse the repository at this point in the history
Tidy up tick=True
  • Loading branch information
spulec committed Aug 6, 2015
2 parents 1e44865 + 8e50db8 commit 2cda4ee
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Expand Up @@ -11,3 +11,4 @@ Patches and Suggestions
- `Andreas Pelme <https://github.com/pelme>`_
- `Jesse London <https://github.com/jesteria>`_
- `Zach Smith <https://github.com/zmsmith>`_
- `Adam Johnson <https://github.com/adamchainz>`_
9 changes: 5 additions & 4 deletions freezegun/api.py
Expand Up @@ -27,9 +27,10 @@ def with_metaclass(meta, *bases):
"""Create a base class with a metaclass."""
return meta("NewBase", bases, {})


def _is_cpython():
return platform.python_implementation() == "CPython"
_is_cpython = (
hasattr(platform, 'python_implementation') and
platform.python_implementation().lower == "cpython"
)


class FakeTime(object):
Expand Down Expand Up @@ -446,7 +447,7 @@ def freeze_time(time_to_freeze, tz_offset=0, ignore=None, tick=False):
if not isinstance(time_to_freeze, (string_type, datetime.date)):
raise TypeError(('freeze_time() expected a string, date instance, or '
'datetime instance, but got type {0}.').format(type(time_to_freeze)))
if tick and not _is_cpython():
if tick and not _is_cpython:
raise SystemError('Calling freeze_time with tick=True is only compatible with CPython')

if ignore is None:
Expand Down
11 changes: 7 additions & 4 deletions tests/test_ticking.py
Expand Up @@ -9,34 +9,37 @@
@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")
4 changes: 2 additions & 2 deletions tests/utils.py
Expand Up @@ -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
4 changes: 1 addition & 3 deletions tox.ini
Expand Up @@ -9,6 +9,4 @@ envlist = py26, py27, pypy, py32, py33, py34, pypy3
[testenv]
commands = make test NOSE_ARGS="{posargs}"
whitelist_externals = make
deps =
nose
coverage
deps = -rrequirements.txt

0 comments on commit 2cda4ee

Please sign in to comment.