From 1b4c078db2e0d23dbb970fbf5790db843377dc11 Mon Sep 17 00:00:00 2001 From: Yuri Prezument Date: Thu, 21 Feb 2013 12:15:31 +0200 Subject: [PATCH 1/3] Failing test case for #669 --- rest_framework/tests/serializer.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index da1101386..24a2d6447 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -338,6 +338,21 @@ class ValidationTests(TestCase): self.assertEquals(serializer.is_valid(), False) self.assertEquals(serializer.errors, {'info': ['Ensure this value has at most 12 characters (it has 13).']}) + def test_datetime_validation_failure(self): + """ + Test DateTimeField validation errors on non-str values. + Regression test for #669. + + https://github.com/tomchristie/django-rest-framework/issues/669 + """ + data = self.data + data['created'] = 0 + + serializer = CommentSerializer(data=data) + self.assertEquals(serializer.is_valid(), False) + + # TODO: check validation error value + class CustomValidationTests(TestCase): class CommentSerializerWithFieldValidator(CommentSerializer): From 8fa82822a07854a7036bfae84dfa9db3b4a0a61c Mon Sep 17 00:00:00 2001 From: Yuri Prezument Date: Thu, 21 Feb 2013 12:35:24 +0200 Subject: [PATCH 2/3] Add assertion in test --- rest_framework/tests/serializer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 24a2d6447..e85323e0c 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -351,7 +351,7 @@ class ValidationTests(TestCase): serializer = CommentSerializer(data=data) self.assertEquals(serializer.is_valid(), False) - # TODO: check validation error value + self.assertIn('created', serializer.errors) class CustomValidationTests(TestCase): From 5f531fc1ea0b6417b162c5cce7cd8193a1231019 Mon Sep 17 00:00:00 2001 From: Yuri Prezument Date: Thu, 21 Feb 2013 15:36:17 +0200 Subject: [PATCH 3/3] Catch TypeError as well as ValueError when parsing dates, ref #699 --- rest_framework/fields.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rest_framework/fields.py b/rest_framework/fields.py index e1fd1b646..86c3a837a 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -472,7 +472,7 @@ class DateField(WritableField): parsed = parse_date(value) if parsed is not None: return parsed - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid_date'] % value raise ValidationError(msg) @@ -520,7 +520,7 @@ class DateTimeField(WritableField): parsed = parse_datetime(value) if parsed is not None: return parsed - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid_datetime'] % value raise ValidationError(msg) @@ -528,7 +528,7 @@ class DateTimeField(WritableField): parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid_date'] % value raise ValidationError(msg) @@ -558,7 +558,7 @@ class TimeField(WritableField): parsed = parse_time(value) assert parsed is not None return parsed - except ValueError: + except (ValueError, TypeError): msg = self.error_messages['invalid'] % value raise ValidationError(msg)