Skip to content

Commit

Permalink
Fixed integration test 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ckunki committed Oct 22, 2024
1 parent ee0b129 commit f39824f
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 31 deletions.
2 changes: 0 additions & 2 deletions doc/user_guide/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ create schema IF NOT EXISTS "TEMP_SCHEMA";
--/
CREATE OR REPLACE PYTHON3_AAF SET SCRIPT "MY_SCHEMA"."MY_QUERY_HANDLER_UDF"(...)
EMITS (outputs VARCHAR(2000000)) AS

from typing import Union
from exasol_advanced_analytics_framework.udf_framework.udf_query_handler import UDFQueryHandler
from exasol_advanced_analytics_framework.udf_framework.dynamic_modules import create_module
Expand Down Expand Up @@ -298,7 +297,6 @@ def run(ctx):
return udf.run(ctx)

/

EXECUTE SCRIPT MY_SCHEMA.AAF_RUN_QUERY_HANDLER('{
"query_handler": {
"factory_class": {
Expand Down
71 changes: 62 additions & 9 deletions exasol_advanced_analytics_framework/example/generator.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import json
import importlib.resources
from jinja2 import Template, Environment, PackageLoader, select_autoescape
from jinja2 import Template, Environment, PackageLoader, BaseLoader, select_autoescape
from pathlib import Path
from exasol_advanced_analytics_framework.deployment import constants
from exasol_advanced_analytics_framework.deployment.jinja_template_location import JinjaTemplateLocation
from typing import Any, Dict

PACKAGE_PATH = "example"

QUERY_HANDLER_SCRIPT = {
SCRIPT_ARGUMENTS = {
"query_handler": {
"factory_class": {
"module": "xyz",
Expand All @@ -29,6 +29,30 @@
},
}

# CREATE_SCRIPT = """--/
# CREATE OR REPLACE PYTHON3_AAF SET SCRIPT
# "{{ query_handler.udf.schema }}"."{{ query_handler.udf.name }}"(...)
# EMITS (outputs VARCHAR(2000000)) AS
# {{ python_code }}
# /
# """

CREATE_SCRIPT = (
'--/\n'
'CREATE OR REPLACE PYTHON3_AAF SET SCRIPT'
' "{{ query_handler.udf.schema }}"."{{ query_handler.udf.name }}"(...)\n'
'EMITS (outputs VARCHAR(2000000)) AS\n'
'{{ python_code }}\n'
'/\n'
)


EXECUTE_SCRIPT = (
"EXECUTE SCRIPT {{ query_handler.udf.schema }}"
".AAF_RUN_QUERY_HANDLER("
"'{{ json_string }}')"
)

def jinja_env():
return Environment(
loader=PackageLoader(
Expand All @@ -44,16 +68,45 @@ def quoted_udf_name(query_handler_script: Dict[str, Any]):
return f'"{schema}"."{name}"'


def generate(query_handler_script=QUERY_HANDLER_SCRIPT):
env = jinja_env()
def render_template(template: str, **kwargs) -> str:
return (
Environment(loader=BaseLoader)
.from_string(template)
.render(**kwargs)
)


def create_script(script_arguments=SCRIPT_ARGUMENTS):
python_code = importlib.resources.read_text(
f"{constants.BASE_DIR}.{PACKAGE_PATH}",
"query_handler.py",
)
json_code = json.dumps(query_handler_script, indent=4)
template = env.get_template("sql.jinja")
return template.render(
return render_template(
CREATE_SCRIPT,
python_code=python_code,
json_code=json_code,
**query_handler_script,
**script_arguments,
)


def execute_script(script_arguments=SCRIPT_ARGUMENTS):
json_string = json.dumps(script_arguments, indent=4)
return render_template(
EXECUTE_SCRIPT,
json_string=json_string,
**script_arguments,
)


# def generate(query_handler_script=SCRIPT_ARGUMENTS):
# env = jinja_env()
# python_code = importlib.resources.read_text(
# f"{constants.BASE_DIR}.{PACKAGE_PATH}",
# "query_handler.py",
# )
# json_code = json.dumps(query_handler_script, indent=4)
# template = env.get_template("sql.jinja")
# return template.render(
# python_code=python_code,
# json_code=json_code,
# **query_handler_script,
# )
3 changes: 2 additions & 1 deletion scripts/document_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def render(self):
" parts are in python. Formally, however, the python code is embedded into",
" an SQL statement, though. -->",
"```python",
example_generator.generate(),
example_generator.create_script(),
example_generator.execute_script() + ";",
"```",
""])

Expand Down
47 changes: 28 additions & 19 deletions tests/integration_tests/with_db/test_user_guide_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,31 @@
import generator as example_generator


def generate_example(bfs_connection_name: str, schema_name: str):
script = dict(example_generator.QUERY_HANDLER_SCRIPT)
script["query_handler"]["udf"]["schema"] = schema_name
script["temporary_output"]["bucketfs_location"]["connection_name"] = bfs_connection_name
script["temporary_output"]["schema_name"] = schema_name
return example_generator.generate(script)
def script_args(bfs_connection_name: str, schema_name: str):
args = dict(example_generator.SCRIPT_ARGUMENTS)
args["query_handler"]["udf"]["schema"] = schema_name
args["temporary_output"]["bucketfs_location"]["connection_name"] = bfs_connection_name
args["temporary_output"]["schema_name"] = schema_name
return args


def test_x1(request):
# opt = request.config.getoption("--exasol-host")
# print(f'{opt}')
# return
example_code = generate_example("BBB", "SSS")
print(f'{example_code}')
result = [[(
"Final result: from query"
" '2024-10-21 12:26:00 table-insert bla-bla', 4"
" and bucketfs: '2024-10-21 12:26:00 bucketfs bla-bla'"
)]]
import pyexasol
@pytest.mark.skip("local")
def test_x2():
pyexasol_connection = pyexasol.connect(
dsn="192.168.124.221:8563",
user="SYS",
password="exasol",
)
bucketfs_connection_name, schema_name = ("BFS_CON", "MY_SCHEMA")
args = script_args(bucketfs_connection_name, schema_name)
statement = example_generator.create_script(args)
# print(f'create_script:\n{statement}')
pyexasol_connection.execute(statement)
statement = example_generator.execute_script(args)
# print(f'execute_script:\n{statement}')
result = pyexasol_connection.execute(statement).fetchall()
print(f'{result}')
expected = (
"Final result: from query '.* table-insert bla-bla', 4"
" and bucketfs: '.* bucketfs bla-bla'"
Expand All @@ -37,8 +43,11 @@ def test_user_guide_example(database_with_slc, pyexasol_connection):
own python module.
"""
bucketfs_connection_name, schema_name = database_with_slc
example_code = generate_example(bucketfs_connection_name, schema_name)
result = pyexasol_connection.execute(example_code).fetchall()
args = script_args(bucketfs_connection_name, schema_name)
statement = example_generator.create_script(args)
pyexasol_connection.execute(statement)
statement = example_generator.execute_script(args)
result = pyexasol_connection.execute(statement).fetchall()
expected = (
"Final result: from query '.* table-insert bla-bla', 4"
" and bucketfs: '.* bucketfs bla-bla'"
Expand Down

0 comments on commit f39824f

Please sign in to comment.