Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

freeze_time wrapping datetime on import, not when decorator or context manager is invoked #4

Closed
nealtodd opened this issue Dec 13, 2012 · 1 comment

Comments

@nealtodd
Copy link

Hi Steve,

Noticed that as soon as you import freeze_time then calls to datetime.now() or date.today() will be return instances of FakeDatetime or FakeDate without a decorator of context manager (or raw start/stop) being invoked.

I.e. these tests (revised from your tests) should both pass (if I understand correctly):

import datetime
from freezegun import freeze_time
from freezegun.api import FakeDate, FakeDatetime

@freeze_time("Jan 14th, 2012")
def test_isinstance_with_active():
    now = datetime.datetime.now()
    assert isinstance(now, datetime.datetime)
    assert isinstance(now, datetime.date)
    assert isinstance(now, FakeDatetime)

    today = datetime.date.today()
    assert isinstance(today, datetime.date)
    assert isinstance(today, FakeDate)

def test_isinstance_without_active():
    now = datetime.datetime.now()
    assert isinstance(now, datetime.datetime)
    assert isinstance(now, datetime.date)
    assert not isinstance(now, FakeDatetime)

    today = datetime.date.today()
    assert isinstance(today, datetime.date)
    assert not isinstance(now, FakeDate)

However, test_isinstance_without_active fails because now and today are Fake.

@spulec
Copy link
Owner

spulec commented Dec 14, 2012

The lines assert not isinstance(now, FakeDatetime) and assert not isinstance(now, FakeDate) are not expected to pass. Once freeze_time is imported, all datetime/date instances will be instances of FakeDatetime/FakeDate and all references to datetime.datetime/datetime.date will actually be references to the classes FakeDatetime/FakeDate.

The thought is that this will all go on transparently behind the scenes and people will be able to just think of it all as datetime.datetime. Feel free to reopen if this is somehow causing issues in your test writing.

@spulec spulec closed this as completed Dec 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants