In an application with wrapper methods over SQLAlchemy
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.
Member), the data layer implementation includes the following two classes:
Database: wraps the SQLAlchemy session management methods
Repository: 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
Repositoryclasses work as expected. One example of a unit test, here, would assert that the
Repositoryclass 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
Repositoryclass, relationships between entities are correctly maintained. For example, we would test that a group of related entities (e.g. one
Organizationentity and two children
Teamentities) that have the right
relationship()configurations are saved properly whether we save the object tree all at once or save the various objects separately over multiple calls to the
add()method of the
Repositoryclass. In order to do that, we can just use the
get()method of the
Repositoryclass 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.