-
Notifications
You must be signed in to change notification settings - Fork 264
/
test_ticking.py
80 lines (62 loc) · 2.37 KB
/
test_ticking.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import datetime
import time
from unittest import mock
import pytest
from freezegun import freeze_time
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)
@pytest.mark.skipif(not hasattr(time, "clock"),
reason="time.clock was removed in Python 3.8")
@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
assert fifth > fourth
@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', 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 error on non-CPython")
@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 not error on CPython")