Skip to content

Commit

Permalink
Merge pull request #58 from cicekhayri/more-tests
Browse files Browse the repository at this point in the history
More tests
  • Loading branch information
cicekhayri authored Jan 12, 2024
2 parents ec1bdc5 + 3cda826 commit cf7fb0e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
6 changes: 4 additions & 2 deletions inspira/migrations/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click
from sqlalchemy import select, create_engine, inspect
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import declarative_base, scoped_session, sessionmaker
from sqlalchemy.sql.ddl import CreateTable, CreateIndex, DropIndex
from sqlalchemy.sql.expression import func
import os
Expand Down Expand Up @@ -32,7 +32,9 @@
except ImportError:
Base = declarative_base()
engine = create_engine("sqlite:///:memory:")
db_session = None
db_session = scoped_session(
sessionmaker(autocommit=False, autoflush=False, bind=engine)
)


class Migration(Base):
Expand Down
25 changes: 25 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,28 @@ def sample_sql_file(tmp_path):
with open(sql_file, "w") as file:
file.write(sql_content)
return str(sql_file)


@pytest.fixture
def add_index_users(tmp_path):
sql_content = """
CREATE INDEX ix_users_name ON users (name);
"""

sql_file = tmp_path / "test.sql"
with open(sql_file, "w") as file:
file.write(sql_content)
return str(sql_file)


@pytest.fixture
def setup_teardown_db_session():
from inspira.migrations.migrations import (
engine,
db_session,
initialize_database,
)

initialize_database(engine)
yield db_session
db_session.rollback()
42 changes: 41 additions & 1 deletion tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
engine,
Base,
generate_create_table_sql,
get_existing_columns,
insert_migration,
db_session,
Migration,
get_existing_indexes,
)
from inspira.migrations.utils import (
get_or_create_migration_directory,
Expand Down Expand Up @@ -176,9 +181,44 @@ def test_get_latest_migration_number(mock_listdir):
assert result == 3


def test_execute_sql_file(sample_sql_file, caplog):
def test_execute_sql_file(sample_sql_file):
execute_sql_file(sample_sql_file)

inspector = inspect(engine)

assert "users" in inspector.get_table_names()


def test_get_existing_columns_table_exists(sample_sql_file):
execute_sql_file(sample_sql_file)
result = get_existing_columns("users")
assert result == ["id", "name", "email"]


def test_get_existing_columns_table_does_not_exist():
table_name = "non_existent_table"
result = get_existing_columns(table_name)
assert result is None


def test_insert_migration(setup_teardown_db_session):
current_version = 0
migration_name = "example_migration"
insert_migration(current_version, migration_name)

result = (
db_session.query(Migration)
.filter_by(version=current_version + 1, migration_name=migration_name)
.first()
)

assert result.version == 1
assert result.migration_name == migration_name


def test_get_existing_indexes(add_index_users):
execute_sql_file(add_index_users)
indexes = get_existing_indexes("users")

assert len(indexes) == 1
assert "ix_users_name" in indexes

0 comments on commit cf7fb0e

Please sign in to comment.