Skip to content

Commit 0fcd9fa

Browse files
authored
Add tests (#70)
1 parent 24a7ca6 commit 0fcd9fa

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

fluid/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
"""Reusable server side python modules"""
22

3-
__version__ = "1.5.0"
3+
__version__ = "1.5.1"

fluid/db/cli.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,24 @@ def current(ctx: click.Context, verbose: bool) -> None:
155155
def create(ctx: click.Context, dbname: str) -> None:
156156
"""Creates a new database"""
157157
if get_db(ctx).migration().db_create(dbname):
158-
click.echo(f"database {dbname} created")
158+
click.echo(f"database '{dbname}' created")
159159
else:
160-
click.echo(f"database {dbname} already available")
160+
click.echo(f"database '{dbname}' already available")
161+
162+
163+
@_db.command()
164+
@click.argument("dbname", nargs=1)
165+
@click.option("-y", "--yes", is_flag=True, help="confirm")
166+
@click.pass_context
167+
def drop(ctx: click.Context, dbname: str, yes: bool) -> None:
168+
"""Drop an existing database"""
169+
if not yes:
170+
click.echo(f"Are you sure you want to drop database '{dbname}'?")
171+
click.confirm("Please confirm", abort=True)
172+
if get_db(ctx).migration().db_drop(dbname):
173+
click.echo(f"database '{dbname}' dropped")
174+
else:
175+
click.echo(f"database '{dbname}' not found")
161176

162177

163178
@_db.command()

fluid/db/migration.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from alembic import command as alembic_cmd
1010
from alembic.config import Config
1111
from sqlalchemy.engine import Engine
12-
from sqlalchemy_utils import create_database, database_exists
12+
from sqlalchemy_utils import create_database, database_exists, drop_database
1313

1414
if TYPE_CHECKING:
1515
from .container import Database
@@ -94,6 +94,15 @@ def db_create(self, dbname: str = "") -> bool:
9494
create_database(url)
9595
return True
9696

97+
def db_drop(self, dbname: str = "") -> bool:
98+
url = self.sync_engine.url
99+
if dbname:
100+
url = url.set(database=dbname)
101+
if database_exists(url):
102+
drop_database(url)
103+
return True
104+
return False
105+
97106
def truncate_all(self) -> None:
98107
"""Drop all tables from :attr:`metadata` in database"""
99108
with self.sync_engine.begin() as conn:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "aio-fluid"
3-
version = "1.5.0"
3+
version = "1.5.1"
44
description = "Tools for backend python services"
55
authors = [
66
{ name = "Luca Sbardella", email = "luca@quantmind.com" }

tests/db/test_cli.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
1+
from click.testing import CliRunner
2+
13
from examples.db.cli import cli
24
from fluid.db import CrudDB
35

46

57
def test_cli():
68
assert isinstance(cli.db, CrudDB)
9+
10+
11+
def test_create_drop_db():
12+
runner = CliRunner()
13+
result = runner.invoke(cli, ["create", "test_db_abc"])
14+
assert result.exit_code == 0
15+
assert "database 'test_db_abc' created" in result.output
16+
result = runner.invoke(cli, ["create", "test_db_abc"])
17+
assert result.exit_code == 0
18+
assert "database 'test_db_abc' already available" in result.output
19+
result = runner.invoke(cli, ["drop", "test_db_abc", "-y"])
20+
assert result.exit_code == 0
21+
assert "database 'test_db_abc' dropped" in result.output
22+
result = runner.invoke(cli, ["drop", "test_db_abc", "-y"])
23+
assert result.exit_code == 0
24+
assert "database 'test_db_abc' not found" in result.output

0 commit comments

Comments
 (0)