11from __future__ import annotations
22
3+ import contextlib
34import getpass
45from importlib .metadata import version
56from pathlib import Path
7+ from typing import Any
68
79import exasol .bucketfs as bfs
10+ import pyexasol
811import pytest
12+ from _pytest .fixtures import FixtureRequest
913from exasol .pytest_backend import paralleltask
1014from exasol .python_extension_common .deployment .language_container_builder import (
1115 LanguageContainerBuilder ,
1216)
1317from exasol .python_extension_common .deployment .language_container_deployer import (
1418 LanguageActivationLevel ,
1519 LanguageContainerDeployer ,
20+ get_language_settings ,
1621)
1722from exasol .slc .models .export_container_result import ( # noqa: F401
1823 ExportContainerResult ,
2429
2530__version__ = version ("pytest-exasol-slc" )
2631
32+ from pyexasol import ExaConnection
33+
34+ SCRIPT_LANGUAGES_OPTION = "--script-languages"
2735SKIP_SLC_OPTION = "--skip-slc"
2836BFS_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
0 commit comments