import pytest
import platform
try:
    import setproctitle
except ImportError:
    setproctitle = None
from pgcli.main import obfuscate_process_password, format_output


@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():
    results = format_output('Title', [('abc', 'def')], ['head1', 'head2'],
                            'test status', 'psql')
    expected = ['Title', '+---------+---------+\n| head1   | head2   |\n|---------+---------|\n| abc     | def     |\n+---------+---------+', 'test status']
    assert results == expected

def test_format_output_auto_expand():
    table_results = format_output('Title', [('abc', 'def')],
                                  ['head1', 'head2'], 'test status', 'psql',
                                  max_width=100)
    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', 'psql',
                                     max_width=1)
    expanded = ['Title', u'-[ RECORD 0 ]-------------------------\nhead1 | abc\nhead2 | def\n', 'test status']
    assert expanded_results == expanded

@@ -95,16 +95,58 @@
 e = None
+%0A%0Afrom pgcli.config import config_location
 %0Afrom pg
@@ -202,16 +202,53 @@
 t_output
+, PGCli%0Afrom utils import dbtest, run
 %0A%0A%0A@pyte
@@ -2629,8 +2629,312 @@
 xpanded%0A
+%0A%0A@dbtest%0Adef test_i_works(tmpdir, executor):%0A    sqlfile = tmpdir.join(%22test.sql%22)%0A    sqlfile.write(%22SELECT NOW()%22)%0A    cli = PGCli(%0A        pgexecute=executor,%0A        pgclirc_file=config_location(),%0A    )%0A    statement = r%22%5Ci %7B%7D%22.format(sqlfile)%0A    run(executor, statement, pgspecial=cli.pgspecial)%0A

test for recent patch for execute_from_file