Skip to content
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

Abort if database name is empty #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dslr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ def cli(url, debug):
settings.initialize(**config)


def check_database_name():
if not settings.db.name:
eprint("Database name cannot be empty", style="red")
sys.exit(1)


@cli.command()
@click.argument("name", shell_complete=complete_snapshot_names)
@click.option(
Expand All @@ -87,6 +93,7 @@ def snapshot(name: str, overwrite_confirmed: bool):
Takes a snapshot of the database
"""
new = True
check_database_name()

try:
snapshot = find_snapshot(name)
Expand Down Expand Up @@ -124,6 +131,8 @@ def restore(name):
"""
Restores the database from a snapshot
"""
check_database_name()

try:
snapshot = find_snapshot(name)
except SnapshotNotFound:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def test_snapshot_overwrite_with_yes(self):
)
self.assertIn("Updated snapshot existing-snapshot-1", result.output)

@mock.patch.dict(os.environ, {"DATABASE_URL": ""})
def test_snapshot_with_missing_database_url(self):
runner = CliRunner()
result = runner.invoke(cli.cli, ["snapshot", "my-snapshot"])

self.assertEqual(result.exit_code, 1)
self.assertIn("Database name cannot be empty", result.output)

def test_restore(self):
runner = CliRunner()
result = runner.invoke(cli.cli, ["restore", "existing-snapshot-1"])
Expand All @@ -84,6 +92,14 @@ def test_restore_not_found(self):
self.assertEqual(result.exit_code, 1)
self.assertIn("Snapshot not-found does not exist", result.output)

@mock.patch.dict(os.environ, {"DATABASE_URL": ""})
def test_restore_with_missing_database_url(self):
runner = CliRunner()
result = runner.invoke(cli.cli, ["restore", "existing-snapshot-1"])

self.assertEqual(result.exit_code, 1)
self.assertIn("Database name cannot be empty", result.output)

def test_list(self):
runner = CliRunner()
result = runner.invoke(cli.cli, ["list"])
Expand Down