EDIT. , , , , freezegun: https://pypi.python.org/pypi/freezegun. , . .
:
, , , . , .
mock- Michael Foord: http://www.voidspace.org.uk/python/mock/, @patch, , , , .
, datetime C, . , , .
- - ( - , Django, , ). , , , , , , :)
datetime.date.today utils/date.py:
import datetime
def today():
return datetime.date.today()
unittest tests.py:
import datetime
import mock
from unittest2 import TestCase
from django.core.exceptions import ValidationError
from .. import validators
class ValidationTests(TestCase):
@mock.patch('utils.date.today')
def test_validate_future_date(self, today_mock):
today_mock.return_value = datetime.date(2010, 1, 1)
validators.validate_future_date(datetime.date(2010, 1, 2))
with self.assertRaises(ValidationError) as e:
validators.validate_future_date(datetime.date(2010, 1, 1))
self.assertEquals([u'Date should be in the future.'], e.exception.messages)
with self.assertRaises(ValidationError) as e:
validators.validate_future_date(datetime.date(2009, 12, 31))
self.assertEquals([u'Date should be in the future.'], e.exception.messages)
:
from django.utils.translation import ugettext_lazy as _
from django.core.exceptions import ValidationError
from utils import date
def validate_future_date(value):
if value <= date.today():
raise ValidationError(_('Date should be in the future.'))
,