forked from dbcli/pgcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_main.py
204 lines (176 loc) · 6.78 KB
/
test_main.py
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
193
194
195
196
197
198
199
200
201
202
203
204
# coding=utf-8
from __future__ import unicode_literals
import os
import platform
import mock
from decimal import Decimal
import pytest
try:
import setproctitle
except ImportError:
setproctitle = None
from pgcli.main import (
obfuscate_process_password, format_output, PGCli, OutputSettings
)
from utils import dbtest, run
@pytest.mark.skipif(platform.system() == 'Windows',
reason='Not applicable in windows')
@pytest.mark.skipif(not setproctitle,
reason='setproctitle not available')
def test_obfuscate_process_password():
original_title = setproctitle.getproctitle()
setproctitle.setproctitle("pgcli user=root password=secret host=localhost")
obfuscate_process_password()
title = setproctitle.getproctitle()
expected = "pgcli user=root password=xxxx host=localhost"
assert title == expected
setproctitle.setproctitle("pgcli user=root password=top secret host=localhost")
obfuscate_process_password()
title = setproctitle.getproctitle()
expected = "pgcli user=root password=xxxx host=localhost"
assert title == expected
setproctitle.setproctitle("pgcli user=root password=top secret")
obfuscate_process_password()
title = setproctitle.getproctitle()
expected = "pgcli user=root password=xxxx"
assert title == expected
setproctitle.setproctitle("pgcli postgres://root:secret@localhost/db")
obfuscate_process_password()
title = setproctitle.getproctitle()
expected = "pgcli postgres://root:xxxx@localhost/db"
assert title == expected
setproctitle.setproctitle(original_title)
def test_format_output():
settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g')
results = format_output('Title', [('abc', 'def')], ['head1', 'head2'],
'test status', settings)
expected = [
'Title',
'+---------+---------+',
'| head1 | head2 |',
'|---------+---------|',
'| abc | def |',
'+---------+---------+',
'test status'
]
assert list(results) == expected
def test_format_array_output(executor):
statement = u"""
SELECT
array[1, 2, 3]::bigint[] as bigint_array,
'{{1,2},{3,4}}'::numeric[] as nested_numeric_array,
'{å,魚,текст}'::text[] as 配列
UNION ALL
SELECT '{}', NULL, array[NULL]
"""
results = run(executor, statement)
expected = [
'+----------------+------------------------+--------------+',
'| bigint_array | nested_numeric_array | 配列 |',
'|----------------+------------------------+--------------|',
'| {1,2,3} | {{1,2},{3,4}} | {å,魚,текст} |',
'| {} | <null> | {<null>} |',
'+----------------+------------------------+--------------+',
'SELECT 2'
]
assert list(results) == expected
def test_format_array_output_expanded(executor):
statement = u"""
SELECT
array[1, 2, 3]::bigint[] as bigint_array,
'{{1,2},{3,4}}'::numeric[] as nested_numeric_array,
'{å,魚,текст}'::text[] as 配列
UNION ALL
SELECT '{}', NULL, array[NULL]
"""
results = run(executor, statement, expanded=True)
expected = [
'-[ RECORD 1 ]-------------------------',
'bigint_array | {1,2,3}',
'nested_numeric_array | {{1,2},{3,4}}',
'配列 | {å,魚,текст}',
'-[ RECORD 2 ]-------------------------',
'bigint_array | {}',
'nested_numeric_array | <null>',
'配列 | {<null>}',
'SELECT 2'
]
assert list(results) == expected
def test_format_output_auto_expand():
settings = OutputSettings(
table_format='psql', dcmlfmt='d', floatfmt='g', max_width=100)
table_results = format_output('Title', [('abc', 'def')],
['head1', 'head2'], 'test status', settings)
table = [
'Title',
'+---------+---------+',
'| head1 | head2 |',
'|---------+---------|',
'| abc | def |',
'+---------+---------+',
'test status'
]
assert list(table_results) == table
expanded_results = format_output(
'Title',
[('abc', 'def')],
['head1', 'head2'],
'test status',
settings._replace(max_width=1)
)
expanded = [
'Title',
'-[ RECORD 1 ]-------------------------',
'head1 | abc',
'head2 | def',
'test status'
]
assert list(expanded_results) == expanded
@dbtest
def test_i_works(tmpdir, executor):
sqlfile = tmpdir.join("test.sql")
sqlfile.write("SELECT NOW()")
rcfile = str(tmpdir.join("rcfile"))
cli = PGCli(
pgexecute=executor,
pgclirc_file=rcfile,
)
statement = r"\i {0}".format(sqlfile)
run(executor, statement, pgspecial=cli.pgspecial)
def test_missing_rc_dir(tmpdir):
rcfile = str(tmpdir.join("subdir").join("rcfile"))
PGCli(pgclirc_file=rcfile)
assert os.path.exists(rcfile)
def test_quoted_db_uri(tmpdir):
with mock.patch.object(PGCli, 'connect') as mock_connect:
cli = PGCli(pgclirc_file=str(tmpdir.join("rcfile")))
cli.connect_uri('postgres://bar%5E:%[email protected]/testdb%5B')
mock_connect.assert_called_with(database='testdb[',
port=None,
host='baz.com',
user='bar^',
passwd=']foo')
def test_ssl_db_uri(tmpdir):
with mock.patch.object(PGCli, 'connect') as mock_connect:
cli = PGCli(pgclirc_file=str(tmpdir.join("rcfile")))
cli.connect_uri(
'postgres://bar%5E:%[email protected]/testdb%5B?'
'sslmode=verify-full&sslcert=m%79.pem&sslkey=my-key.pem&sslrootcert=c%61.pem')
mock_connect.assert_called_with(database='testdb[',
host='baz.com',
port=None,
user='bar^',
passwd=']foo',
sslmode='verify-full',
sslcert='my.pem',
sslkey='my-key.pem',
sslrootcert='ca.pem')
def test_port_db_uri(tmpdir):
with mock.patch.object(PGCli, 'connect') as mock_connect:
cli = PGCli(pgclirc_file=str(tmpdir.join("rcfile")))
cli.connect_uri('postgres://bar:[email protected]:2543/testdb')
mock_connect.assert_called_with(database='testdb',
host='baz.com',
user='bar',
passwd='foo',
port='2543')