2016-01-22 05:01:30 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
class SetTimeMixin(object):
|
|
|
|
''' Patches timezone.now() for the duration of a test case. Allows us to
|
|
|
|
test time-based conditions (ceilings etc) relatively easily. '''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(SetTimeMixin, self).setUp()
|
|
|
|
self._old_timezone_now = timezone.now
|
|
|
|
self.now = timezone.now()
|
|
|
|
timezone.now = self.new_timezone_now
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
timezone.now = self._old_timezone_now
|
|
|
|
super(SetTimeMixin, self).tearDown()
|
|
|
|
|
|
|
|
def set_time(self, time):
|
|
|
|
self.now = time
|
|
|
|
|
|
|
|
def add_timedelta(self, delta):
|
|
|
|
self.now += delta
|
|
|
|
|
|
|
|
def new_timezone_now(self):
|
|
|
|
return self.now
|