def test_missing_rc_dir(tmpdir): try: rcfile = str(tmpdir.join("subdir").join("rcfile")) mssqlcli = create_mssql_cli(mssqlclirc_file=rcfile) assert os.path.exists(rcfile) finally: mssqlcli.sqltoolsclient.shutdown()
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()
def mssqlcli(): """ Pytest fixture which returns interactive mssql-cli instance and cleans up on teardown. """ mssqlcli = create_mssql_cli(interactive_mode=True) yield mssqlcli shutdown(mssqlcli)
def test_mssqlcliclient_reset_connection(self): """ Verify if the MssqlCliClient can successfully reset its connection """ try: mssqlcli = create_mssql_cli() mssqlcli.reset() finally: shutdown(mssqlcli.mssqlcliclient_main)
def test_noninteractive_run(): """ Test that calling run throws an exception only when interactive_mode is false """ mssqlcli = create_mssql_cli(interactive_mode=False) try: mssqlcli.run() assert False except ValueError: assert True finally: mssqlcli.shutdown()
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 invalid_run(**options): ''' Tests mssql-cli runs with invalid combination of properities set ''' mssqlcli = None try: mssqlcli = create_mssql_cli(**options) mssqlcli.run() assert False except ValueError: assert True finally: if mssqlcli is not None: shutdown(mssqlcli)
def test_output_with_interactive_change(): ''' Fails on run after interactive mode has been toggled ''' mssqlcli = create_mssql_cli(interactive_mode=False, output_file='will-fail-eventually.txt') mssqlcli.interactive_mode = True try: mssqlcli.run() assert False except ValueError: assert True finally: shutdown(mssqlcli)
def test_long_query(tmp_filepath): """ Output large query using Python class instance. """ query_str = "SELECT * FROM STRING_SPLIT(REPLICATE(CAST('X,' AS VARCHAR(MAX)), 1024), ',')" try: mssqlcli = create_mssql_cli(interactive_mode=False, output_file=tmp_filepath) output_query = '\n'.join(mssqlcli.execute_query(query_str)) file_baseline = get_io_paths('big.txt')[1] output_baseline = get_file_contents(file_baseline) assert output_query == output_baseline # test output to file output_query_from_file = get_file_contents(tmp_filepath) assert output_query_from_file == output_baseline finally: shutdown(mssqlcli)
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)
def test_format_output_auto_expand(self): 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 list(expanded_results) == expanded
def mssqlcli(): """ Create new mssql-cli instance for each test """ output_file = os.path.join(_BASELINE_DIR, 'tmp.txt') yield create_mssql_cli(interactive_mode=False, output_file=output_file) os.remove(output_file)
def mssqlcli(): """ Create new mssql-cli instance for each test """ return create_mssql_cli(interactive_mode=False)