Skip to content

Commit

Permalink
#7 Moved to the bucketfs PathLike interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed May 16, 2024
1 parent ead6d48 commit ddcee78
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ def get_language_settings(pyexasol_conn: pyexasol.ExaConnection, alter_type: Lan
return result[0][0]


def get_udf_path(bucket_base_path: bfs.path.PathLike, bucket_file: str) -> PurePosixPath:
"""
Returns the path of the specified file in a bucket, as it's seen from a UDF
bucket_base_path - Base directory in the bucket
bucket_file - File path in the bucket, relative to the base directory.
"""
file_path = bucket_base_path / bucket_file
return PurePosixPath(file_path.as_udf_path())


class LanguageContainerDeployer:

def __init__(self,
Expand Down Expand Up @@ -191,17 +202,13 @@ def generate_activation_command(self, bucket_file_path: str,
allow_override - If True the activation of a language container with the same alias will be overriden,
otherwise a RuntimeException will be thrown.
"""
path_in_udf = self._get_udf_path(bucket_file_path)
path_in_udf = get_udf_path(self._bucketfs_path, bucket_file_path)
new_settings = \
self._update_previous_language_settings(alter_type, allow_override, path_in_udf)
alter_command = \
f"ALTER {alter_type.value} SET SCRIPT_LANGUAGES='{new_settings}';"
return alter_command

def _get_udf_path(self, bucket_file_path: str):
file_path = self._bucketfs_path / bucket_file_path
return file_path.as_udf_path()

def _update_previous_language_settings(self, alter_type: LanguageActivationLevel,
allow_override: bool,
path_in_udf: PurePosixPath) -> str:
Expand All @@ -219,7 +226,7 @@ def get_language_definition(self, bucket_file_path: str):
bucket_file_path - Path within the designated bucket where the container is uploaded.
"""
path_in_udf = self._get_udf_path(bucket_file_path)
path_in_udf = get_udf_path(self._bucketfs_path, bucket_file_path)
result = self._generate_new_language_settings(path_in_udf=path_in_udf, prev_lang_aliases=[])
return result

Expand Down
55 changes: 34 additions & 21 deletions test/unit/deployment/test_language_container_deployer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,49 +34,51 @@ def mock_pyexasol_conn() -> ExaConnection:
return create_autospec(ExaConnection)


@pytest.fixture(scope='module')
def mock_bfs_path(container_bfs_path) -> bfs.path.PathLike:
mock_loc = create_autospec(bfs.path.PathLike)
mock_loc.generate_bucket_udf_path.return_value = PurePosixPath(f'/buckets/{container_bfs_path}')
return mock_loc


@pytest.fixture
def container_deployer(mock_pyexasol_conn, mock_bfs_path, language_alias) -> LanguageContainerDeployer:
def container_deployer(mock_pyexasol_conn, language_alias) -> LanguageContainerDeployer:
deployer = LanguageContainerDeployer(pyexasol_connection=mock_pyexasol_conn,
language_alias=language_alias,
bucketfs_path=mock_bfs_path)
bucketfs_path=create_autospec(bfs.path.PathLike))

deployer.upload_container = MagicMock()
deployer.activate_container = MagicMock()
return deployer


def test_slc_deployer_deploy(container_deployer, container_file_name, container_file_path):
container_deployer.run(container_file=container_file_path, bucket_file_path=container_file_name, alter_system=True,
container_deployer.run(container_file=container_file_path,
bucket_file_path=container_file_name,
alter_system=True,
allow_override=True)
container_deployer.upload_container.assert_called_once_with(container_file_path, container_file_name)
container_deployer.activate_container.assert_called_once_with(container_file_name, LanguageActivationLevel.System,
container_deployer.upload_container.assert_called_once_with(container_file_path,
container_file_name)
container_deployer.activate_container.assert_called_once_with(container_file_name,
LanguageActivationLevel.System,
True)


def test_slc_deployer_upload(container_deployer, container_file_name, container_file_path):
container_deployer.run(container_file=container_file_path, alter_system=False)
container_deployer.upload_container.assert_called_once_with(container_file_path, container_file_name)
container_deployer.upload_container.assert_called_once_with(container_file_path,
container_file_name)
container_deployer.activate_container.assert_not_called()


def test_slc_deployer_activate(container_deployer, container_file_name):
container_deployer.run(bucket_file_path=container_file_name, alter_system=True, allow_override=True)
container_deployer.upload_container.assert_not_called()
container_deployer.activate_container.assert_called_once_with(container_file_name, LanguageActivationLevel.System,
container_deployer.activate_container.assert_called_once_with(container_file_name,
LanguageActivationLevel.System,
True)


@patch('exasol.python_extension_common.deployment.language_container_deployer.get_udf_path')
@patch('exasol.python_extension_common.deployment.language_container_deployer.get_language_settings')
def test_slc_deployer_generate_activation_command(mock_lang_settings, container_deployer, language_alias,
def test_slc_deployer_generate_activation_command(mock_lang_settings, mock_udf_path,
container_deployer, language_alias,
container_file_name, container_bfs_path):
mock_lang_settings.return_value = 'R=builtin_r JAVA=builtin_java PYTHON3=builtin_python3'
mock_udf_path.return_value = PurePosixPath(f'/buckets/{container_bfs_path}')

alter_type = LanguageActivationLevel.Session
expected_command = f"ALTER {alter_type.value.upper()} SET SCRIPT_LANGUAGES='" \
Expand All @@ -88,41 +90,52 @@ def test_slc_deployer_generate_activation_command(mock_lang_settings, container_
assert command == expected_command


@patch('exasol.python_extension_common.deployment.language_container_deployer.get_udf_path')
@patch('exasol.python_extension_common.deployment.language_container_deployer.get_language_settings')
def test_slc_deployer_generate_activation_command_override(mock_lang_settings, container_deployer, language_alias,
def test_slc_deployer_generate_activation_command_override(mock_lang_settings, mock_udf_path,
container_deployer, language_alias,
container_file_name, container_bfs_path):
current_bfs_path = 'bfsdefault/default/container_abc'
mock_lang_settings.return_value = \
'R=builtin_r JAVA=builtin_java PYTHON3=builtin_python3 ' \
f'{language_alias}=localzmq+protobuf:///{current_bfs_path}?' \
f'lang=python#/buckets/{current_bfs_path}/exaudf/exaudfclient_py3'
mock_udf_path.return_value = PurePosixPath(f'/buckets/{container_bfs_path}')

alter_type = LanguageActivationLevel.Session
expected_command = f"ALTER {alter_type.value.upper()} SET SCRIPT_LANGUAGES='" \
"R=builtin_r JAVA=builtin_java PYTHON3=builtin_python3 " \
f"{language_alias}=localzmq+protobuf:///{container_bfs_path}?" \
f"lang=python#/buckets/{container_bfs_path}/exaudf/exaudfclient_py3';"

command = container_deployer.generate_activation_command(container_file_name, alter_type, allow_override=True)
command = container_deployer.generate_activation_command(container_file_name, alter_type,
allow_override=True)
assert command == expected_command


@patch('exasol.python_extension_common.deployment.language_container_deployer.get_udf_path')
@patch('exasol.python_extension_common.deployment.language_container_deployer.get_language_settings')
def test_slc_deployer_generate_activation_command_failure(mock_lang_settings, container_deployer, language_alias,
container_file_name):
def test_slc_deployer_generate_activation_command_failure(mock_lang_settings, mock_udf_path,
container_deployer, language_alias,
container_file_name, container_bfs_path):
current_bfs_path = 'bfsdefault/default/container_abc'
mock_lang_settings.return_value = \
'R=builtin_r JAVA=builtin_java PYTHON3=builtin_python3 ' \
f'{language_alias}=localzmq+protobuf:///{current_bfs_path}?' \
f'lang=python#/buckets/{current_bfs_path}/exaudf/exaudfclient_py3'
mock_udf_path.return_value = PurePosixPath(f'/buckets/{container_bfs_path}')

with pytest.raises(RuntimeError):
container_deployer.generate_activation_command(container_file_name, LanguageActivationLevel.Session,
container_deployer.generate_activation_command(container_file_name,
LanguageActivationLevel.Session,
allow_override=False)


def test_slc_deployer_get_language_definition(container_deployer, language_alias,
@patch('exasol.python_extension_common.deployment.language_container_deployer.get_udf_path')
def test_slc_deployer_get_language_definition(mock_udf_path,
container_deployer, language_alias,
container_file_name, container_bfs_path):
mock_udf_path.return_value = PurePosixPath(f'/buckets/{container_bfs_path}')
expected_command = f"{language_alias}=localzmq+protobuf:///{container_bfs_path}?" \
f"lang=python#/buckets/{container_bfs_path}/exaudf/exaudfclient_py3"

Expand Down

0 comments on commit ddcee78

Please sign in to comment.