-
Notifications
You must be signed in to change notification settings - Fork 607
Fix alias_dsn detection for --list-dsn and -D #1617
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
Open
ChrisJr404
wants to merge
2
commits into
dbcli:main
Choose a base branch
from
ChrisJr404:fix/alias-dsn-detection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−2
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import pytest | ||
| from click.testing import CliRunner | ||
|
|
||
| from pgcli.main import cli, PGCli | ||
|
|
||
|
|
||
| def write_config(tmp_path, body): | ||
| cfg = tmp_path / "config" | ||
| cfg.write_text(body) | ||
| return cfg | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def isolate_config(monkeypatch, tmp_path): | ||
| # Keep the real config dir out of the picture. get_config() writes its | ||
| # default template under here when a config file does not exist yet. | ||
| monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path)) | ||
| return tmp_path | ||
|
|
||
|
|
||
| def test_list_dsn_fresh_config(isolate_config): | ||
| # No config file exists yet. --list-dsn used to read the not-yet-written | ||
| # config before PGCli() created it, hit a KeyError on the missing | ||
| # [alias_dsn] section, and print "Invalid DSNs found" with exit code 1. | ||
| # It should just report no aliases and exit cleanly. | ||
| cfg = isolate_config / "config" | ||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ["--pgclirc", str(cfg), "--list-dsn"]) | ||
| assert result.exit_code == 0 | ||
| assert "Invalid DSNs" not in result.output | ||
| assert result.output.strip() == "" | ||
|
|
||
|
|
||
| def test_list_dsn_lists_aliases(isolate_config): | ||
| cfg = write_config( | ||
| isolate_config, | ||
| "[alias_dsn]\n" | ||
| "foo = postgres://u:p@localhost:5432/foo\n" | ||
| "bar = postgres://u:p@localhost:5432/bar\n", | ||
| ) | ||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ["--pgclirc", str(cfg), "--list-dsn"]) | ||
| assert result.exit_code == 0 | ||
| assert "foo : postgres://u:p@localhost:5432/foo" in result.output | ||
| assert "bar : postgres://u:p@localhost:5432/bar" in result.output | ||
|
|
||
|
|
||
| def test_dsn_alias_resolves(isolate_config, monkeypatch): | ||
| cfg = write_config( | ||
| isolate_config, | ||
| "[alias_dsn]\nfoo = postgres://u:p@localhost:5432/foo\n", | ||
| ) | ||
| captured = {} | ||
|
|
||
| def fake_connect(self, *args, **kwargs): | ||
| # connect_uri() parses the alias URI and calls connect() with the | ||
| # resolved parts, so recording them proves the alias was found. | ||
| captured.update(kwargs) | ||
|
|
||
| class DummyExec: | ||
| def run(self, cmd): | ||
| return [] | ||
|
|
||
| def get_timezone(self): | ||
| return "UTC" | ||
|
|
||
| def set_timezone(self, *a, **k): | ||
| pass | ||
|
|
||
| self.pgexecute = DummyExec() | ||
|
|
||
| monkeypatch.setattr(PGCli, "connect", fake_connect) | ||
|
|
||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ["--pgclirc", str(cfg), "-D", "foo", "--ping"]) | ||
| assert result.exit_code == 0 | ||
| assert "Could not find a DSN" not in result.output | ||
| assert captured.get("database") == "foo" | ||
| assert captured.get("host") == "localhost" | ||
|
|
||
|
|
||
| def test_dsn_alias_missing(isolate_config): | ||
| cfg = write_config( | ||
| isolate_config, | ||
| "[alias_dsn]\nfoo = postgres://u:p@localhost:5432/foo\n", | ||
| ) | ||
| runner = CliRunner() | ||
| result = runner.invoke(cli, ["--pgclirc", str(cfg), "-D", "does-not-exist"]) | ||
| assert result.exit_code == 1 | ||
| assert "Could not find a DSN with alias does-not-exist" in result.output | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!