Skip to content

Commit ea96e6a

Browse files
authored
#354: convert utils/jupysql_init from ipynb to py (#355)
#354: convert utils/jupysql_init from ipynb to py (#355)
1 parent 61f73ae commit ea96e6a

4 files changed

Lines changed: 104 additions & 0 deletions

File tree

doc/changes/unreleased.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Unreleased
2+
* #354: converted utils/jupysql_init from ipynb to py
23

34
## Summary
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Initializing a JupySQL session
2+
# This script is not intended to be run independently.
3+
# It brings in the sql magic and performs common DB initialization steps,
4+
# including opening the default schema and activating
5+
# UDF languages at the session level.
6+
7+
from IPython import get_ipython
8+
9+
from exasol.nb_connector.connections import open_sqlalchemy_connection
10+
from exasol.nb_connector.language_container_activation import get_activation_sql
11+
12+
13+
def init_jupysql(ai_lab_config):
14+
open_sqlalchemy_connection(ai_lab_config)
15+
ipy = get_ipython()
16+
if ipy is None:
17+
raise RuntimeError(
18+
"Not running inside IPython. Magic commands will not execute."
19+
)
20+
ipy.run_line_magic("load_ext", "sql")
21+
ipy.run_line_magic("sql", "engine")
22+
ipy.run_line_magic("config", "SqlMagic.short_errors = False")
23+
ipy.run_line_magic("sql", f"OPEN SCHEMA {ai_lab_config.db_schema}")
24+
activation_sql = get_activation_sql(ai_lab_config)
25+
ipy.run_line_magic("sql", activation_sql)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from pathlib import Path
2+
3+
import pytest
4+
5+
from exasol.nb_connector.ai_lab_config import AILabConfig as CKey
6+
from exasol.nb_connector.secret_store import Secrets
7+
from exasol.nb_connector.ui import jupysql_init
8+
from exasol.nb_connector.ui.jupysql_init import init_jupysql
9+
10+
11+
def test_jupysql_python_execution(tmp_path):
12+
config_path = tmp_path / "dummy_config_store.sqlite"
13+
store_password = "store_password"
14+
secrets = Secrets(config_path, master_password=store_password)
15+
secrets.save(CKey.db_schema, "SCHEMA")
16+
secrets.save(CKey.db_host_name, "localhost")
17+
secrets.save(CKey.db_port, "8563")
18+
secrets.save(CKey.db_user, "user")
19+
secrets.save(CKey.db_password, "password")
20+
secrets.save(CKey.storage_backend, "onprem")
21+
ai_lab_config = Secrets(Path(config_path), store_password)
22+
orig_get_ipython = jupysql_init.get_ipython
23+
jupysql_init.get_ipython = lambda: None
24+
try:
25+
with pytest.raises(
26+
RuntimeError,
27+
match="Not running inside IPython. Magic commands will not execute.",
28+
):
29+
init_jupysql(ai_lab_config)
30+
finally:
31+
jupysql_init.get_ipython = orig_get_ipython
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from unittest.mock import (
2+
MagicMock,
3+
call,
4+
patch,
5+
)
6+
7+
import pytest
8+
9+
from exasol.nb_connector.ui import jupysql_init
10+
11+
12+
def test_init_jupysql_ipython_none():
13+
"""This test is checking if proper error is coming when IPython is not there, like in normal python only."""
14+
with (
15+
patch("exasol.nb_connector.ui.jupysql_init.get_ipython", return_value=None),
16+
patch("exasol.nb_connector.ui.jupysql_init.open_sqlalchemy_connection"),
17+
):
18+
with pytest.raises(
19+
RuntimeError,
20+
match="Not running inside IPython. Magic commands will not execute.",
21+
):
22+
jupysql_init.init_jupysql(MagicMock())
23+
24+
25+
def test_init_jupysql_ipython_magics():
26+
"""This test is checking if all magic commands are running properly when IPython is there, like in notebook."""
27+
mock_ipy = MagicMock()
28+
with patch(
29+
"exasol.nb_connector.ui.jupysql_init.get_ipython", return_value=mock_ipy
30+
):
31+
with (
32+
patch("exasol.nb_connector.ui.jupysql_init.open_sqlalchemy_connection"),
33+
patch(
34+
"exasol.nb_connector.ui.jupysql_init.get_activation_sql",
35+
return_value="MOCK_SQL",
36+
),
37+
):
38+
mock_config = MagicMock()
39+
mock_config.db_schema = "MOCK_SCHEMA"
40+
jupysql_init.init_jupysql(mock_config)
41+
assert mock_ipy.mock_calls == [
42+
call.run_line_magic("load_ext", "sql"),
43+
call.run_line_magic("sql", "engine"),
44+
call.run_line_magic("config", "SqlMagic.short_errors = False"),
45+
call.run_line_magic("sql", "OPEN SCHEMA MOCK_SCHEMA"),
46+
call.run_line_magic("sql", "MOCK_SQL"),
47+
]

0 commit comments

Comments
 (0)