Skip to content
Merged
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
6 changes: 5 additions & 1 deletion dslr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def cli(url, debug):
}

# Update the settings singleton
settings.initialize(**config)
try:
settings.initialize(**config)
except ValueError as e:
Comment thread
mixxorz marked this conversation as resolved.
eprint(e, style="red")
sys.exit(1)


@cli.command()
Expand Down
6 changes: 6 additions & 0 deletions dslr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ def initialize(self, *, url: str, debug: bool):
self.url = url
self.debug = debug

if not self.url:
raise ValueError(
"No database URL specified. Please pass it via the --url option, "
'the DATABASE_URL environment variable, or a "dslr.toml" file.'
)

parsed = urlparse(url)

self.db = DatabaseConnection(
Expand Down
47 changes: 24 additions & 23 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,16 @@ def test_db_option(self, mock_cli_settings, mock_get_snapshots):
url="postgres://cli:pw@test:5432/my_db",
)

@mock.patch("dslr.cli.settings")
def test_settings_preference_order(self, mock_cli_settings, mock_get_snapshots):
# No options passed (e.g. PG environment variables are used)
@mock.patch.dict(os.environ, {}, clear=True)
def test_no_url(self, mock_get_snapshots):
runner = CliRunner()
result = runner.invoke(cli.cli, ["list"])
self.assertEqual(result.exit_code, 0)

self.assertEqual(result.exit_code, 1)
self.assertIn("No database URL specified", result.output)

@mock.patch("dslr.cli.settings")
def test_settings_preference_order(self, mock_cli_settings, mock_get_snapshots):
# DATABASE_URL environment variable is used
with mock.patch.dict(
os.environ, {"DATABASE_URL": "postgres://envvar:pw@test:5432/my_db"}
Expand All @@ -272,31 +275,29 @@ def test_settings_preference_order(self, mock_cli_settings, mock_get_snapshots):
result = runner.invoke(cli.cli, ["list"])
self.assertEqual(result.exit_code, 0)

# TOML file is used
with mock.patch(
"builtins.open",
mock.mock_open(read_data=b"url = 'postgres://toml:pw@test:5432/my_db'"),
):
runner = CliRunner()
result = runner.invoke(cli.cli, ["list"])
self.assertEqual(result.exit_code, 0)

# --url option is used
runner = CliRunner()
result = runner.invoke(
cli.cli,
["--url", "postgres://cli:pw@test:5432/my_db", "list"],
)
self.assertEqual(result.exit_code, 0)
# TOML file is used
with mock.patch(
"builtins.open",
mock.mock_open(read_data=b"url = 'postgres://toml:pw@test:5432/my_db'"),
):
runner = CliRunner()
result = runner.invoke(cli.cli, ["list"])
self.assertEqual(result.exit_code, 0)

# --url option is used
runner = CliRunner()
result = runner.invoke(
cli.cli,
["--url", "postgres://cli:pw@test:5432/my_db", "list"],
)
self.assertEqual(result.exit_code, 0)

# Check that the correct order of settings is used
self.assertEqual(4, mock_cli_settings.initialize.call_count)
self.assertEqual(3, mock_cli_settings.initialize.call_count)

self.assertEqual(
mock_cli_settings.initialize.call_args_list,
[
# Nothing is passed
mock.call(debug=False, url=""),
# DATABASE_URL is present so use that
mock.call(debug=False, url="postgres://envvar:pw@test:5432/my_db"),
# TOML is present, so use that over DATABASE_URL
Expand Down