-
Notifications
You must be signed in to change notification settings - Fork 52
Integration test to check that the ORM definitions match the LORIS SQL database #1198
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
Merged
cmadjar
merged 5 commits into
aces:main
from
maximemulder:2024-10-07_sqlalchemy-sync-integration-test
Oct 17, 2024
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import importlib | ||
from pathlib import Path | ||
from typing import Any | ||
|
||
from sqlalchemy import MetaData | ||
from sqlalchemy.dialects.mysql.types import DOUBLE, TINYINT | ||
from sqlalchemy.types import TypeDecorator, TypeEngine | ||
|
||
from lib.db.base import Base | ||
from tests.util.database import get_integration_database_engine | ||
|
||
|
||
def test_orm_sql_sync(): | ||
""" | ||
Test that the SQLAlchemy ORM definitions are in sync with the existing SQL database. | ||
""" | ||
|
||
# Load all the ORM table definitions in the SQLAlchemy schema | ||
for file in Path('python/lib/db/model').glob('*.py'): | ||
importlib.import_module(f'lib.db.model.{file.name[:-3]}') | ||
|
||
orm_metadata = Base.metadata | ||
|
||
engine = get_integration_database_engine() | ||
sql_metadata = MetaData() | ||
sql_metadata.reflect(engine) | ||
|
||
for orm_table in orm_metadata.sorted_tables: | ||
print(f'test table: {orm_table.name}') | ||
|
||
# Check that each ORM table has a corresponding SQL table | ||
sql_table = sql_metadata.tables.get(orm_table.name) | ||
assert sql_table is not None | ||
|
||
# Check that the ORM and SQL tables have the same columns | ||
orm_column_names = orm_table.columns.keys().sort() | ||
sql_column_names = sql_table.columns.keys().sort() | ||
assert orm_column_names == sql_column_names | ||
|
||
for orm_column in orm_table.columns: | ||
print(f' test column: {orm_column.name}') | ||
|
||
# Check that the types of the ORM and SQL column are equal | ||
# The type comparison is not exact, minor differences are ignored | ||
sql_column = sql_table.columns[orm_column.name] | ||
orm_column_python_type = get_orm_python_type(orm_column.type) | ||
sql_column_python_type = get_sql_python_type(sql_column.type) | ||
assert orm_column_python_type == sql_column_python_type | ||
assert orm_column.nullable == sql_column.nullable | ||
|
||
print() | ||
|
||
|
||
def get_orm_python_type(orm_type: TypeEngine[Any]): | ||
if isinstance(orm_type, TypeDecorator): | ||
return orm_type.impl.python_type | ||
|
||
return orm_type.python_type | ||
|
||
|
||
def get_sql_python_type(sql_type: TypeEngine[Any]): | ||
if isinstance(sql_type, TINYINT) and sql_type.display_width == 1: # type: ignore | ||
return bool | ||
|
||
if isinstance(sql_type, DOUBLE): | ||
return float | ||
|
||
return sql_type.python_type |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason there is an empty print here? Maybe cosmetic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@maximemulder maybe I should have tagged you so you could see the review?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, it's just a statement to add an empty line break between each table in the output, the result looks like this:
Note that the output only appears when the test fails though. I should probably have added a fail commit in the history as a demonstration, I'll do that tomorrow morning (now is not really the time for that 😅).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cmadjar See the "test error" commit integration test to see what the error looks like if the ORM mappings are wrong.