forked from dbcli/mycli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (45 loc) · 1.61 KB
/
utils.py
File metadata and controls
56 lines (45 loc) · 1.61 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
from os import getenv
import pymysql
import pytest
from mycli.main import MyCli, special
PASSWORD = getenv('PYTEST_PASSWORD')
USER = getenv('PYTEST_USER', 'root')
HOST = getenv('PYTEST_HOST', 'localhost')
PORT = getenv('PYTEST_PORT', 3306)
CHARSET = getenv('PYTEST_CHARSET', 'utf8')
def db_connection(dbname=None):
conn = pymysql.connect(user=USER, host=HOST, port=PORT, database=dbname, password=PASSWORD,
charset=CHARSET,
local_infile=False)
conn.autocommit = True
return conn
try:
db_connection()
CAN_CONNECT_TO_DB = True
except:
CAN_CONNECT_TO_DB = False
dbtest = pytest.mark.skipif(
not CAN_CONNECT_TO_DB,
reason="Need a mysql instance at localhost accessible by user 'root'")
def create_db(dbname):
with db_connection().cursor() as cur:
try:
cur.execute('''DROP DATABASE IF EXISTS _test_db''')
cur.execute('''CREATE DATABASE _test_db''')
except:
pass
def run(executor, sql, join=False):
" Return string output for the sql to be run "
result = []
# TODO: this needs to go away. `run()` should not test formatted output.
# It should test raw results.
mycli = MyCli()
for title, rows, headers, status in executor.run(sql):
result.extend(mycli.format_output(title, rows, headers, status,
special.is_expanded_output()))
if join:
result = '\n'.join(result)
return result
def set_expanded_output(is_expanded):
""" Pass-through for the tests """
return special.set_expanded_output(is_expanded)