aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_authentication.py2
-rw-r--r--tests/test_description.py7
-rw-r--r--tests/test_fields.py15
-rw-r--r--tests/test_model_serializer.py50
-rw-r--r--tests/test_multitable_inheritance.py2
-rw-r--r--tests/test_parsers.py2
-rw-r--r--tests/test_relations_generic.py2
-rw-r--r--tests/test_renderers.py13
-rw-r--r--tests/test_request.py2
-rw-r--r--tests/test_serializer_lists.py16
-rw-r--r--tests/test_validators.py2
11 files changed, 102 insertions, 11 deletions
diff --git a/tests/test_authentication.py b/tests/test_authentication.py
index 28c3a8b3..44837c4e 100644
--- a/tests/test_authentication.py
+++ b/tests/test_authentication.py
@@ -142,7 +142,7 @@ class SessionAuthTests(TestCase):
cf. [#1810](https://github.com/tomchristie/django-rest-framework/pull/1810)
"""
response = self.csrf_client.get('/auth/login/')
- self.assertContains(response, '<label class="span4">Username:</label>')
+ self.assertContains(response, '<label for="id_username">Username:</label>')
def test_post_form_session_auth_failing_csrf(self):
"""
diff --git a/tests/test_description.py b/tests/test_description.py
index 0675d209..78ce2350 100644
--- a/tests/test_description.py
+++ b/tests/test_description.py
@@ -2,7 +2,8 @@
from __future__ import unicode_literals
from django.test import TestCase
-from rest_framework.compat import apply_markdown, smart_text
+from django.utils.encoding import python_2_unicode_compatible, smart_text
+from rest_framework.compat import apply_markdown
from rest_framework.views import APIView
from .description import ViewWithNonASCIICharactersInDocstring
from .description import UTF8_TEST_DOCSTRING
@@ -107,6 +108,7 @@ class TestViewNamesAndDescriptions(TestCase):
"""
# use a mock object instead of gettext_lazy to ensure that we can't end
# up with a test case string in our l10n catalog
+ @python_2_unicode_compatible
class MockLazyStr(object):
def __init__(self, string):
self.s = string
@@ -114,9 +116,6 @@ class TestViewNamesAndDescriptions(TestCase):
def __str__(self):
return self.s
- def __unicode__(self):
- return self.s
-
class MockView(APIView):
__doc__ = MockLazyStr("a gettext string")
diff --git a/tests/test_fields.py b/tests/test_fields.py
index 13525632..3f4e65f2 100644
--- a/tests/test_fields.py
+++ b/tests/test_fields.py
@@ -804,6 +804,21 @@ class TestChoiceField(FieldValues):
]
)
+ def test_allow_blank(self):
+ """
+ If `allow_blank=True` then '' is a valid input.
+ """
+ field = serializers.ChoiceField(
+ allow_blank=True,
+ choices=[
+ ('poor', 'Poor quality'),
+ ('medium', 'Medium quality'),
+ ('good', 'Good quality'),
+ ]
+ )
+ output = field.run_validation('')
+ assert output is ''
+
class TestChoiceFieldWithType(FieldValues):
"""
diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py
index 1bcd58e0..da79164a 100644
--- a/tests/test_model_serializer.py
+++ b/tests/test_model_serializer.py
@@ -559,3 +559,53 @@ class TestBulkCreate(TestCase):
# Serializer returns correct data.
assert serializer.data == data
+
+
+class TestMetaClassModel(models.Model):
+ text = models.CharField(max_length=100)
+
+
+class TestSerializerMetaClass(TestCase):
+ def test_meta_class_fields_option(self):
+ class ExampleSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = TestMetaClassModel
+ fields = 'text'
+
+ with self.assertRaises(TypeError) as result:
+ ExampleSerializer().fields
+
+ exception = result.exception
+ assert str(exception).startswith(
+ "The `fields` option must be a list or tuple"
+ )
+
+ def test_meta_class_exclude_option(self):
+ class ExampleSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = TestMetaClassModel
+ exclude = 'text'
+
+ with self.assertRaises(TypeError) as result:
+ ExampleSerializer().fields
+
+ exception = result.exception
+ assert str(exception).startswith(
+ "The `exclude` option must be a list or tuple"
+ )
+
+ def test_meta_class_fields_and_exclude_options(self):
+ class ExampleSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = TestMetaClassModel
+ fields = ('text',)
+ exclude = ('text',)
+
+ with self.assertRaises(AssertionError) as result:
+ ExampleSerializer().fields
+
+ exception = result.exception
+ self.assertEqual(
+ str(exception),
+ "Cannot set both 'fields' and 'exclude'."
+ )
diff --git a/tests/test_multitable_inheritance.py b/tests/test_multitable_inheritance.py
index ce1bf3ea..e1b40cc7 100644
--- a/tests/test_multitable_inheritance.py
+++ b/tests/test_multitable_inheritance.py
@@ -31,7 +31,7 @@ class AssociatedModelSerializer(serializers.ModelSerializer):
# Tests
-class IneritedModelSerializationTests(TestCase):
+class InheritedModelSerializationTests(TestCase):
def test_multitable_inherited_model_fields_as_expected(self):
"""
diff --git a/tests/test_parsers.py b/tests/test_parsers.py
index 88eccef3..d28d8bd4 100644
--- a/tests/test_parsers.py
+++ b/tests/test_parsers.py
@@ -5,8 +5,8 @@ from django import forms
from django.core.files.uploadhandler import MemoryFileUploadHandler
from django.test import TestCase
from django.utils import unittest
+from django.utils.six.moves import StringIO
from rest_framework.compat import etree
-from rest_framework.compat import StringIO
from rest_framework.exceptions import ParseError
from rest_framework.parsers import FormParser, FileUploadParser
from rest_framework.parsers import XMLParser
diff --git a/tests/test_relations_generic.py b/tests/test_relations_generic.py
index 380ad91d..b600b333 100644
--- a/tests/test_relations_generic.py
+++ b/tests/test_relations_generic.py
@@ -3,8 +3,8 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey
from django.db import models
from django.test import TestCase
+from django.utils.encoding import python_2_unicode_compatible
from rest_framework import serializers
-from rest_framework.compat import python_2_unicode_compatible
@python_2_unicode_compatible
diff --git a/tests/test_renderers.py b/tests/test_renderers.py
index 416d7f22..00a24fb1 100644
--- a/tests/test_renderers.py
+++ b/tests/test_renderers.py
@@ -7,9 +7,11 @@ from django.core.cache import cache
from django.db import models
from django.test import TestCase
from django.utils import six, unittest
+from django.utils.six import BytesIO
+from django.utils.six.moves import StringIO
from django.utils.translation import ugettext_lazy as _
from rest_framework import status, permissions
-from rest_framework.compat import yaml, etree, StringIO, BytesIO
+from rest_framework.compat import yaml, etree
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \
@@ -384,6 +386,15 @@ class UnicodeJSONRendererTests(TestCase):
content = renderer.render(obj, 'application/json')
self.assertEqual(content, '{"countries":["United Kingdom","France","EspaƱa"]}'.encode('utf-8'))
+ def test_u2028_u2029(self):
+ # The \u2028 and \u2029 characters should be escaped,
+ # even when the non-escaping unicode representation is used.
+ # Regression test for #2169
+ obj = {'should_escape': '\u2028\u2029'}
+ renderer = JSONRenderer()
+ content = renderer.render(obj, 'application/json')
+ self.assertEqual(content, '{"should_escape":"\\u2028\\u2029"}'.encode('utf-8'))
+
class AsciiJSONRendererTests(TestCase):
"""
diff --git a/tests/test_request.py b/tests/test_request.py
index 44afd243..7cf8c327 100644
--- a/tests/test_request.py
+++ b/tests/test_request.py
@@ -187,7 +187,7 @@ class MockView(APIView):
if request.POST.get('example') is not None:
return Response(status=status.HTTP_200_OK)
- return Response(status=status.INTERNAL_SERVER_ERROR)
+ return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)
urlpatterns = patterns(
'',
diff --git a/tests/test_serializer_lists.py b/tests/test_serializer_lists.py
index 640067e3..35b68ae7 100644
--- a/tests/test_serializer_lists.py
+++ b/tests/test_serializer_lists.py
@@ -272,3 +272,19 @@ class TestNestedListOfListsSerializer:
serializer = self.Serializer(data=input_data)
assert serializer.is_valid()
assert serializer.validated_data == expected_output
+
+
+class TestListSerializerClass:
+ """Tests for a custom list_serializer_class."""
+ def test_list_serializer_class_validate(self):
+ class CustomListSerializer(serializers.ListSerializer):
+ def validate(self, attrs):
+ raise serializers.ValidationError('Non field error')
+
+ class TestSerializer(serializers.Serializer):
+ class Meta:
+ list_serializer_class = CustomListSerializer
+
+ serializer = TestSerializer(data=[], many=True)
+ assert not serializer.is_valid()
+ assert serializer.errors == {'non_field_errors': ['Non field error']}
diff --git a/tests/test_validators.py b/tests/test_validators.py
index 9226cc7a..072cec36 100644
--- a/tests/test_validators.py
+++ b/tests/test_validators.py
@@ -168,7 +168,7 @@ class TestUniquenessTogetherValidation(TestCase):
def test_ignore_excluded_fields(self):
"""
When model fields are not included in a serializer, then uniqueness
- validtors should not be added for that field.
+ validators should not be added for that field.
"""
class ExcludedFieldSerializer(serializers.ModelSerializer):
class Meta: