Skip to content

Commit 1023f57

Browse files
committed
Update tests.
1 parent c97298c commit 1023f57

1 file changed

Lines changed: 32 additions & 14 deletions

File tree

tests/test_init_commands_simple.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,80 +3,98 @@
33

44
from pgcli.main import cli, PGCli
55

6+
67
@pytest.fixture
78
def dummy_exec(monkeypatch, tmp_path):
89
# Capture executed commands
910
# Isolate config directory for tests
1011
monkeypatch.setenv("XDG_CONFIG_HOME", str(tmp_path))
1112
dummy_cmds = []
13+
1214
class DummyExec:
1315
def run(self, cmd):
1416
# Ignore ping SELECT 1 commands used for exiting CLI
15-
if cmd.strip().upper() == 'SELECT 1':
17+
if cmd.strip().upper() == "SELECT 1":
1618
return []
1719
# Record init commands
1820
dummy_cmds.append(cmd)
1921
return []
2022

23+
def get_timezone(self):
24+
return "UTC"
25+
26+
def set_timezone(self, *args, **kwargs):
27+
pass
28+
2129
def fake_connect(self, *args, **kwargs):
2230
self.pgexecute = DummyExec()
2331

2432
monkeypatch.setattr(PGCli, "connect", fake_connect)
2533
return dummy_cmds
2634

35+
2736
def test_init_command_option(dummy_exec):
28-
"Test that --init-command triggers execution of the command."
37+
"Test that --init-command triggers execution of the command."
2938
runner = CliRunner()
3039
# Use a custom init command and --ping to exit the CLI after init commands
31-
result = runner.invoke(cli, ["--init-command", "SELECT foo", "--ping", "db", "user"])
40+
result = runner.invoke(
41+
cli, ["--init-command", "SELECT foo", "--ping", "db", "user"]
42+
)
3243
assert result.exit_code == 0
3344
# Should print the init command
3445
assert "Running init commands: SELECT foo" in result.output
3546
# Should exit via ping
3647
assert "PONG" in result.output
3748
# DummyExec should have recorded only the init command
3849
assert dummy_exec == ["SELECT foo"]
39-
50+
51+
4052
def test_init_commands_from_config(dummy_exec, tmp_path):
4153
"""
4254
Test that init commands defined in the config file are executed on startup.
4355
"""
4456
# Create a temporary config file with init-commands
4557
config_file = tmp_path / "pgclirc_test"
4658
config_file.write_text(
47-
"[init-commands]\n"
48-
"first = SELECT foo;\n"
49-
"second = SELECT bar;\n"
59+
"[main]\n[init-commands]\nfirst = SELECT foo;\nsecond = SELECT bar;\n"
5060
)
5161

5262
runner = CliRunner()
5363
# Use --ping to exit the CLI after init commands
54-
result = runner.invoke(cli, ["--pgclirc", str(config_file), "--ping", "testdb", "testuser"])
64+
result = runner.invoke(
65+
cli, ["--pgclirc", str(config_file.absolute()), "--ping", "testdb", "user"]
66+
)
5567
assert result.exit_code == 0
5668
# Should print both init commands in order (note trailing semicolons cause double ';;')
5769
assert "Running init commands: SELECT foo;; SELECT bar;" in result.output
5870
# DummyExec should have recorded both commands
5971
assert dummy_exec == ["SELECT foo;", "SELECT bar;"]
6072

73+
6174
def test_init_commands_option_and_config(dummy_exec, tmp_path):
6275
"""
6376
Test that CLI-provided init command is appended after config-defined commands.
6477
"""
6578
# Create a temporary config file with init-commands
6679
config_file = tmp_path / "pgclirc_test"
67-
config_file.write_text(
68-
"[init-commands]\n"
69-
"first = SELECT foo;\n"
70-
)
80+
config_file.write_text("[main]\n [init-commands]\nfirst = SELECT foo;\n")
7181

7282
runner = CliRunner()
7383
# Use --ping to exit the CLI after init commands
7484
result = runner.invoke(
7585
cli,
76-
["--pgclirc", str(config_file), "--init-command", "SELECT baz;", "--ping", "testdb", "testuser"]
86+
[
87+
"--pgclirc",
88+
str(config_file),
89+
"--init-command",
90+
"SELECT baz;",
91+
"--ping",
92+
"testdb",
93+
"user",
94+
],
7795
)
7896
assert result.exit_code == 0
7997
# Should print config command followed by CLI option (double ';' between commands)
8098
assert "Running init commands: SELECT foo;; SELECT baz;" in result.output
8199
# DummyExec should record both commands in order
82-
assert dummy_exec == ["SELECT foo;", "SELECT baz;"]
100+
assert dummy_exec == ["SELECT foo;", "SELECT baz;"]

0 commit comments

Comments
 (0)