aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_field_options.py
diff options
context:
space:
mode:
authorTom Christie2014-09-22 17:46:02 +0100
committerTom Christie2014-09-22 17:46:02 +0100
commit5d80f7f932bfcc0630ac0fdbf07072a53197b98f (patch)
tree1da23b1314ddd3d22dab5721148765284716bff7 /tests/test_field_options.py
parent5a95baf2a2258fb5297062ac18582129c05fb320 (diff)
downloaddjango-rest-framework-5d80f7f932bfcc0630ac0fdbf07072a53197b98f.tar.bz2
allow_blank, allow_null
Diffstat (limited to 'tests/test_field_options.py')
-rw-r--r--tests/test_field_options.py55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/test_field_options.py b/tests/test_field_options.py
new file mode 100644
index 00000000..444bd424
--- /dev/null
+++ b/tests/test_field_options.py
@@ -0,0 +1,55 @@
+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 ''