Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 6adca04794
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 56 lines (42 sloc) 1.754 kb
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
import datetime
from freezegun import freeze_time


def test_simple_api():
    freezer = freeze_time("2012-01-14")
    freezer.start()
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
    assert datetime.datetime.utcnow() == datetime.datetime(2012, 01, 14)
    assert datetime.date.today() == datetime.date(2012, 01, 14)
    freezer.stop()
    assert datetime.datetime.now() != datetime.datetime(2012, 01, 14)
    freezer = freeze_time("2012-01-10")
    freezer.start()
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 10)
    freezer.stop()


def test_tz_offset():
    freezer = freeze_time("2012-01-14", tz_offset=-4)
    freezer.start()
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 14) - datetime.timedelta(hours=4)
    assert datetime.datetime.utcnow() == datetime.datetime(2012, 01, 14)
    freezer.stop()


def test_tz_offset_with_today():
    freezer = freeze_time("2012-01-14", tz_offset=-4)
    freezer.start()
    assert datetime.date.today() == datetime.date(2012, 01, 13)
    freezer.stop()


@freeze_time("2012-01-14")
def test_decorator():
    assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)


@freeze_time("2012-01-14")
class Tester(object):
    def test_the_class(self):
        assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)


# Mock.patch can be used as decorator or context manager
def test_context_manager():
    with freeze_time("2012-01-14"):
        assert datetime.datetime.now() == datetime.datetime(2012, 01, 14)
    assert datetime.datetime.now() != datetime.datetime(2012, 01, 14)

# @current_time(*args)
# def test_foo_bar():
# pass
# @static_time - clock always returns same thing
# @start_time - clock runs while test runs
Something went wrong with that request. Please try again.