diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/conftest.py | 2 | ||||
-rw-r--r-- | test/features/crud_database.feature | 4 | ||||
-rw-r--r-- | test/features/db_utils.py | 18 | ||||
-rw-r--r-- | test/features/environment.py | 18 | ||||
-rw-r--r-- | test/features/fixture_data/help_commands.txt | 2 | ||||
-rw-r--r-- | test/features/steps/crud_database.py | 19 | ||||
-rw-r--r-- | test/features/steps/wrappers.py | 2 | ||||
-rw-r--r-- | test/test_main.py | 34 | ||||
-rw-r--r-- | test/test_special_iocommands.py | 25 | ||||
-rw-r--r-- | test/test_sqlexecute.py | 2 | ||||
-rw-r--r-- | test/test_tabular_output.py | 2 |
11 files changed, 100 insertions, 28 deletions
diff --git a/test/conftest.py b/test/conftest.py index cf6d721..d7d10ce 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -4,7 +4,7 @@ from .utils import (HOST, USER, PASSWORD, PORT, CHARSET, create_db, import mycli.sqlexecute -@pytest.yield_fixture(scope="function") +@pytest.fixture(scope="function") def connection(): create_db('_test_db') connection = db_connection('_test_db') diff --git a/test/features/crud_database.feature b/test/features/crud_database.feature index 0c298b6..f4a7a7f 100644 --- a/test/features/crud_database.feature +++ b/test/features/crud_database.feature @@ -16,6 +16,10 @@ Feature: manipulate databases: when we connect to dbserver then we see database connected + Scenario: connect and disconnect from quoted test database + When we connect to quoted test database + then we see database connected + Scenario: create and drop default database When we create database then we see database created diff --git a/test/features/db_utils.py b/test/features/db_utils.py index c29dedb..be550e9 100644 --- a/test/features/db_utils.py +++ b/test/features/db_utils.py @@ -1,11 +1,12 @@ import pymysql -def create_db(hostname='localhost', username=None, password=None, - dbname=None): +def create_db(hostname='localhost', port=3306, username=None, + password=None, dbname=None): """Create test database. :param hostname: string + :param port: int :param username: string :param password: string :param dbname: string @@ -14,6 +15,7 @@ def create_db(hostname='localhost', username=None, password=None, """ cn = pymysql.connect( host=hostname, + port=port, user=username, password=password, charset='utf8mb4', @@ -26,14 +28,15 @@ def create_db(hostname='localhost', username=None, password=None, cn.close() - cn = create_cn(hostname, password, username, dbname) + cn = create_cn(hostname, port, password, username, dbname) return cn -def create_cn(hostname, password, username, dbname): +def create_cn(hostname, port, password, username, dbname): """Open connection to database. :param hostname: + :param port: :param password: :param username: :param dbname: string @@ -42,6 +45,7 @@ def create_cn(hostname, password, username, dbname): """ cn = pymysql.connect( host=hostname, + port=port, user=username, password=password, db=dbname, @@ -52,11 +56,12 @@ def create_cn(hostname, password, username, dbname): return cn -def drop_db(hostname='localhost', username=None, password=None, - dbname=None): +def drop_db(hostname='localhost', port=3306, username=None, + password=None, dbname=None): """Drop database. :param hostname: string + :param port: int :param username: string :param password: string :param dbname: string @@ -64,6 +69,7 @@ def drop_db(hostname='localhost', username=None, password=None, """ cn = pymysql.connect( host=hostname, + port=port, user=username, password=password, db=dbname, diff --git a/test/features/environment.py b/test/features/environment.py index 1a49dbe..98c2004 100644 --- a/test/features/environment.py +++ b/test/features/environment.py @@ -16,8 +16,9 @@ def before_all(context): os.environ['LINES'] = "100" os.environ['COLUMNS'] = "100" os.environ['EDITOR'] = 'ex' - os.environ['LC_ALL'] = 'en_US.utf8' + os.environ['LC_ALL'] = 'en_US.UTF-8' os.environ['PROMPT_TOOLKIT_NO_CPR'] = '1' + os.environ['MYCLI_HISTFILE'] = os.devnull test_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) login_path_file = os.path.join(test_dir, 'mylogin.cnf') @@ -42,6 +43,10 @@ def before_all(context): 'my_test_host', os.getenv('PYTEST_HOST', 'localhost') ), + 'port': context.config.userdata.get( + 'my_test_port', + int(os.getenv('PYTEST_PORT', '3306')) + ), 'user': context.config.userdata.get( 'my_test_user', os.getenv('PYTEST_USER', 'root') @@ -72,7 +77,8 @@ def before_all(context): context.conf['myclirc'] = os.path.join(context.package_root, 'test', 'myclirc') - context.cn = dbutils.create_db(context.conf['host'], context.conf['user'], + context.cn = dbutils.create_db(context.conf['host'], context.conf['port'], + context.conf['user'], context.conf['pass'], context.conf['dbname']) @@ -82,8 +88,9 @@ def before_all(context): def after_all(context): """Unset env parameters.""" dbutils.close_cn(context.cn) - dbutils.drop_db(context.conf['host'], context.conf['user'], - context.conf['pass'], context.conf['dbname']) + dbutils.drop_db(context.conf['host'], context.conf['port'], + context.conf['user'], context.conf['pass'], + context.conf['dbname']) # Restore env vars. #for k, v in context.pgenv.items(): @@ -118,11 +125,12 @@ def after_scenario(context, _): host = context.conf['host'] dbname = context.currentdb context.cli.expect_exact( - '{0}@{1}:{2}> '.format( + '{0}@{1}:{2}>'.format( user, host, dbname ), timeout=5 ) + context.cli.sendcontrol('c') context.cli.sendcontrol('d') context.cli.expect_exact(pexpect.EOF, timeout=5) diff --git a/test/features/fixture_data/help_commands.txt b/test/features/fixture_data/help_commands.txt index 657db7d..2c06d5d 100644 --- a/test/features/fixture_data/help_commands.txt +++ b/test/features/fixture_data/help_commands.txt @@ -2,6 +2,7 @@ | Command | Shortcut | Description |
+-------------+----------------------------+------------------------------------------------------------+
| \G | \G | Display current query results vertically. |
+| \clip | \clip | Copy query to the system clipboard. |
| \dt | \dt[+] [table] | List or describe tables. |
| \e | \e | Edit command with editor (uses $EDITOR). |
| \f | \f [name [args..]] | List or execute favorite queries. |
@@ -9,6 +10,7 @@ | \fs | \fs name query | Save a favorite query. |
| \l | \l | List databases. |
| \once | \o [-o] filename | Append next result to an output file (overwrite using -o). |
+| \pipe_once | \| command | Send next result to a subprocess. |
| \timing | \t | Toggle timing of commands. |
| connect | \r | Reconnect to the database. Optional database argument. |
| exit | \q | Exit. |
diff --git a/test/features/steps/crud_database.py b/test/features/steps/crud_database.py index a0bfa53..841f37d 100644 --- a/test/features/steps/crud_database.py +++ b/test/features/steps/crud_database.py @@ -37,6 +37,14 @@ def step_db_connect_test(context): context.cli.sendline('use {0};'.format(db_name)) +@when('we connect to quoted test database') +def step_db_connect_quoted_tmp(context): + """Send connect to database.""" + db_name = context.conf['dbname'] + context.currentdb = db_name + context.cli.sendline('use `{0}`;'.format(db_name)) + + @when('we connect to tmp database') def step_db_connect_tmp(context): """Send connect to database.""" @@ -64,15 +72,13 @@ def step_see_prompt(context): user = context.conf['user'] host = context.conf['host'] dbname = context.currentdb - wrappers.expect_exact(context, '{0}@{1}:{2}> '.format( - user, host, dbname), timeout=5) - context.atprompt = True + wrappers.wait_prompt(context, '{0}@{1}:{2}> '.format(user, host, dbname)) @then('we see help output') def step_see_help(context): for expected_line in context.fixture_data['help_commands.txt']: - wrappers.expect_exact(context, expected_line + '\r\n', timeout=1) + wrappers.expect_exact(context, expected_line, timeout=1) @then('we see database created') @@ -96,10 +102,7 @@ def step_see_db_dropped_no_default(context): context.currentdb = None wrappers.expect_exact(context, 'Query OK, 0 rows affected', timeout=2) - wrappers.expect_exact(context, '{0}@{1}:{2}> '.format( - user, host, database), timeout=5) - - context.atprompt = True + wrappers.wait_prompt(context, '{0}@{1}:{2}>'.format(user, host, database)) @then('we see database connected') diff --git a/test/features/steps/wrappers.py b/test/features/steps/wrappers.py index 565ca59..de833dd 100644 --- a/test/features/steps/wrappers.py +++ b/test/features/steps/wrappers.py @@ -88,7 +88,7 @@ def wait_prompt(context, prompt=None): user = context.conf['user'] host = context.conf['host'] dbname = context.currentdb - prompt = '{0}@{1}:{2}> '.format( + prompt = '{0}@{1}:{2}>'.format( user, host, dbname), expect_exact(context, prompt, timeout=5) context.atprompt = True diff --git a/test/test_main.py b/test/test_main.py index 3f92bd1..707c359 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -492,3 +492,37 @@ def test_ssh_config(monkeypatch): MockMyCli.connect_args["ssh_host"] == "arg_host" and \ MockMyCli.connect_args["ssh_port"] == 3 and \ MockMyCli.connect_args["ssh_key_filename"] == "/path/to/key" + + +@dbtest +def test_init_command_arg(executor): + init_command = "set sql_select_limit=1000" + sql = 'show variables like "sql_select_limit";' + runner = CliRunner() + result = runner.invoke( + cli, args=CLI_ARGS + ["--init-command", init_command], input=sql + ) + + expected = "sql_select_limit\t1000\n" + assert result.exit_code == 0 + assert expected in result.output + + +@dbtest +def test_init_command_multiple_arg(executor): + init_command = 'set sql_select_limit=2000; set max_join_size=20000' + sql = ( + 'show variables like "sql_select_limit";\n' + 'show variables like "max_join_size"' + ) + runner = CliRunner() + result = runner.invoke( + cli, args=CLI_ARGS + ['--init-command', init_command], input=sql + ) + + expected_sql_select_limit = 'sql_select_limit\t2000\n' + expected_max_join_size = 'max_join_size\t20000\n' + + assert result.exit_code == 0 + assert expected_sql_select_limit in result.output + assert expected_max_join_size in result.output diff --git a/test/test_special_iocommands.py b/test/test_special_iocommands.py index b8b3acb..73bfbab 100644 --- a/test/test_special_iocommands.py +++ b/test/test_special_iocommands.py @@ -49,7 +49,8 @@ def test_editor_command(): assert mycli.packages.special.get_filename(r'\e filename') == "filename" os.environ['EDITOR'] = 'true' - mycli.packages.special.open_external_editor(r'select 1') == "select 1" + os.environ['VISUAL'] = 'true' + mycli.packages.special.open_external_editor(sql=r'select 1') == "select 1" def test_tee_command(): @@ -93,9 +94,8 @@ def test_once_command(): with pytest.raises(TypeError): mycli.packages.special.execute(None, u"\\once") - mycli.packages.special.execute(None, u"\\once /proc/access-denied") with pytest.raises(OSError): - mycli.packages.special.write_once(u"hello world") + mycli.packages.special.execute(None, u"\\once /proc/access-denied") mycli.packages.special.write_once(u"hello world") # write without file set with tempfile.NamedTemporaryFile() as f: @@ -104,9 +104,24 @@ def test_once_command(): assert f.read() == b"hello world\n" mycli.packages.special.execute(None, u"\\once -o " + f.name) - mycli.packages.special.write_once(u"hello world") + mycli.packages.special.write_once(u"hello world line 1") + mycli.packages.special.write_once(u"hello world line 2") f.seek(0) - assert f.read() == b"hello world\n" + assert f.read() == b"hello world line 1\nhello world line 2\n" + + +def test_pipe_once_command(): + with pytest.raises(IOError): + mycli.packages.special.execute(None, u"\\pipe_once") + + with pytest.raises(OSError): + mycli.packages.special.execute( + None, u"\\pipe_once /proc/access-denied") + + mycli.packages.special.execute(None, u"\\pipe_once wc") + mycli.packages.special.write_once(u"hello world") + mycli.packages.special.unset_pipe_once_if_written() + # how to assert on wc output? def test_parseargfile(): diff --git a/test/test_sqlexecute.py b/test/test_sqlexecute.py index c2d38be..5168bf6 100644 --- a/test/test_sqlexecute.py +++ b/test/test_sqlexecute.py @@ -166,7 +166,7 @@ def test_favorite_query_expanded_output(executor): results = run(executor, "\\fs test-ae select * from test") assert_result_equal(results, status='Saved.') - results = run(executor, "\\f test-ae \G") + results = run(executor, "\\f test-ae \\G") assert is_expanded_output() is True assert_result_equal(results, title='> select * from test', headers=['a'], rows=[('abc',)], auto_status=False) diff --git a/test/test_tabular_output.py b/test/test_tabular_output.py index 7d7d000..c20c7de 100644 --- a/test/test_tabular_output.py +++ b/test/test_tabular_output.py @@ -16,7 +16,7 @@ from pymysql.constants import FIELD_TYPE @pytest.fixture def mycli(): cli = MyCli() - cli.connect(None, USER, PASSWORD, HOST, PORT, None) + cli.connect(None, USER, PASSWORD, HOST, PORT, None, init_command=None) return cli |