Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DataDaoDe committed Jan 27, 2025
1 parent fbef24e commit b6ab18a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
22 changes: 12 additions & 10 deletions advanced_alchemy/utils/databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ def create_database_sync(engine: Engine, database: str | None, encoding: str = "
sql = f"CREATE DATABASE {database} CHARACTER SET = '{encoding}'"
conn.execute(text(sql))

elif dialect_name == "sqlite" and database != ":memory:":
if database:
elif dialect_name == "sqlite":
if database and database != ":memory:":
with engine.begin() as conn:
conn.execute(text("CREATE TABLE DB(id int)"))
conn.execute(text("DROP TABLE DB"))

else:
pass
else:
with engine.begin() as conn:
sql = f"CREATE DATABASE {database}"
Expand All @@ -99,12 +100,13 @@ async def create_database_async(engine: AsyncEngine, database: str | None, encod
sql = f"CREATE DATABASE {database} CHARACTER SET = '{encoding}'"
await conn.execute(text(sql))

elif dialect_name == "sqlite" and database != ":memory:":
if database:
elif dialect_name == "sqlite":
if database and database != ":memory:":
async with engine.begin() as conn:
await conn.execute(text("CREATE TABLE DB(id int)"))
await conn.execute(text("DROP TABLE DB"))

else:
pass
else:
async with engine.begin() as conn:
sql = f"CREATE DATABASE {database}"
Expand Down Expand Up @@ -154,8 +156,8 @@ def _disconnect_users_sql(version: tuple[int, int] | None, database: str | None)

async def drop_database_async(engine: AsyncEngine, database: str | None) -> None:
dialect_name = engine.url.get_dialect().name
if dialect_name == "sqlite" and database != ":memory:":
if database:
if dialect_name == "sqlite":
if database and database != ":memory:":
Path(database).unlink()
elif dialect_name == "postgresql":
async with engine.begin() as conn:
Expand All @@ -177,8 +179,8 @@ async def drop_database_async(engine: AsyncEngine, database: str | None) -> None

def drop_database_sync(engine: Engine, database: str | None) -> None:
dialect_name = engine.url.get_dialect().name
if dialect_name == "sqlite" and database != ":memory:":
if database:
if dialect_name == "sqlite":
if database and database != ":memory:":
Path(database).unlink()
elif dialect_name == "postgresql":
with engine.begin() as conn:
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ def test_dump_data(cli_runner: CliRunner, database_cli: Group, mock_context: Mag
assert result.exit_code == 0


def test_create(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock, tmp_path: Path) -> None:
"""Test create the database."""

result = cli_runner.invoke(
database_cli,
["--config", "tests.unit.fixtures.configs", "create", "--encoding", "latin1", "--no-prompt"],
)
assert result.exit_code == 0


def test_drop(cli_runner: CliRunner, database_cli: Group, mock_context: MagicMock, tmp_path: Path) -> None:
"""Test drop the database."""

result = cli_runner.invoke(
database_cli,
["--config", "tests.unit.fixtures.configs", "drop", "--no-prompt"],
)
assert result.exit_code == 0


def test_cli_group_creation() -> None:
"""Test that the CLI group is created correctly."""
cli_group = add_migration_commands()
Expand Down

0 comments on commit b6ab18a

Please sign in to comment.