-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathcommand_option.py
More file actions
192 lines (169 loc) · 6.42 KB
/
command_option.py
File metadata and controls
192 lines (169 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Steps for testing -c/--command option behavioral tests.
"""
import subprocess
from behave import when, then
@when('we run pgcli with -c "{command}"')
def step_run_pgcli_with_c(context, command):
"""Run pgcli with -c flag and a command."""
cmd = [
"pgcli",
"-h", context.conf["host"],
"-p", str(context.conf["port"]),
"-U", context.conf["user"],
"-d", context.conf["dbname"],
"-c", command
]
try:
context.cmd_output = subprocess.check_output(
cmd,
cwd=context.package_root,
stderr=subprocess.STDOUT,
timeout=5
)
context.exit_code = 0
except subprocess.CalledProcessError as e:
context.cmd_output = e.output
context.exit_code = e.returncode
except subprocess.TimeoutExpired as e:
context.cmd_output = b"Command timed out"
context.exit_code = -1
@when('we run pgcli with --command "{command}"')
def step_run_pgcli_with_command(context, command):
"""Run pgcli with --command flag and a command."""
cmd = [
"pgcli",
"-h", context.conf["host"],
"-p", str(context.conf["port"]),
"-U", context.conf["user"],
"-d", context.conf["dbname"],
"--command", command
]
try:
context.cmd_output = subprocess.check_output(
cmd,
cwd=context.package_root,
stderr=subprocess.STDOUT,
timeout=5
)
context.exit_code = 0
except subprocess.CalledProcessError as e:
context.cmd_output = e.output
context.exit_code = e.returncode
except subprocess.TimeoutExpired as e:
context.cmd_output = b"Command timed out"
context.exit_code = -1
@then("we see the query result")
def step_see_query_result(context):
"""Verify that the query result is in the output."""
output = context.cmd_output.decode('utf-8')
# Check for common query result indicators
assert any([
"SELECT" in output,
"test_column" in output,
"greeting" in output,
"hello" in output,
"+-" in output, # table border
"|" in output, # table column separator
]), f"Expected query result in output, but got: {output}"
@then("we see both query results")
def step_see_both_query_results(context):
"""Verify that both query results are in the output."""
output = context.cmd_output.decode('utf-8')
# Should contain output from both SELECT statements
assert "SELECT" in output, f"Expected SELECT in output, but got: {output}"
# The output should have multiple result sets
assert output.count("SELECT") >= 2, f"Expected at least 2 SELECT results, but got: {output}"
@then("we see the command output")
def step_see_command_output(context):
"""Verify that the special command output is present."""
output = context.cmd_output.decode('utf-8')
# For \dt we should see table-related output
# It might be empty if no tables exist, but shouldn't error
assert context.exit_code == 0, f"Expected exit code 0, but got: {context.exit_code}"
@then("we see an error message")
def step_see_error_message(context):
"""Verify that an error message is in the output."""
output = context.cmd_output.decode('utf-8')
assert any([
"does not exist" in output,
"error" in output.lower(),
"ERROR" in output,
]), f"Expected error message in output, but got: {output}"
@then("pgcli exits successfully")
def step_pgcli_exits_successfully(context):
"""Verify that pgcli exited with code 0."""
assert context.exit_code == 0, f"Expected exit code 0, but got: {context.exit_code}"
# Clean up
context.cmd_output = None
context.exit_code = None
@then("pgcli exits with error")
def step_pgcli_exits_with_error(context):
"""Verify that pgcli exited with a non-zero code."""
assert context.exit_code != 0, f"Expected non-zero exit code, but got: {context.exit_code}"
# Clean up
context.cmd_output = None
context.exit_code = None
@when("we run pgcli with multiple -c options")
def step_run_pgcli_with_multiple_c(context):
"""Run pgcli with multiple -c flags."""
cmd = [
"pgcli",
"-h", context.conf["host"],
"-p", str(context.conf["port"]),
"-U", context.conf["user"],
"-d", context.conf["dbname"],
"-c", "SELECT 'first' as result",
"-c", "SELECT 'second' as result",
"-c", "SELECT 'third' as result"
]
try:
context.cmd_output = subprocess.check_output(
cmd,
cwd=context.package_root,
stderr=subprocess.STDOUT,
timeout=10
)
context.exit_code = 0
except subprocess.CalledProcessError as e:
context.cmd_output = e.output
context.exit_code = e.returncode
except subprocess.TimeoutExpired as e:
context.cmd_output = b"Command timed out"
context.exit_code = -1
@when("we run pgcli with mixed -c and --command")
def step_run_pgcli_with_mixed_options(context):
"""Run pgcli with mixed -c and --command flags."""
cmd = [
"pgcli",
"-h", context.conf["host"],
"-p", str(context.conf["port"]),
"-U", context.conf["user"],
"-d", context.conf["dbname"],
"-c", "SELECT 'from_c' as source",
"--command", "SELECT 'from_command' as source"
]
try:
context.cmd_output = subprocess.check_output(
cmd,
cwd=context.package_root,
stderr=subprocess.STDOUT,
timeout=10
)
context.exit_code = 0
except subprocess.CalledProcessError as e:
context.cmd_output = e.output
context.exit_code = e.returncode
except subprocess.TimeoutExpired as e:
context.cmd_output = b"Command timed out"
context.exit_code = -1
@then("we see all command outputs")
def step_see_all_command_outputs(context):
"""Verify that all command outputs are present."""
output = context.cmd_output.decode('utf-8')
# Should contain output from all commands
assert "first" in output or "from_c" in output, f"Expected 'first' or 'from_c' in output, but got: {output}"
assert "second" in output or "from_command" in output, f"Expected 'second' or 'from_command' in output, but got: {output}"
# For the 3-command test, also check for third
if "third" in output or "result" in output:
assert "third" in output, f"Expected 'third' in output for 3-command test, but got: {output}"