Skip to content

Commit

Permalink
Verify post-test cleanup in main model mapping tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jdavcs committed Oct 14, 2021
1 parent c405dba commit cef3706
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion test/unit/data/model/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def test_relationships(self, session, cls_, star, satellite): # satellite is a
import pytest
from sqlalchemy import (
delete,
func,
select,
UniqueConstraint,
)
Expand Down Expand Up @@ -4537,6 +4538,8 @@ def test_columns(self, session, cls_):
assert stored_obj.parent_id == parent_tag.id
assert stored_obj.name == name

delete_from_database(session, parent_tag)

def test_relationships(
self,
session,
Expand All @@ -4557,6 +4560,8 @@ def add_association(assoc_object, assoc_attribute):
assert stored_obj.parent.id == parent_tag.id
assert stored_obj.children == [child_tag]

delete_from_database(session, [parent_tag, child_tag])


class TestTask(BaseTest):

Expand Down Expand Up @@ -4775,8 +4780,12 @@ def test_relationships(
stored_workflow,
stored_workflow_menu_entry_factory,
):
cleanup = []

history1 = history_factory(deleted=False)
cleanup.append(history1)
history2 = history_factory(deleted=True)
cleanup.append(history2)

obj = cls_()

Expand All @@ -4796,14 +4805,24 @@ def test_relationships(
obj.social_auth.append(user_authnz_token)

_private_role = role_factory(name=obj.email)
cleanup.append(_private_role)

private_user_role = user_role_association_factory(obj, _private_role)
cleanup.append(private_user_role)

obj.roles.append(private_user_role)

_non_private_role = role_factory(name='a')
cleanup.append(_non_private_role)

non_private_user_role = user_role_association_factory(obj, _non_private_role)
cleanup.append(non_private_user_role)

obj.roles.append(non_private_user_role)

swme = stored_workflow_menu_entry_factory()
cleanup.append(swme)

swme.stored_workflow = stored_workflow
swme.user = obj

Expand Down Expand Up @@ -4835,7 +4854,7 @@ def test_relationships(
assert stored_obj.non_private_roles == [non_private_user_role]
assert stored_obj.stored_workflows == [stored_workflow]

delete_from_database(session, [history1, history2, swme, private_user_role, non_private_user_role])
delete_from_database(session, cleanup)


class TestUserAction(BaseTest):
Expand Down Expand Up @@ -6386,6 +6405,25 @@ def test_relationships(self, session, cls_, workflow_step, tag, user):


# Misc. helper fixtures.
@pytest.fixture(autouse=True)
def ensure_database_is_empty(session, model):
"""
Auto-runs before each test and any unscoped fixtures, except session and model on
which it depends. Verifies that all model tables in the database are empty. This
ensures that a test is not affected by data leftover from a previous test run.
For fixture instantiation order, see:
https://docs.pytest.org/en/6.2.x/fixture.html#fixture-instantiation-order
"""
# Created indirectrly (via db trigger and at Job instantiation): can't cleanup up automatically
exceptions = ['HistoryAudit', 'JobStateHistory']
models = (cls_ for cls_ in model.__dict__.values()
if hasattr(cls_, '__mapper__') and cls_.__name__ not in exceptions)
# For each mapped class, check that the database table to which it is mapped is empty
for m in models:
stmt = select(func.count()).select_from(m)
result = session.execute(stmt).scalar()
assert result == 0


@pytest.fixture(scope='module')
def model():
Expand Down

0 comments on commit cef3706

Please sign in to comment.