-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for udf script output (#137)
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pytest | ||
import pyexasol | ||
from inspect import cleandoc | ||
|
||
|
||
@pytest.fixture | ||
def logging_address(): | ||
# 172.17.0.1 is an IP address of docker host in Linux | ||
yield ("172.17.0.1", 8580) | ||
|
||
|
||
@pytest.fixture | ||
def connection(dsn, user, password, schema, logging_address): | ||
_, port = logging_address | ||
con = pyexasol.connect( | ||
dsn=dsn, | ||
user=user, | ||
password=password, | ||
schema=schema, | ||
udf_output_bind_address=("", port), | ||
udf_output_connect_address=logging_address, | ||
) | ||
|
||
yield con | ||
|
||
con.close() | ||
|
||
|
||
@pytest.fixture | ||
def echo(connection, logging_address): | ||
name = "ECHO" | ||
ip, port = logging_address | ||
udf = cleandoc( | ||
f""" | ||
--/ | ||
CREATE OR REPLACE LUA SCALAR SCRIPT {name}(text VARCHAR(2000)) | ||
RETURNS VARCHAR(2000) AS | ||
function run(ctx) | ||
local socket = require("socket") | ||
local tcp_socket = socket.tcp() | ||
local ok, err = tcp_socket:connect("{ip}", {port}) | ||
tcp_socket:send(ctx.text) | ||
tcp_socket:shutdown() | ||
return ctx.text | ||
end | ||
/ | ||
""" | ||
) | ||
connection.execute(udf) | ||
|
||
def executor(text): | ||
udf_call = f"SELECT {name}('{text}');" | ||
stmt, logfiles = connection.execute_udf_output(udf_call) | ||
result = stmt.fetchval() | ||
log = "LOG: " + "".join((log.read_text() for log in logfiles)) | ||
return result, log | ||
|
||
yield executor | ||
|
||
drop = f"DROP SCRIPT IF EXISTS {name};" | ||
connection.execute(drop) | ||
|
||
|
||
@pytest.mark.udf | ||
def test_udf_output(connection, echo): | ||
text = "Some text which should be echoed" | ||
|
||
result, log = echo(text) | ||
|
||
expected_result = text | ||
expected_log = f"LOG: {text}" | ||
|
||
actual_result = result | ||
actual_log = log | ||
|
||
assert actual_result == expected_result | ||
assert actual_log == expected_log |