aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_serializer.py
diff options
context:
space:
mode:
authorTom Christie2014-12-13 14:58:04 +0000
committerTom Christie2014-12-13 14:58:04 +0000
commitaa571abb2089aedb5902a8f1670e59b4df99f3e9 (patch)
tree7992c626a96bafa1b9ef0e79315b3637e28f4b0d /tests/test_serializer.py
parenteb78da4b5441bec1758e7d6db86822b3ea890f5f (diff)
downloaddjango-rest-framework-aa571abb2089aedb5902a8f1670e59b4df99f3e9.tar.bz2
Better errors when serializer has incorrectly named field.
Diffstat (limited to 'tests/test_serializer.py')
-rw-r--r--tests/test_serializer.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/tests/test_serializer.py b/tests/test_serializer.py
index 6dabaf42..9d47b0aa 100644
--- a/tests/test_serializer.py
+++ b/tests/test_serializer.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals
from rest_framework import serializers
import pytest
@@ -175,3 +176,23 @@ class TestStarredSource:
instance = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
serializer = self.Serializer(instance)
assert serializer.data == self.data
+
+
+class TestIncorrectlyConfigured:
+ def test_incorrect_field_name(self):
+ class ExampleSerializer(serializers.Serializer):
+ incorrect_name = serializers.IntegerField()
+
+ class ExampleObject:
+ def __init__(self):
+ self.correct_name = 123
+
+ instance = ExampleObject()
+ serializer = ExampleSerializer(instance)
+ with pytest.raises(AttributeError) as exc_info:
+ serializer.data
+ assert str(exc_info.value) == (
+ "Got AttributeError when attempting to get a value for field `incorrect_name` on serializer `ExampleSerializer`.\n"
+ "The serializer field might be named incorrectly and not match any attribute or key on the `ExampleObject` instance.\n"
+ "Original exception text was: ExampleObject instance has no attribute 'incorrect_name'."
+ )