def run_schema_metadata_validation(self, charset, test_db): try: # setup db test_db_name = test_db client = self.create_mssqlcliclient(test_db_name) new_schemas = [] for characters in self.get_next_characters( self.charset_dict[charset]): schema_name = u'mssqlcli_{0}_{1}_{2}'.format( self.get_local_machine_name(), random_str(), characters) query = u'CREATE SCHEMA {0}'.format(schema_name) if self.run_query(client, query): new_schemas.append(schema_name) else: assert False # should not fail completer = MssqlCompleter(smart_completion=True) completion_refresher.refresh_schemas(completer, client) completions = completer.get_completions(document=Document( u'select * from ', 14, None), complete_event=None, smart_completion=True) db_schemas = set(map(lambda e: e.text, completions)) for new_schema in new_schemas: assert u'"{}"'.format(new_schema) in db_schemas finally: shutdown(client)
def create_test_db(): client = create_mssql_cli_client() local_machine_name = socket.gethostname().replace('-', '_').replace('.', '_') test_db_name = u'mssqlcli_testdb_{0}_{1}'.format(local_machine_name, random_str()) query_db_create = u"CREATE DATABASE {0};".format(test_db_name) count = 0 # retry logic in case db create fails while count < 5: for _, _, status, _, is_create_error in client.execute_query( query_db_create): if not is_create_error: shutdown(client) return test_db_name # log warning to console warnings.warn( 'Test DB create failed with error: {0}'.format(status)) count += 1 shutdown(client) # cleanup db just in case, then raise exception clean_up_test_db(test_db_name) raise AssertionError("DB creation failed.")
def create_test_db(): client = create_mssql_cli_client() local_machine_name = socket.gethostname().replace('-', '_').replace('.', '_') test_db_name = u'mssqlcli_testdb_{0}_{1}'.format(local_machine_name, random_str()) query = u"CREATE DATABASE {0};".format(test_db_name) for _, _, _, _, is_error in client.execute_query(query): if is_error is True: test_db_name = None shutdown(client) return test_db_name
def run_charset_validation(self, charset): """ Verify the column names and string values in rows returned by select statement are properly encoded as unicode. """ local_machine_name = socket.gethostname().replace('-', '_').replace( '.', '_') try: client = create_mssql_cli_client() # Each characters in charset is a string with max length 50 # Each time in the for loop, the string used for 'create table' and # 'insert into' statement that are executed by client.execute_query(). # We validates the query results are the same value we inserted and # they are properly unicode encoded. for characters in self.get_next_characters(charset): test_str = characters col1_name = u'col1_{0}'.format(test_str) col2_name = u'col2_{0}'.format(test_str) table_name = u'#mssqlcli_{0}_{1}_{2}'.format( local_machine_name, random_str(), test_str) setup_query = u"CREATE TABLE {0} ({1} nvarchar(MAX), {2} int);"\ u"INSERT INTO {0} VALUES (N'value_{3}1', 1);"\ u"INSERT INTO {0} VALUES (N'value_{3}2', 2);"\ .format(table_name, col1_name, col2_name, test_str) if not self.run_query(client, setup_query): assert False #should not fail select_query = u"SELECT {0}, {1} FROM {2};".format( col1_name, col2_name, table_name) for rows, columns, _, _, is_error in client.execute_query( select_query): assert not is_error assert len(columns) == 2 assert columns[0] == col1_name assert columns[1] == col2_name assert len(rows) == 2 assert rows[0][0] == u'value_{0}1'.format(test_str) assert rows[1][0] == u'value_{0}2'.format(test_str) finally: shutdown(client)
def create_test_db(): """ Creates database for test, using various status checks and retry logic for reliability. - Calls helper method to check status of create db task, if possible - Exits on successful response or retry period exceeds time limit """ options = create_mssql_cli_options(database='master') client = create_mssql_cli_client(options) local_machine_name = socket.gethostname().replace('-', '_').replace('.', '_') test_db_name = u'mssqlcli_testdb_{0}_{1}'.format(local_machine_name, random_str()) query_db_create = u"CREATE DATABASE {0};".format(test_db_name) try: for _, _, status, _, is_create_error in client.execute_query( query_db_create): if _is_client_db_on_cloud(client): # retry logic is only supported for sql azure create_db_status, create_db_error = _check_create_db_status( test_db_name, client) if create_db_status == 'FAILED': # break loop to assert db creation failure raise AssertionError("Database creation failed. Retry logic for SQL " \ "Azure DB was unsuccessful with the following error: " \ "\n{}".format(create_db_error)) if is_create_error: # break loop to assert db creation failure raise AssertionError( "Database creation failed: {}".format(status)) return test_db_name finally: shutdown(client)