Added pytz exception in compat module.

Mock pytz.timezone localize in tests.
Ref: #4986
This commit is contained in:
Sergey Petrunin 2017-03-22 00:01:07 -04:00
parent e4a1bd140b
commit b0a0c30bfe
4 changed files with 24 additions and 6 deletions

View File

@ -2,4 +2,3 @@
pytest==3.0.5
pytest-django==3.1.2
pytest-cov==2.4.0
pytz==2016.10

View File

@ -275,6 +275,14 @@ except ImportError:
def pygments_css(style):
return None
try:
import pytz
from pytz.exceptions import InvalidTimeError
except ImportError:
InvalidTimeError = Exception
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
# See: http://bugs.python.org/issue22767
if six.PY3:
@ -339,6 +347,7 @@ def set_many(instance, field, value):
field = getattr(instance, field)
field.set(value)
def include(module, namespace=None, app_name=None):
from django.conf.urls import include
if django.VERSION < (1,9):

View File

@ -33,7 +33,8 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import ISO_8601
from rest_framework.compat import (
get_remote_field, unicode_repr, unicode_to_repr, value_from_object
InvalidTimeError, get_remote_field, unicode_repr, unicode_to_repr,
value_from_object
)
from rest_framework.exceptions import ErrorDetail, ValidationError
from rest_framework.settings import api_settings
@ -1108,7 +1109,7 @@ class DateTimeField(Field):
if (field_timezone is not None) and not timezone.is_aware(value):
try:
return timezone.make_aware(value, field_timezone)
except Exception:
except InvalidTimeError:
self.fail('make_aware', timezone=field_timezone)
elif (field_timezone is None) and timezone.is_aware(value):
return timezone.make_naive(value, utc)

View File

@ -9,10 +9,10 @@ import pytest
from django.http import QueryDict
from django.test import TestCase, override_settings
from django.utils import six
from django.utils.timezone import pytz, utc
from django.utils.timezone import utc
import rest_framework
from rest_framework import serializers
from rest_framework import compat, serializers
from rest_framework.fields import is_simple_callable
try:
@ -1217,7 +1217,16 @@ class TestNaiveDayLightSavingTimeTimeZoneDateTimeField(FieldValues):
'2017-11-05T01:30:00': ['Invalid datetime for the timezone "America/New_York".']
}
outputs = {}
field = serializers.DateTimeField(default_timezone=pytz.timezone('America/New_York'))
class MockTimezone:
@staticmethod
def localize(value, is_dst):
raise compat.InvalidTimeError()
def __str__(self):
return 'America/New_York'
field = serializers.DateTimeField(default_timezone=MockTimezone())
class TestTimeField(FieldValues):