Skip to content

Commit

Permalink
Dropped savalidation support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrevutskyi committed Oct 14, 2023
1 parent 5beaa83 commit 4bf0082
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 146 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Changelog
Changes in Flask-Restless-NG
============================

Unreleased
-------------
- Dropped savalidation support


Version 3.1.0 (2023-10-14):
-------------
- Added support for Flask 3.0
Expand Down
19 changes: 0 additions & 19 deletions flask_restless/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,25 +594,6 @@ def extract_error_messages(exception):
# 'errors' comes from sqlalchemy_elixir_validations
if hasattr(exception, 'errors'):
return exception.errors
# 'message' comes from savalidation
if hasattr(exception, 'message'):
# TODO this works only if there is one validation error
try:
left, right = str(exception).rsplit(':', 1)
left_bracket = left.rindex('[')
right_bracket = right.rindex(']')
except ValueError as exc:
current_app.logger.exception(str(exc))
# could not parse the string; we're not trying too hard here...
return None
msg = right[:right_bracket].strip(' "')
fieldname = left[left_bracket + 1:].strip()
return {fieldname: msg}
# new savalidation
if hasattr(exception, 'invalid_instances'):
# TODO handle more than once instance
return exception.invalid_instances[0].validation_errors


def error(id_=None, links=None, status=None, code=None, title=None,
detail=None, source=None, meta=None):
Expand Down
1 change: 0 additions & 1 deletion requirements/test.txt
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

Expand Down
126 changes: 0 additions & 126 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]'

0 comments on commit 4bf0082

Please sign in to comment.