aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/test_field_options.py55
-rw-r--r--tests/test_fields.py (renamed from tests/test_field_values.py)67
-rw-r--r--tests/test_model_serializer.py47
3 files changed, 105 insertions, 64 deletions
diff --git a/tests/test_field_options.py b/tests/test_field_options.py
deleted file mode 100644
index 444bd424..00000000
--- a/tests/test_field_options.py
+++ /dev/null
@@ -1,55 +0,0 @@
-from rest_framework import fields
-import pytest
-
-
-class TestFieldOptions:
- def test_required(self):
- """
- By default a field must be included in the input.
- """
- field = fields.IntegerField()
- with pytest.raises(fields.ValidationError) as exc_info:
- field.run_validation()
- assert exc_info.value.messages == ['This field is required.']
-
- def test_not_required(self):
- """
- If `required=False` then a field may be omitted from the input.
- """
- field = fields.IntegerField(required=False)
- with pytest.raises(fields.SkipField):
- field.run_validation()
-
- def test_disallow_null(self):
- """
- By default `None` is not a valid input.
- """
- field = fields.IntegerField()
- with pytest.raises(fields.ValidationError) as exc_info:
- field.run_validation(None)
- assert exc_info.value.messages == ['This field may not be null.']
-
- def test_allow_null(self):
- """
- If `allow_null=True` then `None` is a valid input.
- """
- field = fields.IntegerField(allow_null=True)
- output = field.run_validation(None)
- assert output is None
-
- def test_disallow_blank(self):
- """
- By default '' is not a valid input.
- """
- field = fields.CharField()
- with pytest.raises(fields.ValidationError) as exc_info:
- field.run_validation('')
- assert exc_info.value.messages == ['This field may not be blank.']
-
- def test_allow_blank(self):
- """
- If `allow_blank=True` then '' is a valid input.
- """
- field = fields.CharField(allow_blank=True)
- output = field.run_validation('')
- assert output is ''
diff --git a/tests/test_field_values.py b/tests/test_fields.py
index bac50f0b..6bf9aed4 100644
--- a/tests/test_field_values.py
+++ b/tests/test_fields.py
@@ -6,6 +6,73 @@ import django
import pytest
+# Tests for field keyword arguments and core functionality.
+# ---------------------------------------------------------
+
+class TestFieldOptions:
+ def test_required(self):
+ """
+ By default a field must be included in the input.
+ """
+ field = fields.IntegerField()
+ with pytest.raises(fields.ValidationError) as exc_info:
+ field.run_validation()
+ assert exc_info.value.messages == ['This field is required.']
+
+ def test_not_required(self):
+ """
+ If `required=False` then a field may be omitted from the input.
+ """
+ field = fields.IntegerField(required=False)
+ with pytest.raises(fields.SkipField):
+ field.run_validation()
+
+ def test_disallow_null(self):
+ """
+ By default `None` is not a valid input.
+ """
+ field = fields.IntegerField()
+ with pytest.raises(fields.ValidationError) as exc_info:
+ field.run_validation(None)
+ assert exc_info.value.messages == ['This field may not be null.']
+
+ def test_allow_null(self):
+ """
+ If `allow_null=True` then `None` is a valid input.
+ """
+ field = fields.IntegerField(allow_null=True)
+ output = field.run_validation(None)
+ assert output is None
+
+ def test_disallow_blank(self):
+ """
+ By default '' is not a valid input.
+ """
+ field = fields.CharField()
+ with pytest.raises(fields.ValidationError) as exc_info:
+ field.run_validation('')
+ assert exc_info.value.messages == ['This field may not be blank.']
+
+ def test_allow_blank(self):
+ """
+ If `allow_blank=True` then '' is a valid input.
+ """
+ field = fields.CharField(allow_blank=True)
+ output = field.run_validation('')
+ assert output is ''
+
+ def test_default(self):
+ """
+ If `default` is set, then omitted values get the default input.
+ """
+ field = fields.IntegerField(default=123)
+ output = field.run_validation()
+ assert output is 123
+
+
+# Tests for field input and output values.
+# ----------------------------------------
+
def get_items(mapping_or_list_of_two_tuples):
# Tests accept either lists of two tuples, or dictionaries.
if isinstance(mapping_or_list_of_two_tuples, dict):
diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py
index d9f9efbe..731ed2fb 100644
--- a/tests/test_model_serializer.py
+++ b/tests/test_model_serializer.py
@@ -6,6 +6,7 @@ These tests deal with ensuring that we correctly map the model fields onto
an appropriate set of serializer fields for each case.
"""
from django.core.exceptions import ImproperlyConfigured
+from django.core.validators import MaxValueValidator, MinValueValidator, MinLengthValidator
from django.db import models
from django.test import TestCase
from rest_framework import serializers
@@ -15,7 +16,8 @@ def dedent(blocktext):
return '\n'.join([line[12:] for line in blocktext.splitlines()[1:-1]])
-# Testing regular field mappings
+# Tests for regular field mappings.
+# ---------------------------------
class CustomField(models.Field):
"""
@@ -24,9 +26,6 @@ class CustomField(models.Field):
pass
-COLOR_CHOICES = (('red', 'Red'), ('blue', 'Blue'), ('green', 'Green'))
-
-
class RegularFieldsModel(models.Model):
"""
A model class for testing regular flat fields.
@@ -35,7 +34,6 @@ class RegularFieldsModel(models.Model):
big_integer_field = models.BigIntegerField()
boolean_field = models.BooleanField(default=False)
char_field = models.CharField(max_length=100)
- choices_field = models.CharField(max_length=100, choices=COLOR_CHOICES)
comma_seperated_integer_field = models.CommaSeparatedIntegerField(max_length=100)
date_field = models.DateField()
datetime_field = models.DateTimeField()
@@ -57,6 +55,19 @@ class RegularFieldsModel(models.Model):
return 'method'
+COLOR_CHOICES = (('red', 'Red'), ('blue', 'Blue'), ('green', 'Green'))
+
+
+class FieldOptionsModel(models.Model):
+ value_limit_field = models.IntegerField(validators=[MinValueValidator(1), MaxValueValidator(10)])
+ length_limit_field = models.CharField(validators=[MinLengthValidator(3)], max_length=12)
+ blank_field = models.CharField(blank=True, max_length=10)
+ null_field = models.IntegerField(null=True)
+ default_field = models.IntegerField(default=0)
+ descriptive_field = models.IntegerField(help_text='Some help text', verbose_name='A label')
+ choices_field = models.CharField(max_length=100, choices=COLOR_CHOICES)
+
+
class TestRegularFieldMappings(TestCase):
def test_regular_fields(self):
"""
@@ -70,9 +81,8 @@ class TestRegularFieldMappings(TestCase):
TestSerializer():
auto_field = IntegerField(read_only=True)
big_integer_field = IntegerField()
- boolean_field = BooleanField(default=False)
+ boolean_field = BooleanField(required=False)
char_field = CharField(max_length=100)
- choices_field = ChoiceField(choices=[('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')])
comma_seperated_integer_field = CharField(max_length=100, validators=[<django.core.validators.RegexValidator object>])
date_field = DateField()
datetime_field = DateTimeField()
@@ -80,7 +90,7 @@ class TestRegularFieldMappings(TestCase):
email_field = EmailField(max_length=100)
float_field = FloatField()
integer_field = IntegerField()
- null_boolean_field = BooleanField(required=False)
+ null_boolean_field = BooleanField(allow_null=True)
positive_integer_field = IntegerField()
positive_small_integer_field = IntegerField()
slug_field = SlugField(max_length=100)
@@ -92,6 +102,24 @@ class TestRegularFieldMappings(TestCase):
""")
self.assertEqual(repr(TestSerializer()), expected)
+ def test_field_options(self):
+ class TestSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = FieldOptionsModel
+
+ expected = dedent("""
+ TestSerializer():
+ id = IntegerField(label='ID', read_only=True)
+ value_limit_field = IntegerField(max_value=10, min_value=1)
+ length_limit_field = CharField(max_length=12, min_length=3)
+ blank_field = CharField(allow_blank=True, max_length=10)
+ null_field = IntegerField(allow_null=True)
+ default_field = IntegerField(required=False)
+ descriptive_field = IntegerField(help_text='Some help text', label='A label')
+ choices_field = ChoiceField(choices=[('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')])
+ """)
+ self.assertEqual(repr(TestSerializer()), expected)
+
def test_method_field(self):
"""
Properties and methods on the model should be allowed as `Meta.fields`
@@ -178,7 +206,8 @@ class TestRegularFieldMappings(TestCase):
assert str(excinfo.exception) == expected
-# Testing relational field mappings
+# Tests for relational field mappings.
+# ------------------------------------
class ForeignKeyTargetModel(models.Model):
name = models.CharField(max_length=100)