Cascade story deletes to tasks and events

When a story was deleted, its tasks and events would be orphaned in
the database.  Add the delete cascade to those relationships so that
they are cleaned up automatically.

Also add a test for this.

Change-Id: I203557fb4b3a9275310eead5454b5d0090b689bb
This commit is contained in:
James E. Blair 2016-11-05 08:51:05 -07:00
parent 83e9df0629
commit bf1f9b1c9c
2 changed files with 39 additions and 5 deletions

View File

@ -313,8 +313,10 @@ class Story(FullText, ModelBuilder, Base):
description = Column(UnicodeText())
is_bug = Column(Boolean, default=True)
private = Column(Boolean, default=False)
tasks = relationship('Task', backref='story')
events = relationship('TimeLineEvent', backref='story')
tasks = relationship('Task', backref='story',
cascade="all, delete-orphan")
events = relationship('TimeLineEvent', backref='story',
cascade="all, delete-orphan")
tags = relationship('StoryTag', secondary='story_storytags')
permissions = relationship('Permission', secondary='story_permissions')

View File

@ -13,7 +13,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from storyboard.db.api import stories
from storyboard.db.api import stories as stories_api
from storyboard.db.api import tasks as tasks_api
from storyboard.db.api import timeline_events as events_api
from storyboard.tests.db import base
@ -28,7 +30,7 @@ class StoriesTest(base.BaseDbTestCase):
}
def test_create_story(self):
self._test_create(self.story_01, stories.story_create)
self._test_create(self.story_01, stories_api.story_create)
def test_update_story(self):
delta = {
@ -36,4 +38,34 @@ class StoriesTest(base.BaseDbTestCase):
'description': u'New Description'
}
self._test_update(self.story_01, delta,
stories.story_create, stories.story_update)
stories_api.story_create, stories_api.story_update)
def test_delete_story(self):
# This test uses mock_data
story_id = 1
# Verify that we can look up a story with tasks and events
story = stories_api.story_get_simple(story_id)
self.assertIsNotNone(story)
tasks = tasks_api.task_get_all(story_id=story_id)
self.assertEqual(len(tasks), 3)
task_ids = [t.id for t in tasks]
events = events_api.events_get_all(story_id=story_id)
self.assertEqual(len(events), 3)
event_ids = [e.id for e in events]
# Delete the story
stories_api.story_delete(story_id)
story = stories_api.story_get_simple(story_id)
self.assertIsNone(story)
# Verify that the story's tasks were deleted
tasks = tasks_api.task_get_all(story_id=story_id)
self.assertEqual(len(tasks), 0)
for tid in task_ids:
task = tasks_api.task_get(task_id=tid)
self.assertIsNone(task)
# And the events
events = events_api.events_get_all(story_id=story_id)
self.assertEqual(len(events), 0)
for eid in event_ids:
event = events_api.event_get(event_id=eid)
self.assertIsNone(event)