def __init__(self, connection: ConnectionInfo, downloaddir: str, targetpackage: str): self._connection = connection self._downloaddir = downloaddir self._targetpackage = targetpackage server_info = SQLPythonExecutor(connection).execute_function_in_sql( servermethods.get_server_info) globals().update(server_info)
def __init__(self, connection_info: ConnectionInfo, language_name: str = "Python"): """Initialize a SQLPackageManager to manage packages on the SQL Server. :param connection_info: The ConnectionInfo object that holds the connection string and other information. :param language_name: The name of the language to be executed in sp_execute_external_script, if using EXTERNAL LANGUAGE. """ self._connection_info = connection_info self._pyexecutor = SQLPythonExecutor(connection_info, language_name=language_name) self._language_name = language_name
def test_scope(): _remove_all_new_packages(pkgmanager) package = os.path.join(path_to_packages, "testpackageA-0.0.1.zip") def get_location(): import testpackageA return testpackageA.__file__ _revotesterconnection = sqlmlutils.ConnectionInfo(server="localhost", database="AirlineTestDB", uid="Tester", pwd="FakeT3sterPwd!") revopkgmanager = SQLPackageManager(_revotesterconnection) revoexecutor = SQLPythonExecutor(_revotesterconnection) revopkgmanager.install(package, scope=Scope.private_scope()) private_location = revoexecutor.execute_function_in_sql(get_location) pkg_name = "testpackageA" pyexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=False) revopkgmanager.uninstall(pkg_name, scope=Scope.private_scope()) revopkgmanager.install(package, scope=Scope.public_scope()) public_location = revoexecutor.execute_function_in_sql(get_location) assert private_location != public_location pyexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=True, class_to_check='ClassA') revopkgmanager.uninstall(pkg_name, scope=Scope.public_scope()) revoexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=False) pyexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=False)
def test_scope(): """Test installing in a private scope with a db_owner (not dbo) user""" _remove_all_new_packages(pkgmanager) package = os.path.join(path_to_packages, "testpackageA-0.0.1.zip") def get_location(): import testpackageA return testpackageA.__file__ # The airline_user_connection is NOT dbo, so it has access to both Private and Public scopes # revopkgmanager = SQLPackageManager(airline_user_connection) revoexecutor = SQLPythonExecutor(airline_user_connection) # Install a package into the private scope # revopkgmanager.install(package, scope=Scope.private_scope()) private_location = revoexecutor.execute_function_in_sql(get_location) pkg_name = "testpackageA" pyexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=False) revopkgmanager.uninstall(pkg_name, scope=Scope.private_scope()) # Try the same installation in public scope # revopkgmanager.install(package, scope=Scope.public_scope()) public_location = revoexecutor.execute_function_in_sql(get_location) assert private_location != public_location pyexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=True, class_to_check='ClassA') revopkgmanager.uninstall(pkg_name, scope=Scope.public_scope()) # Make sure the package was removed properly # revoexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=False) pyexecutor.execute_function_in_sql(check_package, package_name=pkg_name, exists=False)
from contextlib import redirect_stdout from conftest import connection def _drop_all_ddl_packages(conn): pkgs = _get_sql_package_table(conn) for pkg in pkgs: try: SQLPackageManager(conn)._drop_sql_package( pkg['name'], scope=Scope.private_scope()) except Exception: pass pyexecutor = SQLPythonExecutor(connection) pkgmanager = SQLPackageManager(connection) _drop_all_ddl_packages(connection) def _package_exists(module_name: str): mod = __import__(module_name) return mod is not None def _package_no_exist(module_name: str): import pytest with pytest.raises(Exception): __import__(module_name) return True
def __init__(self, connection_info: ConnectionInfo): self._connection_info = connection_info self._pyexecutor = SQLPythonExecutor(connection_info)
from contextlib import redirect_stdout, redirect_stderr from pandas import DataFrame from sqlmlutils import ConnectionInfo, SQLPythonExecutor from conftest import driver, server, database, uid, pwd connection = ConnectionInfo(driver=driver, server=server, database=database, uid=uid, pwd=pwd) current_dir = os.path.dirname(__file__) script_dir = os.path.join(current_dir, "scripts") sqlpy = SQLPythonExecutor(connection) def test_with_named_args(): def func_with_args(arg1, arg2): print(arg1) return arg2 output = io.StringIO() with redirect_stderr(output), redirect_stdout(output): res = sqlpy.execute_function_in_sql(func_with_args, arg1="str1", arg2="str2") assert "str1" in output.getvalue() assert res == "str2"