|
| 1 | +import textwrap |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import nbformat |
| 5 | +import papermill as pm |
| 6 | +import pytest |
| 7 | + |
| 8 | +pytestmark = pytest.mark.notebook |
| 9 | + |
| 10 | +NOTEBOOK_PATH = Path(__file__).parents[2] / "notebooks" / "spark_integration_example.ipynb" |
| 11 | + |
| 12 | +# --------------------------------------------------------------------------- |
| 13 | +# Mock pyspark |
| 14 | +# Replaces pyspark.sql.SparkSession with a fake one |
| 15 | +# --------------------------------------------------------------------------- |
| 16 | +_MOCK_PYSPARK = textwrap.dedent("""\ |
| 17 | + import sys |
| 18 | + import types |
| 19 | + from unittest.mock import MagicMock |
| 20 | +
|
| 21 | + def _make_fake_pyspark(): |
| 22 | + pyspark_mod = types.ModuleType("pyspark") |
| 23 | + sql_mod = types.ModuleType("pyspark.sql") |
| 24 | + pyspark_mod.sql = sql_mod |
| 25 | + sys.modules.setdefault("pyspark", pyspark_mod) |
| 26 | + sys.modules.setdefault("pyspark.sql", sql_mod) |
| 27 | + return pyspark_mod, sql_mod |
| 28 | +
|
| 29 | + _pyspark, _sql = _make_fake_pyspark() |
| 30 | +
|
| 31 | + _SHOW_CATALOGS = ( |
| 32 | + "+-------------+\\n" |
| 33 | + "|catalogName |\\n" |
| 34 | + "+-------------+\\n" |
| 35 | + "|spark_catalog|\\n" |
| 36 | + "|local |\\n" |
| 37 | + "+-------------+\\n" |
| 38 | + ) |
| 39 | + _SHOW_NAMESPACES = ( |
| 40 | + "+---------+\\n" |
| 41 | + "|namespace|\\n" |
| 42 | + "+---------+\\n" |
| 43 | + "|default |\\n" |
| 44 | + "+---------+\\n" |
| 45 | + ) |
| 46 | + _SHOW_TABLES = ( |
| 47 | + "+---------+-----------+-----------+\\n" |
| 48 | + "|namespace|tableName |isTemporary|\\n" |
| 49 | + "+---------+-----------+-----------+\\n" |
| 50 | + "|default |test_all |false |\\n" |
| 51 | + "+---------+-----------+-----------+\\n" |
| 52 | + ) |
| 53 | + _DESCRIBE_TABLE = ( |
| 54 | + "+--------------------+---------+-------+\\n" |
| 55 | + "|col_name |data_type|comment|\\n" |
| 56 | + "+--------------------+---------+-------+\\n" |
| 57 | + "|boolean_col |boolean |null |\\n" |
| 58 | + "|integer_col |integer |null |\\n" |
| 59 | + "+--------------------+---------+-------+\\n" |
| 60 | + ) |
| 61 | + _SQL_RESPONSES = { |
| 62 | + "SHOW CATALOGS": _SHOW_CATALOGS, |
| 63 | + "SHOW NAMESPACES": _SHOW_NAMESPACES, |
| 64 | + "SHOW TABLES FROM default": _SHOW_TABLES, |
| 65 | + "DESCRIBE TABLE default.test_all_types": _DESCRIBE_TABLE, |
| 66 | + } |
| 67 | +
|
| 68 | + def _make_df(output): |
| 69 | + df = MagicMock() |
| 70 | + df.show.side_effect = lambda *a, **kw: print(output, end="") |
| 71 | + return df |
| 72 | +
|
| 73 | + class _FakeBuilder: |
| 74 | + def remote(self, url): return self |
| 75 | + def getOrCreate(self): return _FakeSession() |
| 76 | +
|
| 77 | + class _FakeSession: |
| 78 | + builder = _FakeBuilder() |
| 79 | + def sql(self, query): |
| 80 | + key = query.strip().rstrip(";") |
| 81 | + output = _SQL_RESPONSES.get(key, "+------+\\n| col |\\n+------+\\n| val |\\n+------+\\n") |
| 82 | + return _make_df(output) |
| 83 | +
|
| 84 | + _FakeSparkSession = MagicMock(spec=object) |
| 85 | + _FakeSparkSession.builder = _FakeBuilder() |
| 86 | + _sql.SparkSession = _FakeSparkSession |
| 87 | +""") |
| 88 | + |
| 89 | + |
| 90 | +def get_all_stdout(nb: nbformat.NotebookNode) -> str: |
| 91 | + """Concatenate all stdout streams from every executed cell.""" |
| 92 | + return "".join( |
| 93 | + out.get("text", "") |
| 94 | + for cell in nb.cells |
| 95 | + for out in cell.get("outputs", []) |
| 96 | + if out.get("output_type") == "stream" and out.get("name") == "stdout" |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def _inject_mock_and_execute(notebook_path: Path, output_path: Path) -> nbformat.NotebookNode: |
| 101 | + """ |
| 102 | + Load the real notebook, prepend the mock-pyspark setup cell, write to a |
| 103 | + temporary copy and execute it with papermill. |
| 104 | + """ |
| 105 | + nb = nbformat.read(str(notebook_path), as_version=4) |
| 106 | + |
| 107 | + mock_cell = nbformat.v4.new_code_cell(_MOCK_PYSPARK) |
| 108 | + mock_cell.metadata["tags"] = ["injected-mock"] |
| 109 | + nb.cells.insert(0, mock_cell) |
| 110 | + |
| 111 | + patched_path = output_path.parent / "spark_patched.ipynb" |
| 112 | + nbformat.write(nb, str(patched_path)) |
| 113 | + |
| 114 | + return pm.execute_notebook(str(patched_path), str(output_path), kernel_name="python3") |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture(scope="session") |
| 118 | +def spark_nb(tmp_path_factory: pytest.TempPathFactory) -> nbformat.NotebookNode: |
| 119 | + out = tmp_path_factory.mktemp("nb_out") / "spark_integration_example_out.ipynb" |
| 120 | + return _inject_mock_and_execute(NOTEBOOK_PATH, out) |
| 121 | + |
| 122 | + |
| 123 | +class TestSmoke: |
| 124 | + def test_notebook_completes_without_error(self, spark_nb: nbformat.NotebookNode) -> None: |
| 125 | + assert spark_nb is not None |
| 126 | + |
| 127 | + def test_all_code_cells_executed(self, spark_nb: nbformat.NotebookNode) -> None: |
| 128 | + for cell in spark_nb.cells: |
| 129 | + if cell.cell_type == "code": |
| 130 | + assert cell.get("execution_count") is not None, f"Cell not executed:\n{cell.source[:80]}" |
| 131 | + |
| 132 | + |
| 133 | +class TestCellOutputs: |
| 134 | + def test_show_catalogs_lists_spark_catalog_and_local(self, spark_nb: nbformat.NotebookNode) -> None: |
| 135 | + stdout = get_all_stdout(spark_nb) |
| 136 | + assert "spark_catalog" in stdout |
| 137 | + assert "local" in stdout |
| 138 | + |
| 139 | + def test_show_namespaces_contains_default(self, spark_nb: nbformat.NotebookNode) -> None: |
| 140 | + assert "default" in get_all_stdout(spark_nb) |
| 141 | + |
| 142 | + def test_show_tables_produces_tabular_output(self, spark_nb: nbformat.NotebookNode) -> None: |
| 143 | + assert "+---------+-----------+-----------+" in get_all_stdout(spark_nb) |
| 144 | + |
| 145 | + def test_describe_table_lists_column_names(self, spark_nb: nbformat.NotebookNode) -> None: |
| 146 | + assert "col_name" in get_all_stdout(spark_nb) |
| 147 | + |
| 148 | + def test_describe_table_lists_data_types(self, spark_nb: nbformat.NotebookNode) -> None: |
| 149 | + stdout = get_all_stdout(spark_nb) |
| 150 | + assert "boolean" in stdout or "integer" in stdout |
| 151 | + |
| 152 | + def test_show_tables_includes_test_table_row(self, spark_nb: nbformat.NotebookNode) -> None: |
| 153 | + assert "test_all" in get_all_stdout(spark_nb) |
0 commit comments