|
| 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