-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Cadasta/default-none
Allow empty strings for non-required fields
- Loading branch information
Showing
2 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import pytest | ||
from django.test import TestCase | ||
from django.core.exceptions import ValidationError | ||
from jsonattrs.models import Schema, Attribute, AttributeType | ||
|
||
from .fixtures import create_fixtures | ||
|
||
|
||
class ValidationTest(TestCase): | ||
def setUp(self): | ||
self.fixtures = create_fixtures(do_schemas=False, load_attr_types=True) | ||
self.schema = Schema.objects.create( | ||
content_type=self.fixtures['party_t'], selectors=() | ||
) | ||
|
||
def test_validate_empty_integer(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='integer') | ||
) | ||
|
||
attr.validate('') | ||
# No assertion here, validation should pass without exceptions | ||
|
||
def test_validate_empty_integer_on_required_attribute(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='integer'), | ||
required=True | ||
) | ||
with pytest.raises(ValidationError): | ||
attr.validate('') | ||
|
||
def test_validate_empty_decimal(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='decimal') | ||
) | ||
|
||
attr.validate('') | ||
# No assertion here, validation should pass without exceptions | ||
|
||
def test_validate_empty_decimal_on_required_attribute(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='decimal'), | ||
required=True | ||
) | ||
with pytest.raises(ValidationError): | ||
attr.validate('') | ||
|
||
def test_validate_empty_select_one(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='select_one'), | ||
choices=['a', 'b', 'c'] | ||
) | ||
|
||
attr.validate('') | ||
attr.validate(['']) | ||
# No assertion here, validation should pass without exceptions | ||
|
||
def test_validate_empty_select_one_on_required_attribute(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='select_one'), | ||
choices=['a', 'b', 'c'], | ||
required=True | ||
) | ||
with pytest.raises(ValidationError): | ||
attr.validate('') | ||
with pytest.raises(ValidationError): | ||
attr.validate(['']) | ||
|
||
def test_validate_empty_select_multiple(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='select_multiple'), | ||
choices=['a', 'b', 'c'] | ||
) | ||
|
||
attr.validate('') | ||
attr.validate(['']) | ||
# No assertion here, validation should pass without exceptions | ||
|
||
def test_validate_empty_select_multiple_on_required_attribute(self): | ||
attr = Attribute.objects.create( | ||
schema=self.schema, | ||
name='testattr', | ||
long_name='Test attribute', | ||
index=1, | ||
attr_type=AttributeType.objects.get(name='select_multiple'), | ||
choices=['a', 'b', 'c'], | ||
required=True | ||
) | ||
|
||
with pytest.raises(ValidationError): | ||
attr.validate('') | ||
with pytest.raises(ValidationError): | ||
attr.validate(['']) |