forked from dbcli/mssql-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_main.py
170 lines (156 loc) · 5.41 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
# coding=utf-8
from __future__ import unicode_literals
import os
import unittest
import tempfile
from mssqltestutils import (
create_mssql_cli,
shutdown,
getTempPath
)
from mssqlcli.mssql_cli import OutputSettings, MssqlFileHistory
class MainTests(unittest.TestCase):
@staticmethod
def test_history_file_not_store_credentials():
# Used by prompt tool kit, verify statements that contain password or secret
# are not stored by the history file.
statements = [
'Create Database Scoped Credential With Password = <secret>',
'CREATE MASTER KEY WITH SECRET=xyz',
]
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file_path = temp_file.name
file_history = MssqlFileHistory(temp_file_path)
for statement in statements:
file_history.append_string(statement)
assert len(file_history.get_strings()) == 0
@staticmethod
def test_format_output():
mssqlcli = create_mssql_cli()
settings = OutputSettings(table_format='psql', dcmlfmt='d', floatfmt='g')
results = mssqlcli.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_output_live_connection(self):
statement = u"""
select 1 as [ShiftID], 'Day' as [Name] UNION ALL
select 2, N'魚' UNION ALL
select 3, 'Night'
"""
try:
mssqlcli = create_mssql_cli()
result = self.run_and_return_string_from_formatter(mssqlcli, statement)
expected = [
u'+-----------+--------+',
u'| ShiftID | Name |',
u'|-----------+--------|',
u'| 1 | Day |',
u'| 2 | 魚 |',
u'| 3 | Night |',
u'+-----------+--------+',
u'(3 rows affected)'
]
assert list(result) == expected
finally:
shutdown(mssqlcli.mssqlcliclient_main)
def test_format_output_expanded_live_connection(self):
statement = u"""
select N'配列' as [Name] UNION ALL
select 'Evening' UNION ALL
select 'Night'
"""
try:
mssqlcli = create_mssql_cli()
result = self.run_and_return_string_from_formatter(mssqlcli, statement, expanded=True)
expected = [
'-[ RECORD 1 ]-------------------------',
'Name | 配列',
'-[ RECORD 2 ]-------------------------',
'Name | Evening',
'-[ RECORD 3 ]-------------------------',
'Name | Night',
'(3 rows affected)'
]
assert '\n'.join(result) == '\n'.join(expected)
finally:
shutdown(mssqlcli.mssqlcliclient_main)
@staticmethod
def test_format_output_auto_expand():
mssqlcli = create_mssql_cli()
settings = OutputSettings(
table_format='psql',
dcmlfmt='d',
floatfmt='g',
max_width=100)
table_results = mssqlcli.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 = mssqlcli.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 '\n'.join(expanded_results) == '\n'.join(expanded)
@staticmethod
def test_missing_rc_dir():
try:
rcfilePath = getTempPath('subdir', 'rcfile')
mssqlcli = create_mssql_cli(mssqlclirc_file=rcfilePath)
assert os.path.exists(rcfilePath)
finally:
mssqlcli.sqltoolsclient.shutdown()
@staticmethod
def run_and_return_string_from_formatter(mssql_cli, sql, join=False, expanded=False):
"""
Return string output for the sql to be run.
"""
mssql_cli.connect_to_database()
mssql_cli_client = mssql_cli.mssqlcliclient_main
for rows, col, message, _, _ in mssql_cli_client.execute_query(sql):
settings = OutputSettings(
table_format='psql',
dcmlfmt='d',
floatfmt='g',
expanded=expanded
)
formatted = mssql_cli.format_output(None, rows, col, message, settings)
if join:
formatted = '\n'.join(formatted)
return formatted