Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify post-test cleanup for model mapping unit tests; fix a few errors #12714

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
exclude = ['HistoryAudit', 'JobStateHistory']
models = (cls_ for cls_ in model.__dict__.values()
if hasattr(cls_, '__mapper__') and cls_.__name__ not in exclude)
# 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
25 changes: 25 additions & 0 deletions test/unit/shed_unit/model/test_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pytest
from sqlalchemy import (
delete,
func,
select,
UniqueConstraint,
)
Expand Down Expand Up @@ -529,6 +530,8 @@ def test_relationships(
assert stored_obj.repository.id == repository.id
assert are_same_entity_collections(stored_obj.reviews, [review1, review2])

delete_from_database(session, [review1, review2, review3])


class TestRepositoryRatingAssociation(BaseTest):

Expand Down Expand Up @@ -746,6 +749,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 @@ -766,6 +771,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 TestUser(BaseTest):

Expand Down Expand Up @@ -852,6 +859,8 @@ def test_relationships(
assert are_same_entity_collections(stored_obj.roles, [private_user_role, non_private_user_role])
assert stored_obj.non_private_roles == [non_private_user_role]

delete_from_database(session, [_private_role, _non_private_role, private_user_role, non_private_user_role])


class TestUserGroupAssociation(BaseTest):

Expand Down Expand Up @@ -912,6 +921,22 @@ def test_relationships(self, session, cls_, user, role):


# 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
"""
models = (cls_ for cls_ in model.__dict__.values() if hasattr(cls_, '__mapper__'))
# 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