Skip to content

Commit 1fb8886

Browse files
author
Elliot Boschwitz
authored
Improvements to mssqlcli client tests
* Created new client fixture that sets up mssqlcli client with a newly created db * Configured various tests to utilize testing db * Created wrapper around queries to improve logging in case a query fails * Improved exception handling
1 parent a8a80a9 commit 1fb8886

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

tests/test_mssqlcliclient.py

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
create_mssql_cli,
1010
create_mssql_cli_options,
1111
create_mssql_cli_client,
12+
create_test_db,
13+
clean_up_test_db,
1214
shutdown,
1315
random_str
1416
)
@@ -26,6 +28,22 @@ def client():
2628
yield cl
2729
shutdown(cl)
2830

31+
@staticmethod
32+
@pytest.fixture(scope='class')
33+
def client_with_db():
34+
db_name = create_test_db()
35+
36+
# create options with db name
37+
options = create_mssql_cli_options()
38+
options.database = db_name
39+
40+
cl = create_mssql_cli_client(options)
41+
yield cl
42+
43+
# cleanup
44+
shutdown(cl)
45+
clean_up_test_db(db_name)
46+
2947
class TestMssqlCliClientConnection(MssqlCliClient):
3048
"""
3149
Tests for mssqlcliclient.py and sqltoolsclient.py.
@@ -126,46 +144,53 @@ def test_schema_table_views_and_columns_query(client):
126144
Note: This test should run against a database that the credentials
127145
MSSQL_CLI_USER and MSSQL_CLI_PASSWORD have write access to.
128146
"""
147+
client = client_with_db
148+
129149
# create random strings for entities
130150
tabletest1 = "test_%s" % random_str()
131151
tabletest2 = "test_%s" % random_str()
132152
viewtest = "test_%s" % random_str()
133153
schematest = "test_%s" % random_str()
134154

135-
def drop_entities(mssqlcli_client):
136-
list(mssqlcli_client.execute_query('DROP TABLE %s;' % tabletest1))
137-
list(mssqlcli_client.execute_query('DROP TABLE %s;' % tabletest2))
138-
list(mssqlcli_client.execute_query('DROP VIEW %s IF EXISTS;' % viewtest))
139-
list(mssqlcli_client.execute_query('DROP TABLE %s;' % "."
140-
.join([schematest, tabletest1])))
141-
list(mssqlcli_client.execute_query('DROP SCHEMA %s;' % schematest))
155+
queries_create = [
156+
'CREATE TABLE %s (a int, b varchar(25));' % tabletest1,
157+
'CREATE TABLE %s (x int, y varchar(25), z bit);' % tabletest2,
158+
'CREATE VIEW %s as SELECT a from %s;' % (viewtest, tabletest1),
159+
'CREATE SCHEMA %s;' % schematest,
160+
'CREATE TABLE %s (a int);' % '.'.join([schematest, tabletest1])
161+
]
162+
163+
queries_drop = [
164+
'DROP TABLE IF EXISTS %s;' % tabletest1,
165+
'DROP TABLE IF EXISTS %s;' % tabletest2,
166+
'DROP VIEW IF EXISTS %s;' % viewtest,
167+
'DROP TABLE IF EXISTS %s;' % ".".join([schematest, tabletest1]),
168+
'DROP SCHEMA IF EXISTS %s;' % schematest
169+
]
142170

143171
try:
144-
drop_entities(client) # drop entities in beginning (in case tables exist)
145-
146-
list(client.execute_query('CREATE TABLE %s (a int, b varchar(25));' % tabletest1))
147-
list(client.execute_query('CREATE TABLE %s (x int, y varchar(25), z bit);'
148-
% tabletest2))
149-
list(client.execute_query('CREATE VIEW %s as SELECT a from %s;'
150-
% (viewtest, tabletest1)))
151-
list(client.execute_query('CREATE SCHEMA %s;' % schematest))
152-
list(client.execute_query('CREATE TABLE %s (a int);'
153-
% '.'.join([schematest, tabletest1])))
172+
# drop entities in beginning (in case tables exist)
173+
self.execute_queries(client, queries_drop)
174+
self.execute_queries(client, queries_create)
154175

155176
assert (schematest, tabletest1) in set(client.get_tables())
156177
assert ('dbo', viewtest) in set(client.get_views())
157178
assert (schematest, tabletest1, 'a', 'int', 'NULL') in set(client.get_table_columns())
158179
assert ('dbo', viewtest, 'a', 'int', 'NULL') in set(client.get_view_columns())
159180
assert schematest in client.get_schemas()
181+
except Exception as e:
182+
raise AssertionError(e)
160183
finally:
161-
drop_entities(client)
184+
self.execute_queries(client, queries_drop)
162185

163186
@staticmethod
164187
@pytest.mark.unstable
165188
def test_stored_proc_multiple_result_sets(client):
166189
"""
167190
Verify the results of running a stored proc with multiple result sets
168191
"""
192+
client = client_with_db
193+
169194
create_stored_proc = u"CREATE PROC sp_mssqlcli_multiple_results " \
170195
u"AS " \
171196
u"BEGIN " \
@@ -177,14 +202,25 @@ def test_stored_proc_multiple_result_sets(client):
177202
del_stored_proc = u"DROP PROCEDURE sp_mssqlcli_multiple_results"
178203

179204
try:
180-
list(client.execute_query(create_stored_proc))
205+
self.execute_queries(client, [create_stored_proc])
181206
row_counts = []
182207
for rows, _, _, _, _ in client.execute_query(exec_stored_proc):
183208
row_counts.append(len(rows))
184209
assert row_counts[0] == 2
185210
assert row_counts[1] == 3
211+
except Exception as e:
212+
raise AssertionError(e)
186213
finally:
187-
list(client.execute_query(del_stored_proc))
214+
self.execute_queries(client, [del_stored_proc])
215+
216+
@staticmethod
217+
def execute_queries(client, queries):
218+
""" Executes a batch of queries. """
219+
for query in queries:
220+
for _, _, status, _, is_error in client.execute_query(query):
221+
if is_error:
222+
raise AssertionError("Query execution failed: {}".format(status))
223+
return True
188224

189225

190226
class TestMssqlCliClientMultipleStatement(MssqlCliClient):

0 commit comments

Comments
 (0)