1
1
mirror of https://github.com/dbcli/pgcli.git synced 2024-10-06 10:17:15 +03:00
pgcli/tests/test_main.py

102 lines
3.7 KiB
Python
Raw Normal View History

import os
2015-10-23 12:49:20 +03:00
import platform
import mock
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
2015-10-23 12:49:20 +03:00
@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)
2015-11-22 16:46:06 +03:00
2015-11-22 16:46:06 +03:00
def test_format_output():
settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g')
2015-11-22 16:46:06 +03:00
results = format_output('Title', [('abc', 'def')], ['head1', 'head2'],
'test status', settings)
2015-11-22 16:46:06 +03:00
expected = ['Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status']
assert results == expected
def test_format_output_auto_expand():
settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g', max_width=100)
2015-11-22 16:46:06 +03:00
table_results = format_output('Title', [('abc', 'def')],
['head1', 'head2'], 'test status', settings)
2015-11-22 16:46:06 +03:00
table = ['Title', '+---------+---------+\n| head1 | head2 |\n|---------+---------|\n| abc | def |\n+---------+---------+', 'test status']
assert table_results == table
expanded_results = format_output('Title', [('abc', 'def')],
['head1', 'head2'], 'test status', settings._replace(max_width=1))
2015-11-22 16:46:06 +03:00
expanded = ['Title', u'-[ RECORD 0 ]-------------------------\nhead1 | abc\nhead2 | def\n', 'test status']
assert 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,
)
2016-02-04 01:31:10 +03:00
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"))
2016-02-03 23:58:32 +03:00
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:%5Dfoo@baz.com/testdb%5B')
mock_connect.assert_called_with('testdb[', 'baz.com', 'bar^', None, ']foo')
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:foo@baz.com:2543/testdb')
mock_connect.assert_called_with('testdb', 'baz.com', 'bar', '2543', 'foo')