-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added unique=True on Version; re synthesized-io/sdk#582 * updated get_version_id * added existing_nullable=False to a migration * added tests for coverage
- Loading branch information
1 parent
68dc221
commit f2e861e
Showing
4 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
src/insight/alembic/versions/a2198ae60b44_added_version_name_unique_constaint.py
This file contains 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,39 @@ | ||
"""Set version.name.unique = True | ||
Revision ID: a2198ae60b44 | ||
Revises: d2198fd60b0e | ||
Create Date: 2023-12-13 13:25:17.878689 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "a2198ae60b44" | ||
down_revision = "d2198fd60b0e" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.alter_column( | ||
"version", | ||
"name", | ||
existing_type=sa.VARCHAR(length=50), | ||
unique=True, | ||
existing_nullable=False, | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.alter_column( | ||
"version", | ||
"name", | ||
existing_type=sa.VARCHAR(length=50), | ||
unique=False, | ||
existing_nullable=False, | ||
) | ||
# ### end Alembic commands ### |
This file contains 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 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 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,65 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from sqlalchemy.exc import IntegrityError, SQLAlchemyError | ||
|
||
from insight.database.utils import get_version_id | ||
|
||
|
||
@pytest.fixture | ||
def mock_session(): | ||
session = MagicMock() | ||
session.begin_nested.return_value.__enter__.return_value = session | ||
|
||
def add_side_effect(model_instance): | ||
model_instance.id = 123 | ||
|
||
session.add.side_effect = add_side_effect | ||
session.commit = MagicMock() | ||
|
||
session.rollback = MagicMock() | ||
|
||
mock_scalar_one_or_none = MagicMock() | ||
mock_scalar_one_or_none.id = 789 | ||
executed = MagicMock() | ||
executed.scalar_one_or_none.return_value = mock_scalar_one_or_none | ||
session.execute.return_value = executed | ||
|
||
assert session.execute().scalar_one_or_none().id == 789 | ||
return session | ||
|
||
|
||
def test_get_version_id_existing_version(mock_session): | ||
mock_session.execute.return_value.scalar_one_or_none.return_value = MagicMock(id=123) | ||
version_id = get_version_id("existing_version", mock_session) | ||
assert version_id == 123 | ||
|
||
|
||
def test_get_version_id_new_version(mock_session): | ||
mock_session.execute.return_value.scalar_one_or_none.return_value = None | ||
mock_session.begin_nested.return_value.__enter__.return_value.add.return_value = MagicMock( | ||
id=123 | ||
) | ||
version_id = get_version_id("new_version", mock_session) | ||
assert version_id == 123 | ||
|
||
|
||
def test_get_version_id_integrity_error(mock_session): | ||
# First call to execute raises IntegrityError | ||
# Second call to execute returns a MagicMock with the correct id | ||
second_execute = MagicMock() | ||
second_execute.scalar_one_or_none.return_value = MagicMock(id=789) | ||
mock_session.execute.side_effect = [ | ||
IntegrityError("Mocked Integrity Error", "params", "orig"), | ||
second_execute, | ||
] | ||
|
||
version_id = get_version_id("version_with_error", mock_session) | ||
|
||
assert version_id == 789 | ||
|
||
|
||
def test_get_version_id_sqlalchemy_error(mock_session): | ||
mock_session.execute.side_effect = SQLAlchemyError("Mocked SQLAlchemy Error") | ||
with pytest.raises(SQLAlchemyError): | ||
get_version_id("version_with_sqlalchemy_error", mock_session) |