Skip to content

Commit

Permalink
Adds test for deleting from polymorphic models.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfinkels committed May 17, 2016
1 parent d3cb7f3 commit 0719228
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion flask_restless/views/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ def delete(self, resource_id):
instance = get_by(self.session, self.model, resource_id,
self.primary_key)
if instance is None:
detail = 'No resource found with ID {0}'.format(resource_id)
detail = 'No resource found with type {0} and ID {1}'
detail = detail.format(collection_name(self.model), resource_id)
return error_response(404, detail=detail)
self.session.delete(instance)
was_deleted = len(self.session.deleted) > 0
Expand Down
87 changes: 87 additions & 0 deletions tests/test_deleting.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from unittest2 import skip

from sqlalchemy import Column
from sqlalchemy import Enum
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import Unicode
Expand All @@ -29,6 +30,7 @@
from flask.ext.restless import APIManager
from flask.ext.restless import ProcessingException

from .helpers import check_sole_error
from .helpers import dumps
from .helpers import loads
from .helpers import FlaskSQLAlchemyTestBase
Expand Down Expand Up @@ -267,6 +269,91 @@ def assert_deletion(was_deleted=False, **kw):
assert response.status_code == 204


class TestSingleTableInheritance(ManagerTestBase):
"""Tests for APIs created for polymorphic models defined using
single table inheritance.
"""

def setUp(self):
super(TestSingleTableInheritance, self).setUp()

class Employee(self.Base):
__tablename__ = 'employee'
id = Column(Integer, primary_key=True)
type = Column(Enum('employee', 'manager'), nullable=False)
__mapper_args__ = {
'polymorphic_on': type,
'polymorphic_identity': 'employee'
}

# This model inherits directly from the `Employee` class, so
# there is only one table here.
class Manager(Employee):
__mapper_args__ = {
'polymorphic_identity': 'manager'
}

self.Employee = Employee
self.Manager = Manager
self.Base.metadata.create_all()
self.manager.create_api(Employee, methods=['DELETE'])
self.manager.create_api(Manager, methods=['DELETE'])

def test_subclass_at_subclass(self):
"""Tests for deleting a resource of the subclass type at the URL
for the subclass.
"""
manager = self.Manager(id=1)
self.session.add(manager)
self.session.commit()
response = self.app.delete('/api/manager/1')
assert response.status_code == 204
assert self.session.query(self.Manager).count() == 0
assert self.session.query(self.Employee).count() == 0

def test_subclass_at_superclass(self):
"""Tests for deleting a resource of the subclass type at the URL
for the superclass.
"""
manager = self.Manager(id=1)
self.session.add(manager)
self.session.commit()
response = self.app.delete('/api/employee/1')
assert response.status_code == 204
assert self.session.query(self.Manager).count() == 0
assert self.session.query(self.Employee).count() == 0

def test_superclass_at_superclass(self):
"""Tests for deleting a resource of the superclass type at the
URL for the superclass.
"""
employee = self.Employee(id=1)
self.session.add(employee)
self.session.commit()
response = self.app.delete('/api/employee/1')
assert response.status_code == 204
assert self.session.query(self.Manager).count() == 0
assert self.session.query(self.Employee).count() == 0

def test_superclass_at_subclass(self):
"""Tests that attempting to delete a resource of the superclass
type at the URL for the subclass causes an error.
"""
employee = self.Employee(id=1)
self.session.add(employee)
self.session.commit()
response = self.app.delete('/api/manager/1')
check_sole_error(response, 404, ['No resource found', 'type',
'manager', 'ID', '1'])
assert self.session.query(self.Manager).count() == 0
assert self.session.query(self.Employee).first() is employee


class TestFlaskSQLAlchemy(FlaskSQLAlchemyTestBase):
"""Tests for deleting resources defined as Flask-SQLAlchemy models instead
of pure SQLAlchemy models.
Expand Down

0 comments on commit 0719228

Please sign in to comment.