Permalink
Browse files

once again, dont set fake date time until started

  • Loading branch information...
1 parent 12ad2bd commit a9cc66ce1f6c932e239dada6ee743783f75f3ca7 @spulec committed Mar 24, 2013
Showing with 56 additions and 6 deletions.
  1. +28 −3 freezegun/api.py
  2. +1 −0 requirements.txt
  3. 0 tests/__init__.py
  4. +4 −0 tests/fake_module.py
  5. +21 −2 tests/test_class_import.py
  6. +2 −1 tox.ini
View
@@ -1,5 +1,6 @@
import datetime
import functools
+import sys
from dateutil import parser
@@ -77,9 +78,6 @@ def utcnow(cls):
result = real_datetime.utcnow()
return result
-datetime.datetime = FakeDatetime
-datetime.date = FakeDate
-
def datetime_to_fakedatetime(datetime):
return FakeDatetime(datetime.year,
@@ -116,6 +114,23 @@ def __exit__(self, *args):
self.stop()
def start(self):
+ datetime.datetime = FakeDatetime
+ datetime.date = FakeDate
+
+ modules_for_datetime_faking = []
+ modules_for_date_faking = []
+
+ modules = sys.modules.values()
+
+ for module in modules:
+ if module is None:
+ continue
+ if module.__name__ != 'datetime':
+ if hasattr(module, 'datetime') and module.datetime == real_datetime:
+ module.datetime = FakeDatetime
+ if hasattr(module, 'date') and module.date == real_date:
+ module.date = FakeDate
+
datetime.datetime.time_to_freeze = self.time_to_freeze
datetime.datetime.tz_offset = self.tz_offset
datetime.datetime.active = True
@@ -129,6 +144,16 @@ def stop(self):
datetime.datetime.active = False
datetime.date.active = False
+ datetime.datetime = real_datetime
+ datetime.date = real_date
+
+ for mod_name, module in sys.modules.items():
+ if mod_name != 'datetime':
+ if hasattr(module, 'datetime') and module.datetime == FakeDatetime:
+ module.datetime = real_datetime
+ if hasattr(module, 'date') and module.date == FakeDate:
+ module.date = real_date
+
def decorate_callable(self, func):
def wrapper(*args, **kwargs):
with self:
View
@@ -1,2 +1,3 @@
coverage
nose
+sure
View
No changes.
@@ -0,0 +1,4 @@
+from datetime import datetime
+
+def fake_function():
+ return datetime.now()
@@ -1,7 +1,26 @@
+import sure
+from .fake_module import fake_function
from freezegun import freeze_time
+from freezegun.api import FakeDatetime
from datetime import datetime
@freeze_time("2012-01-14")
-def test():
- assert datetime.now() == datetime(2012, 1, 14)
+def test_import_datetime_works():
+ fake_function().day.should.equal(14)
+
+
+def test_start_and_stop_works():
+ freezer = freeze_time("2012-01-14")
+
+ fake_function().should.be.a(datetime)
+ fake_function().shouldnt.be.a(FakeDatetime)
+
+ freezer.start()
+ fake_function().day.should.equal(14)
+ fake_function().should.be.a(datetime)
+ fake_function().should.be.a(FakeDatetime)
+
+ freezer.stop()
+ fake_function().should.be.a(datetime)
+ fake_function().shouldnt.a(FakeDatetime)
View
@@ -10,4 +10,5 @@ envlist = py26, py27, py33
commands = make test
deps =
nose
- coverage
+ coverage
+ sure

0 comments on commit a9cc66c

Please sign in to comment.