Skip to content

Commit dd40abf

Browse files
committed
Added implementation + integration tests for #25
Signed-off-by: Steffen Pankratz <steffen.pankratz@exasol.com>
1 parent 2e8af31 commit dd40abf

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

exasol/pytest_slc/__init__.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import getpass
45
from importlib.metadata import version
56
from pathlib import Path
7+
from typing import Any
68

79
import exasol.bucketfs as bfs
10+
import pyexasol
811
import pytest
12+
from _pytest.fixtures import FixtureRequest
913
from exasol.pytest_backend import paralleltask
1014
from exasol.python_extension_common.deployment.language_container_builder import (
1115
LanguageContainerBuilder,
1216
)
1317
from exasol.python_extension_common.deployment.language_container_deployer import (
1418
LanguageActivationLevel,
1519
LanguageContainerDeployer,
20+
get_language_settings,
1621
)
1722
from exasol.slc.models.export_container_result import ( # noqa: F401
1823
ExportContainerResult,
@@ -24,6 +29,9 @@
2429

2530
__version__ = version("pytest-exasol-slc")
2631

32+
from pyexasol import ExaConnection
33+
34+
SCRIPT_LANGUAGES_OPTION = "--script-languages"
2735
SKIP_SLC_OPTION = "--skip-slc"
2836
BFS_CONTAINER_DIRECTORY = "container"
2937

@@ -32,6 +40,11 @@ def pytest_addoption(parser):
3240
parser.addoption(
3341
SKIP_SLC_OPTION, action="store_true", default=False, help="Skip SLC deployment"
3442
)
43+
parser.addoption(
44+
SCRIPT_LANGUAGES_OPTION,
45+
default=None,
46+
help="Script language definition",
47+
)
3548

3649

3750
@pytest.fixture(scope="session")
@@ -62,7 +75,16 @@ def export_slc_async(
6275
"""
6376
The fixture starts the export() function of the provided
6477
LanguageContainerBuilder object as an asynchronous task.
78+
"""
79+
80+
"""
81+
The operation will be skipped if SCRIPT_LANGUAGES_OPTION is defined.
82+
"""
83+
if request.config.getoption(SCRIPT_LANGUAGES_OPTION):
84+
yield None
85+
return
6586

87+
"""
6688
The operation will be skipped if none of the backends is in use or the
6789
container builder is not defined or the SLC deployment is skipped.
6890
"""
@@ -173,3 +195,50 @@ def deployed_slc(deploy_slc, language_alias) -> str:
173195
"""
174196
deploy_slc(language_alias)
175197
return language_alias
198+
199+
200+
def set_script_languages(
201+
pyexasol_connection: ExaConnection, script_languages: Any | None
202+
):
203+
query = "ALTER SESSION SET SCRIPT_LANGUAGES={script_languages}"
204+
pyexasol_connection.execute(
205+
query, query_params={"script_languages": script_languages}
206+
)
207+
208+
209+
@pytest.fixture(scope="session")
210+
def script_languages(request: FixtureRequest) -> str:
211+
script_languages = request.config.getoption(SCRIPT_LANGUAGES_OPTION)
212+
if not script_languages or script_languages == "":
213+
raise RuntimeError(f"Value for {SCRIPT_LANGUAGES_OPTION} missing")
214+
return script_languages
215+
216+
217+
@contextlib.contextmanager
218+
def activate_script_languages(
219+
pyexasol_connection_: ExaConnection, script_languages_: str
220+
):
221+
# get script languages currently used
222+
current_script_languages = get_language_settings(
223+
pyexasol_connection_, LanguageActivationLevel.Session
224+
)
225+
set_script_languages(pyexasol_connection_, script_languages_)
226+
yield
227+
# reset script languages
228+
set_script_languages(pyexasol_connection_, current_script_languages)
229+
230+
231+
@pytest.fixture(scope="session")
232+
def activate_script_languages_for_session(
233+
script_languages, pyexasol_connection: pyexasol.ExaConnection
234+
):
235+
with activate_script_languages(pyexasol_connection, script_languages):
236+
yield
237+
238+
239+
@pytest.fixture(scope="module")
240+
def activate_script_languages_for_module(
241+
script_languages, pyexasol_connection: pyexasol.ExaConnection
242+
):
243+
with activate_script_languages(pyexasol_connection, script_languages):
244+
yield
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pytest
2+
from exasol.pytest_backend import (
3+
BACKEND_ONPREM,
4+
BACKEND_OPTION,
5+
)
6+
7+
from exasol.pytest_slc import SCRIPT_LANGUAGES_OPTION
8+
9+
pytest_plugins = ["pytester"]
10+
11+
12+
def test_empty_value_for_script_languages_raises_wrapper(pytester):
13+
pytester.makepyfile("""
14+
import pytest
15+
16+
def test_empty_value_for_script_languages_raises(script_languages):
17+
assert True
18+
""")
19+
result = pytester.runpytest(
20+
BACKEND_OPTION, BACKEND_ONPREM, SCRIPT_LANGUAGES_OPTION, ""
21+
)
22+
assert result.ret == pytest.ExitCode.TESTS_FAILED
23+
result.assert_outcomes(errors=1)
24+
25+
26+
def test_setting_script_languages_wrapper(pytester):
27+
expected_script_languages = r"PYTHON3=PYTEST_SCL_INTEGRATION_TEST"
28+
pytester.makepyfile(f"""
29+
import pytest
30+
from exasol.python_extension_common.deployment.language_container_deployer import (
31+
LanguageActivationLevel,
32+
get_language_settings,
33+
)
34+
35+
def test_empty_value_for_script_languages_raises(activate_script_languages_for_module, pyexasol_connection):
36+
current_script_languages = get_language_settings(
37+
pyexasol_connection, LanguageActivationLevel.Session
38+
)
39+
assert current_script_languages == "{expected_script_languages}"
40+
""")
41+
result = pytester.runpytest(
42+
BACKEND_OPTION,
43+
BACKEND_ONPREM,
44+
SCRIPT_LANGUAGES_OPTION,
45+
expected_script_languages,
46+
)
47+
assert result.ret == pytest.ExitCode.OK
48+
result.assert_outcomes(passed=1, skipped=1)

0 commit comments

Comments
 (0)