|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from pathlib import Path |
3 | 4 | from types import SimpleNamespace |
4 | 5 | from typing import Any |
5 | 6 |
|
|
10 | 11 | from mycli.packages.credentials.password_sources import ( |
11 | 12 | KNOWN_PASSWORD_SOURCES, |
12 | 13 | ) |
| 14 | +from mycli.packages.special_commands.dsn_aliases import DsnAliases |
| 15 | +from mycli.packages.special_commands.special_command_utils import format_connection_dsn |
13 | 16 |
|
14 | 17 |
|
15 | 18 | class DummyLogger: |
@@ -93,6 +96,44 @@ def test_expand_dsn_alias_env_var_returns_none() -> None: |
93 | 96 | assert cli_runner.expand_dsn_alias_env_var(None, 'prod') is None |
94 | 97 |
|
95 | 98 |
|
| 99 | +@pytest.mark.parametrize('username', ('team/alice', 'team%2Falice', 'name@example.com', '团队/用户')) |
| 100 | +@pytest.mark.parametrize('expand_env', (False, True)) |
| 101 | +@pytest.mark.parametrize('socket', (None, '/tmp/mysql.sock')) |
| 102 | +def test_run_from_cli_args_preserves_saved_dsn_username( |
| 103 | + monkeypatch: pytest.MonkeyPatch, tmp_path: Path, username: str, expand_env: bool, socket: str | None |
| 104 | +) -> None: |
| 105 | + dsn = format_connection_dsn( |
| 106 | + user=username, |
| 107 | + host='db.example.com', |
| 108 | + port=3307, |
| 109 | + database='prod', |
| 110 | + socket=socket, |
| 111 | + character_set='utf8mb4', |
| 112 | + ) |
| 113 | + config_file = tmp_path / 'myclirc' |
| 114 | + config_file.write_text('[alias_dsn]\n', encoding='utf-8') |
| 115 | + aliases = DsnAliases(ConfigObj(str(config_file), encoding='utf-8', list_values=False)) |
| 116 | + aliases.save('prod', dsn) |
| 117 | + saved_config = ConfigObj(str(config_file), encoding='utf-8', list_values=False) |
| 118 | + config = default_config() |
| 119 | + config['main']['expand_dsn_alias_env_vars'] = str(expand_env) |
| 120 | + config['alias_dsn'] = dict(saved_config['alias_dsn']) |
| 121 | + client = DummyMyCli(config=config) |
| 122 | + cli_args = make_cli_args() |
| 123 | + cli_args.dsn = 'prod' |
| 124 | + |
| 125 | + run_with_client(monkeypatch, cli_args, client) |
| 126 | + |
| 127 | + connection = client.connect_calls[-1] |
| 128 | + assert (connection['user'], connection['host'], connection['port'], connection['database'], connection['socket']) == ( |
| 129 | + username, |
| 130 | + 'db.example.com', |
| 131 | + None if socket else 3307, |
| 132 | + 'prod', |
| 133 | + socket, |
| 134 | + ) |
| 135 | + |
| 136 | + |
96 | 137 | @pytest.mark.parametrize('expand_env', (False, True)) |
97 | 138 | @pytest.mark.parametrize( |
98 | 139 | ('path', 'expected'), |
|
0 commit comments