| 1 |
import datetime |
| 2 |
import sys |
| 3 |
import time |
| 4 |
|
| 5 |
from unittest import mock |
| 6 |
import pytest |
| 7 |
|
| 8 |
from freezegun import freeze_time |
| 9 |
from tests import utils |
| 10 |
|
| 11 |
@utils.cpython_only |
| 12 |
def test_ticking_datetime() -> None: |
| 13 |
with freeze_time("Jan 14th, 2012", tick=True): |
| 14 |
time.sleep(0.001) |
| 15 |
assert datetime.datetime.now() > datetime.datetime(2012, 1, 14) |
| 16 |
|
| 17 |
|
| 18 |
@pytest.mark.skipif(not hasattr(time, "clock"), |
| 19 |
reason="time.clock was removed in Python 3.8") |
| 20 |
@utils.cpython_only |
| 21 |
def test_ticking_time_clock() -> None: |
| 22 |
with freeze_time('2012-01-14 03:21:34', tick=True): |
| 23 |
first = time.clock() |
| 24 |
time.sleep(0.001) |
| 25 |
with freeze_time('2012-01-14 03:21:35', tick=True): |
| 26 |
second = time.clock() |
| 27 |
time.sleep(0.001) |
| 28 |
|
| 29 |
with freeze_time('2012-01-14 03:21:36', tick=True): |
| 30 |
third = time.clock() |
| 31 |
time.sleep(0.001) |
| 32 |
|
| 33 |
|
| 34 |
with freeze_time('2012-01-14 03:20:00', tick=True): |
| 35 |
fourth = time.clock() |
| 36 |
time.sleep(0.001) |
| 37 |
fifth = time.clock() |
| 38 |
|
| 39 |
assert first > 0 |
| 40 |
assert second > first |
| 41 |
assert second > 1 |
| 42 |
assert third > second |
| 43 |
assert third > 2 |
| 44 |
|
| 45 |
assert third > fourth |
| 46 |
assert second > fourth |
| 47 |
assert first > fourth |
| 48 |
|
| 49 |
assert fifth > fourth |
| 50 |
|
| 51 |
|
| 52 |
@utils.cpython_only |
| 53 |
def test_ticking_date() -> None: |
| 54 |
with freeze_time("Jan 14th, 2012, 23:59:59.9999999", tick=True): |
| 55 |
time.sleep(0.001) |
| 56 |
assert datetime.date.today() == datetime.date(2012, 1, 15) |
| 57 |
|
| 58 |
|
| 59 |
@utils.cpython_only |
| 60 |
def test_ticking_time() -> None: |
| 61 |
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True): |
| 62 |
time.sleep(0.001) |
| 63 |
assert time.time() > 1326585599.0 |
| 64 |
|
| 65 |
|
| 66 |
@utils.cpython_only |
| 67 |
def test_ticking_tick() -> None: |
| 68 |
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True) as ft: |
| 69 |
ft.tick(61) |
| 70 |
time.sleep(0.001) |
| 71 |
assert datetime.datetime.now().replace( |
| 72 |
second=0, microsecond=0 |
| 73 |
) == datetime.datetime(2012, 1, 15, 0, 1, 0) |
| 74 |
|
| 75 |
ft.tick(delta=datetime.timedelta(minutes=2)) |
| 76 |
time.sleep(0.001) |
| 77 |
assert datetime.datetime.now().replace( |
| 78 |
second=0, microsecond=0 |
| 79 |
) == datetime.datetime(2012, 1, 15, 0, 3, 0) |
| 80 |
|
| 81 |
|
| 82 |
@utils.cpython_only |
| 83 |
def test_ticking_move_to() -> None: |
| 84 |
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True) as ft: |
| 85 |
ft.move_to("Jan 15th, 2012, 00:59:59.999999") |
| 86 |
time.sleep(0.001) |
| 87 |
assert datetime.datetime.now().replace(second=0, microsecond=0) == datetime.datetime(2012, 1, 15, 1, 0, 0) |
| 88 |
|
| 89 |
|
| 90 |
@utils.cpython_only_mark |
| 91 |
@pytest.mark.parametrize("func_name", |
| 92 |
("monotonic", "monotonic_ns", "perf_counter", "perf_counter_ns"), |
| 93 |
) |
| 94 |
def test_ticking_monotonic(func_name: str) -> None: |
| 95 |
if sys.version_info[0:2] >= (3, 7): |
| 96 |
|
| 97 |
|
| 98 |
😅 |
| 99 |
assert hasattr(time, func_name) |
| 100 |
else: |
| 101 |
if not hasattr(time, func_name): |
| 102 |
pytest.skip( |
| 103 |
"time.%s does not exist in the current Python version" % func_name) |
| 104 |
|
| 105 |
func = getattr(time, func_name) |
| 106 |
with freeze_time("Jan 14th, 2012, 23:59:59", tick=True): |
| 107 |
initial = func() |
| 108 |
time.sleep(0.001) |
| 109 |
assert func() > initial |
| 110 |
|
| 111 |
|
| 112 |
@mock.patch('freezegun.api._is_cpython', False) |
| 113 |
def test_pypy_compat() -> None: |
| 114 |
try: |
| 115 |
freeze_time("Jan 14th, 2012, 23:59:59", tick=True) |
| 116 |
except SystemError: |
| 117 |
pass |
| 118 |
else: |
| 119 |
raise AssertionError("tick=True should error on non-CPython") |
| 120 |
|
| 121 |
|
| 122 |
@mock.patch('freezegun.api._is_cpython', True) |
| 123 |
def test_non_pypy_compat() -> None: |
| 124 |
try: |
| 125 |
freeze_time("Jan 14th, 2012, 23:59:59", tick=True) |
| 126 |
except Exception: |
| 127 |
raise AssertionError("tick=True should not error on CPython") |