Skip to content

Commit

Permalink
#58 Made the pytest-slc plugin using the pyexasol_connection (#59)
Browse files Browse the repository at this point in the history
* #58 Made the plugin using the pyexasol_connection

* #58 Made a change in the upload_slc fixture

* Apply suggestions from code review

Co-authored-by: Christoph Kuhnke <[email protected]>

* #58 Made two versions of the deploy_slc fixture

* #58 Made two versions of the deploy_slc fixture

* #58 Made two versions of the deploy_slc fixture

* #58 Applied the suggestion from the review.

---------

Co-authored-by: Christoph Kuhnke <[email protected]>
  • Loading branch information
ahsimb and ckunki authored Sep 23, 2024
1 parent 3496613 commit d1dfad9
Show file tree
Hide file tree
Showing 9 changed files with 352 additions and 240 deletions.
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PROJECTS := "pytest-extension pytest-slc pytest-backend pytest-saas pytest-itde"
PROJECTS := "pytest-slc pytest-extension pytest-backend pytest-saas pytest-itde"

# Default target
default:
Expand Down
35 changes: 32 additions & 3 deletions pytest-slc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,36 @@ pip install pytest-exasol-slc

## Usage in Tests

Below is an example of a test that requires a script language container to be built and uploaded into the database.
Note, that by default this test will run twice - once for each backend.
Below is an example of a test that requires a script language container to be built and deployed in the database.
The example test case overrides two fixtures used internally by the fixture `deployed_slc` in this plugin:
* Fixture `language_alias` provides a meaningful name for the language alias.
* Fixture `slc_builder` prepares the structure of the SLC used in the example test case.

The language container will be activated with value returned by the fixture `language_alias`.
Note, that by default the test will run twice - once for each backend.

```python
import pytest

@pytest.fixture(scope='session')
def language_alias():
return "MY_LANGUAGE_ALIAS"

@pytest.fixture(scope='session')
def slc_builder(use_onprem, use_saas):
if use_onprem or use_saas:
with language_container_factory() as container_builder:
yield container_builder
else:
yield None

def test_something_with_slc(deployed_slc):
...
```

Alternatively, the language container can be deployed using the function version of this fixture. The function
can be called multiple times providing an opportunity to activate the language container with different
aliases.

```python
import pytest
Expand All @@ -28,6 +56,7 @@ def slc_builder(use_onprem, use_saas):
else:
yield None

def test_something_with_slc(upload_slc):
def test_something_with_slc(deploy_slc):
deploy_slc("MY_LANGUAGE_ALIAS")
...
```
2 changes: 2 additions & 0 deletions pytest-slc/doc/changes/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* [unreleased](unreleased.md)
* [0.1.0](changes_0.1.0.md)
* [0.2.0](changes_0.2.0.md)
* [0.3.0](changes_0.3.0.md)

<!--- This MyST Parser Sphinx directive is necessary to keep Sphinx happy. We need list here all release letters again, because release droid and other scripts assume Markdown --->
```{toctree}
Expand All @@ -12,5 +13,6 @@ hidden:
unreleased
changes_0.1.0
changes_0.2.0
changes_0.3.0
```
9 changes: 9 additions & 0 deletions pytest-slc/doc/changes/changes_0.3.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 0.3.0 - 2024-09-23

## Summary

🚀 Starting with this release `pytest-exasol-slc` connects to the database using a fixture from `pytest-exasol-extension`.

## Bug fixes

* #58: Used the `pyexasol_connection` fixture from the `pytest-exasol-extension`.
60 changes: 46 additions & 14 deletions pytest-slc/exasol/pytest_slc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from __future__ import annotations
from pathlib import Path
import getpass
from exasol.pytest_backend import paralleltask
import pytest

import pyexasol
import exasol.bucketfs as bfs
from exasol.python_extension_common.deployment.language_container_deployer import LanguageContainerDeployer
from exasol.python_extension_common.deployment.language_container_deployer import (
LanguageContainerDeployer, LanguageActivationLevel)
from exasol.python_extension_common.deployment.language_container_builder import LanguageContainerBuilder

BFS_CONTAINER_DIRECTORY = 'container'
Expand Down Expand Up @@ -63,18 +64,49 @@ def export_slc(slc_builder, export_slc_async) -> Path | None:
return Path(export_info.cache_file)


@pytest.fixture(scope='session')
def language_alias(project_short_tag):
"""
The user can override this fixture if they want to provide a more meaningful
name for the language alias.
"""
owner = getpass.getuser()
return f"PYTHON3-{project_short_tag or 'EXTENSION'}-{owner}"


@pytest.fixture(scope="session")
def deploy_slc(slc_builder, export_slc,
pyexasol_connection, backend_aware_bucketfs_params):
"""
The fixture provides a function deploying the language container in a database.
The function can be called multiple times, allowing to activate the container with
multiple aliases. However, the container will be uploaded only once.
"""
bucket_file_path = ''

def func(language_alias: str) -> None:
nonlocal bucket_file_path
if (slc_builder is not None) and (export_slc is not None):
bucketfs_path = bfs.path.build_path(**backend_aware_bucketfs_params,
path=BFS_CONTAINER_DIRECTORY)
deployer = LanguageContainerDeployer(pyexasol_connection=pyexasol_connection,
bucketfs_path=bucketfs_path,
language_alias=language_alias)
if bucket_file_path:
# The container has already been uploaded and just needs to be activated
for alter_type in [LanguageActivationLevel.Session, LanguageActivationLevel.System]:
deployer.activate_container(bucket_file_path, alter_type, allow_override=True)
else:
bucket_file_path = export_slc.name
deployer.run(container_file=export_slc, bucket_file_path=bucket_file_path,
alter_system=True, allow_override=True)
return func


@pytest.fixture(scope="session")
def upload_slc(slc_builder, export_slc,
backend_aware_database_params, backend_aware_bucketfs_params):
def deployed_slc(deploy_slc, language_alias) -> None:
"""
The fixture uploads language container to a database, according to the selected
backends.
The fixture calls deploy_slc() once, with the language_alis defined in the fixture
with the corresponded name.
"""
if (slc_builder is not None) and (export_slc is not None):
pyexasol_connection = pyexasol.connect(**backend_aware_database_params)
bucketfs_path = bfs.path.build_path(**backend_aware_bucketfs_params,
path=BFS_CONTAINER_DIRECTORY)
deployer = LanguageContainerDeployer(pyexasol_connection=pyexasol_connection,
bucketfs_path=bucketfs_path,
language_alias=slc_builder.language_alias)
deployer.run(container_file=export_slc, alter_system=True, allow_override=True)
deploy_slc(language_alias)
2 changes: 1 addition & 1 deletion pytest-slc/exasol/pytest_slc/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
# Do not edit this file manually!
# If you need to change the version, do so in the project.toml, e.g. by using `poetry version X.Y.Z`.
MAJOR = 0
MINOR = 2
MINOR = 3
PATCH = 0
VERSION = f"{MAJOR}.{MINOR}.{PATCH}"
Loading

0 comments on commit d1dfad9

Please sign in to comment.