forked from jfinkels/flask-restless
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5beaa83
commit 4bf0082
Showing
4 changed files
with
5 additions
and
146 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
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
-r dev.txt | ||
jsonschema | ||
savalidation | ||
|
||
PyMySQL # required after Flask_SQLAlchemy 3.0 | ||
|
||
|
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 |
---|---|---|
|
@@ -31,15 +31,6 @@ | |
from .helpers import ManagerTestBase | ||
from .helpers import check_sole_error | ||
|
||
# for SAValidation package on pypi.python.org | ||
try: | ||
import savalidation as _sav | ||
import savalidation.validators as sav | ||
except ImportError: | ||
has_savalidation = False | ||
else: | ||
has_savalidation = True | ||
|
||
|
||
class CoolValidationError(Exception): | ||
"""Raised when there is a validation error. | ||
|
@@ -249,120 +240,3 @@ def test_updating_relationship_invalid(self): | |
assert 'empty title not allowed' in error['detail'].lower() | ||
# Check that the relationship was not updated. | ||
assert person.articles == [] | ||
|
||
|
||
@unittest.skipUnless(has_savalidation, 'savalidation not found.') | ||
class TestSAValidation(ManagerTestBase): | ||
"""Tests for validation errors raised by the ``savalidation`` package. For | ||
more information about this package, see `its PyPI page | ||
<https://pypi.python.org/pypi/SAValidation>`_. | ||
""" | ||
|
||
def setUp(self): | ||
"""Create APIs for the validated models.""" | ||
super(TestSAValidation, self).setUp() | ||
|
||
class Person(self.Base, _sav.ValidationMixin): | ||
__tablename__ = 'person' | ||
id = Column(Integer, primary_key=True) | ||
email = Column(Unicode) | ||
|
||
sav.validates_presence_of('email') | ||
sav.validates_email('email') | ||
|
||
self.Person = Person | ||
self.Base.metadata.create_all(bind=self.engine) | ||
exceptions = [_sav.ValidationError] | ||
self.manager.create_api(Person, methods=['POST', 'PATCH'], | ||
validation_exceptions=exceptions) | ||
|
||
def test_create_valid(self): | ||
"""Tests that an attempt to create a valid resource yields no error | ||
response. | ||
""" | ||
data = dict(data=dict(type='person', email=u'[email protected]')) | ||
response = self.app.post('/api/person', json=data) | ||
assert response.status_code == 201 | ||
document = response.json | ||
person = document['data'] | ||
assert person['attributes']['email'] == u'[email protected]' | ||
|
||
def test_create_absent(self): | ||
"""Tests that an attempt to create a resource with a missing required | ||
attribute yields an error response. | ||
""" | ||
data = dict(data=dict(type='person')) | ||
response = self.app.post('/api/person', json=data) | ||
assert response.status_code == 400 | ||
document = response.json | ||
errors = document['errors'] | ||
error = errors[0] | ||
assert 'validation' in error['title'].lower() | ||
assert 'email' in error['detail'].lower() | ||
# Check that the person was not created. | ||
assert self.session.query(self.Person).count() == 0 | ||
|
||
def test_create_invalid(self): | ||
"""Tests that an attempt to create an invalid resource yields an error | ||
response. | ||
""" | ||
data = {'data': | ||
{'type': 'person', | ||
'attributes': {'email': 'bogus'} | ||
} | ||
} | ||
response = self.app.post('/api/person', json=data) | ||
assert response.status_code == 400 | ||
document = response.json | ||
errors = document['errors'] | ||
error = errors[0] | ||
assert 'validation' in error['title'].lower() | ||
assert 'email' in error['detail'].lower() | ||
# Check that the person was not created. | ||
assert self.session.query(self.Person).count() == 0 | ||
|
||
def test_update_valid(self): | ||
"""Tests that an attempt to update a resource with valid data yields no | ||
error response. | ||
""" | ||
person = self.Person(id=1, email=u'[email protected]') | ||
self.session.add(person) | ||
self.session.commit() | ||
data = {'data': | ||
{'id': '1', | ||
'type': 'person', | ||
'attributes': {'email': u'[email protected]'} | ||
} | ||
} | ||
response = self.app.patch('/api/person/1', json=data) | ||
assert response.status_code == 204 | ||
assert person.email == u'[email protected]' | ||
|
||
def test_update_invalid(self): | ||
"""Tests that an attempt to update a resource with invalid data yields | ||
an error response. | ||
""" | ||
person = self.Person(id=1, email=u'[email protected]') | ||
self.session.add(person) | ||
self.session.commit() | ||
data = {'data': | ||
{'id': '1', | ||
'type': 'person', | ||
'attributes': {'email': 'bogus'} | ||
} | ||
} | ||
response = self.app.patch('/api/person/1', json=data) | ||
assert response.status_code == 400 | ||
document = response.json | ||
errors = document['errors'] | ||
error = errors[0] | ||
assert 'validation' in error['title'].lower() | ||
assert 'email' in error['detail'].lower() | ||
# Check that the person was not updated. | ||
assert person.email == u'[email protected]' |