| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| """System tests for Jupyter/IPython connector.""" |
|
|
| import re |
|
|
| import pytest |
| import psutil |
|
|
|
|
| IPython = pytest.importorskip("IPython") |
| io = pytest.importorskip("IPython.utils.io") |
| pandas = pytest.importorskip("pandas") |
| tools = pytest.importorskip("IPython.testing.tools") |
| interactiveshell = pytest.importorskip("IPython.terminal.interactiveshell") |
|
|
|
|
| @pytest.fixture(scope="session") |
| def ipython(): |
| config = tools.default_config() |
| config.TerminalInteractiveShell.simple_prompt = True |
| shell = interactiveshell.TerminalInteractiveShell.instance(config=config) |
| return shell |
|
|
|
|
| @pytest.fixture() |
| def ipython_interactive(ipython): |
| """Activate IPython's builtin hooks |
| |
| for the duration of the test scope. |
| """ |
| with ipython.builtin_trap: |
| yield ipython |
|
|
|
|
| def test_bigquery_magic(ipython_interactive): |
| ip = IPython.get_ipython() |
| current_process = psutil.Process() |
| conn_count_start = len(current_process.connections()) |
|
|
| |
| with pytest.warns(FutureWarning, match="bigquery_magics"): |
| ip.extension_manager.load_extension("google.cloud.bigquery") |
|
|
| sql = """ |
| SELECT |
| CONCAT( |
| 'https://stackoverflow.com/questions/', |
| CAST(id as STRING)) as url, |
| view_count |
| FROM `bigquery-public-data.stackoverflow.posts_questions` |
| WHERE tags like '%google-bigquery%' |
| ORDER BY view_count DESC |
| LIMIT 10 |
| """ |
| with io.capture_output() as captured: |
| result = ip.run_cell_magic("bigquery", "--use_rest_api", sql) |
|
|
| conn_count_end = len(current_process.connections()) |
|
|
| lines = re.split("\n|\r", captured.stdout) |
| |
| updates = list(filter(lambda x: bool(x) and x != "\x1b[2K", lines)) |
| assert re.match("Executing query with job ID: .*", updates[0]) |
| assert (re.match("Query executing: .*s", line) for line in updates[1:-1]) |
| assert isinstance(result, pandas.DataFrame) |
| assert len(result) == 10 |
| assert list(result) == ["url", "view_count"] |
|
|
| |
| |
| |
| assert conn_count_end <= conn_count_start |
|
|