66import pytest
77from mssqltestutils import (
88 create_mssql_cli ,
9- random_str
9+ random_str ,
10+ shutdown ,
11+ test_queries ,
12+ get_file_contents ,
13+ get_io_paths ,
14+ _BASELINE_DIR
1015)
1116
12- _BASELINE_DIR = os .path .dirname (os .path .abspath (__file__ ))
1317
1418class TestNonInteractiveResults :
1519 """
@@ -24,20 +28,14 @@ def tmp_filepath():
2428 yield fp
2529 os .remove (fp )
2630
27- testdata = [
28- ("-Q \" SELECT 1\" " , 'small.txt' ),
29- ("-Q \" SELECT 1; SELECT 2;\" " , 'multiple.txt' ),
30- ("-Q \" SELECT %s\" " % ('x' * 250 ), 'col_too_wide.txt' ),
31- ("-Q \" SELECT REPLICATE(CAST('X,' AS VARCHAR(MAX)), 1024)\" " , 'col_wide.txt' )
32- ]
33-
34- @pytest .mark .parametrize ("query_str, test_file" , testdata )
31+ @pytest .mark .parametrize ("query_str, test_file" , test_queries )
3532 @pytest .mark .timeout (60 )
3633 def test_query (self , query_str , test_file ):
3734 """ Tests query outputs to command-line, ensuring -Q and -i produce
3835 the same results. """
39- file_input , file_baseline = self .input_output_paths (test_file )
40- output_baseline = self .get_file_contents (file_baseline )
36+ file_input , file_baseline = get_io_paths (test_file )
37+ output_baseline = get_file_contents (file_baseline )
38+ query_str = '-Q "{}"' .format (query_str ) # append -Q for non-interactive call
4139
4240 # test with -Q
4341 output_query_for_Q = self .execute_query_via_subprocess (query_str )
@@ -47,12 +45,13 @@ def test_query(self, query_str, test_file):
4745 output_query_for_i = self .execute_query_via_subprocess ("-i %s" % file_input )
4846 assert output_query_for_i == output_baseline
4947
50- @pytest .mark .parametrize ("query_str, test_file" , testdata )
48+ @pytest .mark .parametrize ("query_str, test_file" , test_queries )
5149 @pytest .mark .timeout (60 )
5250 def test_output_file (self , query_str , test_file , tmp_filepath ):
5351 """ Tests -o (and ensures file overwrite works) """
54- file_input , file_baseline = self .input_output_paths (test_file )
55- output_baseline = self .get_file_contents (file_baseline )
52+ file_input , file_baseline = get_io_paths (test_file )
53+ output_baseline = get_file_contents (file_baseline )
54+ query_str = '-Q "{}"' .format (query_str ) # append -Q for non-interactive call
5655
5756 # test with -Q
5857 output_query_for_Q = self .execute_query_via_subprocess (query_str , output_file = tmp_filepath )
@@ -63,35 +62,23 @@ def test_output_file(self, query_str, test_file, tmp_filepath):
6362 output_file = tmp_filepath )
6463 assert output_query_for_i == output_baseline
6564
65+ @staticmethod
6666 @pytest .mark .timeout (60 )
67- def test_long_query (self , tmp_filepath ):
67+ def test_long_query (tmp_filepath ):
6868 """ Output large query using Python class instance. """
6969 query_str = "SELECT * FROM STRING_SPLIT(REPLICATE(CAST('X,' AS VARCHAR(MAX)), 1024), ',')"
7070 try :
7171 mssqlcli = create_mssql_cli (interactive_mode = False , output_file = tmp_filepath )
7272 output_query = '\n ' .join (mssqlcli .execute_query (query_str ))
73- file_baseline = self . input_output_paths ('big.txt' )[1 ]
74- output_baseline = self . get_file_contents (file_baseline )
73+ file_baseline = get_io_paths ('big.txt' )[1 ]
74+ output_baseline = get_file_contents (file_baseline )
7575 assert output_query == output_baseline
7676
7777 # test output to file
78- output_query_from_file = self . get_file_contents (tmp_filepath )
78+ output_query_from_file = get_file_contents (tmp_filepath )
7979 assert output_query_from_file == output_baseline
8080 finally :
81- mssqlcli .shutdown ()
82-
83- @staticmethod
84- @pytest .mark .timeout (60 )
85- def test_noninteractive_run ():
86- """ Test that calling run throws an exception only when interactive_mode is false """
87- mssqlcli = create_mssql_cli (interactive_mode = False )
88- try :
89- mssqlcli .run ()
90- assert False
91- except ValueError :
92- assert True
93- finally :
94- mssqlcli .shutdown ()
81+ shutdown (mssqlcli )
9582
9683 @classmethod
9784 @pytest .mark .timeout (60 )
@@ -100,13 +87,6 @@ def test_Q_with_i_run(cls):
10087 output = cls .execute_query_via_subprocess ("-Q 'select 1' -i 'this_breaks.txt'" )
10188 assert output == "Invalid arguments: either -Q or -i may be specified."
10289
103- @staticmethod
104- def input_output_paths (test_file_suffix ):
105- """ Returns tuple of file paths for the input an output of a test. """
106- i = os .path .join (_BASELINE_DIR , 'test_query_inputs' , 'input_%s' % test_file_suffix )
107- o = os .path .join (_BASELINE_DIR , 'test_query_baseline' , 'baseline_%s' % test_file_suffix )
108- return (i , o )
109-
11090 @classmethod
11191 def execute_query_via_subprocess (cls , query_str , output_file = None ):
11292 """ Helper method for running a query. """
@@ -122,19 +102,9 @@ def execute_query_via_subprocess(cls, query_str, output_file=None):
122102
123103 if output_file :
124104 # get file contents if we used -o
125- return cls . get_file_contents (output_file )
105+ return get_file_contents (output_file )
126106 return output .decode ("utf-8" ).replace ('\r ' , '' ).strip ()
127107
128- @staticmethod
129- def get_file_contents (file_path ):
130- """ Get expected result from file. """
131- try :
132- with open (file_path , 'r' ) as f :
133- # remove string literals (needed in python2) and newlines
134- return f .read ().replace ('\r ' , '' ).strip ()
135- except OSError as e :
136- raise e
137-
138108
139109class TestNonInteractiveShutdownQuery :
140110 """
@@ -160,7 +130,7 @@ def test_shutdown_after_query(query_str, mssqlcli):
160130 try :
161131 mssqlcli .execute_query (query_str )
162132 finally :
163- mssqlcli . shutdown ()
133+ shutdown (mssqlcli )
164134 assert mssqlcli .mssqlcliclient_main .sql_tools_client .\
165135 tools_service_process .poll () is not None
166136
@@ -192,6 +162,6 @@ def test_shutdown_after_query(query_str, mssqlcli):
192162 try :
193163 mssqlcli .execute_query (query_str )
194164 finally :
195- mssqlcli . shutdown ()
165+ shutdown (mssqlcli )
196166 assert mssqlcli .mssqlcliclient_main .sql_tools_client .\
197167 tools_service_process .poll () is not None
0 commit comments