Drop unused mapping testing utilities and fixtures

This commit is contained in:
John Davis
2024-05-02 14:12:35 -04:00
parent fa2afe80ff
commit 76c8003d4f
4 changed files with 0 additions and 1234 deletions
File diff suppressed because it is too large Load Diff
@@ -1,66 +0,0 @@
from abc import (
ABC,
abstractmethod,
)
from uuid import uuid4
import pytest
from sqlalchemy import UniqueConstraint
class AbstractBaseTest(ABC):
@pytest.fixture
def cls_(self):
"""
Return class under test.
Assumptions: if the class under test is Foo, then the class grouping
the tests should be a subclass of BaseTest, named TestFoo.
"""
prefix = len("Test")
class_name = self.__class__.__name__[prefix:]
return getattr(self.get_model(), class_name)
@abstractmethod
def get_model(self):
pass
def has_unique_constraint(table, fields):
for constraint in table.constraints:
if isinstance(constraint, UniqueConstraint):
col_names = {c.name for c in constraint.columns}
if set(fields) == col_names:
return True
def has_index(table, fields):
for index in table.indexes:
col_names = {c.name for c in index.columns}
if set(fields) == col_names:
return True
def collection_consists_of_objects(collection, *objects):
"""
Returns True iff list(collection) == list(objects), where object equality is determined
by primary key equality: object1.id == object2.id.
"""
if len(collection) != len(objects): # False if lengths are different
return False
if not collection: # True if both are empty
return True
# Sort, then compare each member by its 'id' attribute, which must be its primary key.
collection.sort(key=lambda item: item.id)
objects_l = list(objects)
objects_l.sort(key=lambda item: item.id)
for item1, item2 in zip(collection, objects_l):
if item1.id is None or item2.id is None or item1.id != item2.id:
return False
return True
def get_unique_value():
"""Generate unique values to accommodate unique constraints."""
return uuid4().hex
@@ -1,68 +0,0 @@
import pytest
from galaxy.model import tool_shed_install as model
from galaxy.model.unittest_utils.model_testing_utils import (
dbcleanup_wrapper,
initialize_model,
)
@pytest.fixture(scope="module")
def init_model(engine):
"""Create model objects in the engine's database."""
# Must use the same engine as the session fixture used by this module.
initialize_model(model.mapper_registry, engine)
# Fixtures yielding persisted instances of models, deleted from the database on test exit.
@pytest.fixture
def repository(session):
instance = model.ToolShedRepository()
yield from dbcleanup_wrapper(session, instance)
@pytest.fixture
def repository_repository_dependency_association(session):
instance = model.RepositoryRepositoryDependencyAssociation()
yield from dbcleanup_wrapper(session, instance)
@pytest.fixture
def repository_dependency(session, repository):
instance = model.RepositoryDependency(repository.id)
yield from dbcleanup_wrapper(session, instance)
@pytest.fixture
def tool_dependency(session, repository):
instance = model.ToolDependency()
instance.tool_shed_repository = repository
instance.status = "a"
yield from dbcleanup_wrapper(session, instance)
@pytest.fixture
def tool_version(session):
instance = model.ToolVersion()
yield from dbcleanup_wrapper(session, instance)
# Fixtures yielding factory functions.
@pytest.fixture
def tool_version_association_factory():
def make_instance(*args, **kwds):
return model.ToolVersionAssociation(*args, **kwds)
return make_instance
@pytest.fixture
def tool_version_factory():
def make_instance(*args, **kwds):
return model.ToolVersion(*args, **kwds)
return make_instance
@@ -1,100 +0,0 @@
"""
This module contains tests for the utility functions in the test_mapping module.
"""
import pytest
from sqlalchemy import (
Column,
Index,
Integer,
UniqueConstraint,
)
from sqlalchemy.orm import registry
from galaxy.model import _HasTable
from galaxy.model.unittest_utils.mapping_testing_utils import (
collection_consists_of_objects,
has_index,
has_unique_constraint,
)
from galaxy.model.unittest_utils.model_testing_utils import (
get_stored_instance_by_id,
initialize_model,
persist,
)
def test_has_index(session):
assert has_index(Bar.__table__, ("field1",))
assert not has_index(Foo.__table__, ("field1",))
def test_has_unique_constraint(session):
assert has_unique_constraint(Bar.__table__, ("field2",))
assert not has_unique_constraint(Foo.__table__, ("field1",))
def test_collection_consists_of_objects(session):
# create objects
foo1 = Foo()
foo2 = Foo()
foo3 = Foo()
# store objects
persist(session, foo1)
persist(session, foo2)
persist(session, foo3)
# retrieve objects from storage
stored_foo1 = get_stored_instance_by_id(session, Foo, foo1.id)
stored_foo2 = get_stored_instance_by_id(session, Foo, foo2.id)
stored_foo3 = get_stored_instance_by_id(session, Foo, foo3.id)
# verify retrieved objects are not the same python objects as those we stored
assert stored_foo1 is not foo1
assert stored_foo2 is not foo2
assert stored_foo3 is not foo3
# trivial case
assert collection_consists_of_objects([stored_foo1, stored_foo2], foo1, foo2)
# empty collection and no objects
assert collection_consists_of_objects([])
# ordering in collection does not matter
assert collection_consists_of_objects([stored_foo2, stored_foo1], foo1, foo2)
# contains wrong object
assert not collection_consists_of_objects([stored_foo1, stored_foo3], foo1, foo2)
# contains wrong number of objects
assert not collection_consists_of_objects([stored_foo1, stored_foo1, stored_foo2], foo1, foo2)
# if an object's primary key is not set, it cannot be equal to another object
foo1.id, stored_foo1.id = None, None # type:ignore[assignment]
assert not collection_consists_of_objects([stored_foo1], foo1)
# Test utilities
mapper_registry = registry()
@mapper_registry.mapped
class Foo(_HasTable):
__tablename__ = "foo"
id = Column(Integer, primary_key=True)
field1 = Column(Integer)
@mapper_registry.mapped
class Bar(_HasTable):
__tablename__ = "bar"
id = Column(Integer, primary_key=True)
field1 = Column(Integer)
field2 = Column(Integer)
__table_args__ = (
Index("ix", "field1"),
UniqueConstraint("field2"),
)
@pytest.fixture(scope="module")
def init_model(engine):
"""Create model objects in the engine's database."""
# Must use the same engine as the session fixture used by this module.
initialize_model(mapper_registry, engine)