Problem summary:
In an application with wrapper methods over SQLAlchemy add()
and query()
methods, can integration tests that use the add()
method wrapper use the query()
method wrapper to validate that database table attributes and relationships are properly saved?
Detailed problem description:
We have a python application that manages a SQLite database via SQLAlchemy.
Besides the database model classes (e.g. Organization
, Team
, Member
), the data layer implementation includes the following two classes:
Database
: wraps the SQLAlchemy session management methodsRepository
: wraps the SQLAlchemy CRUD operations and adds custom logic to before-insert, before-update, and before-delete events.
Given my lack of experience in testing applications like this, I would like to validate the following ideas:
Unit tests would ensure that each feature (method) of the
Database
andRepository
classes work as expected. One example of a unit test, here, would assert that theRepository
class can write an entity (e.g.Organization
) to the database correctly. In order to do that assertion, we're permitted to query the database directly using SQLAlchemy queries (or even the SQLite client, perhaps) and check the values of the different attributes to validate that the ORM properly saved the object and that the before-insert logic was applied successfully.Integration tests would ensure that after a sequence of actions using the
Repository
class, relationships between entities are correctly maintained. For example, we would test that a group of related entities (e.g. oneOrganization
entity and two childrenTeam
entities) that have the rightrelationship()
configurations are saved properly whether we save the object tree all at once or save the various objects separately over multiple calls to theadd()
method of theRepository
class. In order to do that, we can just use theget()
method of theRepository
class and check the relationship attributes in the query results, since that method was already tested via unit tests. There's no need to keep using the SQLAlchemy query method directly to load the data (like what we'd do inside unit tests).
Please let me know if my understanding of how these tests is incorrect and how it should be implemented instead, in case it is.