Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

tests: snapshot ipynb convert tests, add juv test (#2851)

Myles Scolnick6cab3879bd8b
diff --git a/.gitignore b/.gitignore
index e7e5741624..5a679caa94 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,7 @@
*.csv
*.pdf
*.txt
+!tests/**/*.txt
# Byte-compiled / optimized / DLL files
__pycache__
diff --git a/tests/_cli/ipynb_data/juv.ipynb.txt b/tests/_cli/ipynb_data/juv.ipynb.txt
new file mode 100644
index 0000000000..633bbaa10d
--- /dev/null
+++ b/tests/_cli/ipynb_data/juv.ipynb.txt
@@ -0,0 +1,35 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "21add5b",
+ "metadata": {
+ "jupyter": {
+ "source_hidden": true
+ }
+ },
+ "outputs": [],
+ "source": [
+ "# /// script\n",
+ "# requires-python = \">=3.12\"\n",
+ "# dependencies = [\n",
+ "# \"anywidget\",\n",
+ "# \"polars\",\n",
+ "# ]\n",
+ "# ///"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "0624bd12",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {},
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
\ No newline at end of file
diff --git a/tests/_cli/snapshots/converted_arithmetic.py.txt b/tests/_cli/snapshots/converted_arithmetic.py.txt
new file mode 100644
index 0000000000..2b71d4dcb8
--- /dev/null
+++ b/tests/_cli/snapshots/converted_arithmetic.py.txt
@@ -0,0 +1,20 @@
+import marimo
+
+app = marimo.App()
+
+
+def __():
+ x = 0
+ x
+ return (x,)
+
+
+def __(x):
+ x + 1
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/snapshots/converted_blank.py.txt b/tests/_cli/snapshots/converted_blank.py.txt
new file mode 100644
index 0000000000..5f24931fb6
--- /dev/null
+++ b/tests/_cli/snapshots/converted_blank.py.txt
@@ -0,0 +1,12 @@
+import marimo
+
+app = marimo.App()
+
+
+def __():
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/snapshots/converted_juv.py.txt b/tests/_cli/snapshots/converted_juv.py.txt
new file mode 100644
index 0000000000..d033bf3f75
--- /dev/null
+++ b/tests/_cli/snapshots/converted_juv.py.txt
@@ -0,0 +1,19 @@
+# /// script
+# dependencies = [
+# "anywidget",
+# "polars",
+# ]
+# ///
+
+import marimo
+
+app = marimo.App()
+
+
+def __():
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/snapshots/converted_markdown.py.txt b/tests/_cli/snapshots/converted_markdown.py.txt
new file mode 100644
index 0000000000..23550e13d2
--- /dev/null
+++ b/tests/_cli/snapshots/converted_markdown.py.txt
@@ -0,0 +1,40 @@
+import marimo
+
+app = marimo.App()
+
+
+def __(mo):
+ mo.md(
+ r"""
+ # Hello, markdown
+
+ \"\"\"
+ 'hello"
+ '''
+ \"\"\"
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md(
+ r"""
+ Here is some math
+
+ $x \approx 0$
+ """
+ )
+ return
+
+
+def __():
+ import marimo as mo
+ return (mo,)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/snapshots/converted_multiple_defs.py.txt b/tests/_cli/snapshots/converted_multiple_defs.py.txt
new file mode 100644
index 0000000000..2118698478
--- /dev/null
+++ b/tests/_cli/snapshots/converted_multiple_defs.py.txt
@@ -0,0 +1,53 @@
+import marimo
+
+app = marimo.App()
+
+
+def __():
+ _x = 0
+ _x
+ return
+
+
+def __():
+ _x = 1
+ _x
+ return
+
+
+def __():
+ y = 0
+ return (y,)
+
+
+def __():
+ y_1 = 1
+ return (y_1,)
+
+
+def __(y_1):
+ y_1
+ return
+
+
+def __():
+ for _i in range(3):
+ print(_i)
+ return
+
+
+def __():
+ for _i in range(4):
+ print(_i)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/snapshots/converted_unparsable.py.txt b/tests/_cli/snapshots/converted_unparsable.py.txt
new file mode 100644
index 0000000000..2f4a8188d4
--- /dev/null
+++ b/tests/_cli/snapshots/converted_unparsable.py.txt
@@ -0,0 +1,23 @@
+import marimo
+
+app = marimo.App()
+
+
+app._unparsable_cell(
+ r"""
+ !echo hello, world
+
+ x = 0
+ """,
+ name="__"
+)
+
+
+def __(x):
+ x
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_cli/test_ipynb_to_marimo.py b/tests/_cli/test_ipynb_to_marimo.py
index 5f5bb21927..41c2603f27 100644
--- a/tests/_cli/test_ipynb_to_marimo.py
+++ b/tests/_cli/test_ipynb_to_marimo.py
@@ -1,139 +1,26 @@
# Copyright 2024 Marimo. All rights reserved.
from __future__ import annotations
-import os
-import sys
-import tempfile
-import textwrap
-from typing import TYPE_CHECKING
+import re
+from pathlib import Path
import pytest
-from marimo._ast import codegen
-from marimo._cli.convert.ipynb import convert_from_ipynb_file
+from marimo._convert.ipynb import convert_from_ipynb
+from tests.mocks import snapshotter
-if TYPE_CHECKING:
- from collections.abc import Sequence
+snapshot = snapshotter(__file__)
-DIR_PATH = os.path.dirname(os.path.realpath(__file__))
+DIR_PATH = Path(__file__).parent / "ipynb_data"
-def get_codes(ipynb_name: str) -> tuple[Sequence[str], Sequence[str]]:
- contents = convert_from_ipynb_file(
- DIR_PATH + f"/ipynb_data/{ipynb_name}.ipynb.txt"
- )
-
- tempfile_name = ""
- try:
- # in windows, can't re-open an open named temporary file, hence
- # delete=False and manual clean up
- with tempfile.NamedTemporaryFile(
- mode="w", suffix=".py", delete=False
- ) as f:
- tempfile_name = f.name
- f.write(contents)
- f.seek(0)
- app = codegen.get_app(f.name)
- assert app is not None
- cell_manager = app._cell_manager
- return list(cell_manager.codes()), list(cell_manager.names())
- finally:
- os.remove(tempfile_name)
-
-
-def test_markdown() -> None:
- codes, names = get_codes("markdown")
-
- assert len(codes) == 3
- assert (
- codes[0]
- == textwrap.dedent(
- """
- mo.md(
- r\"\"\"
- # Hello, markdown
-
- \\"\\"\\"
- 'hello"
- '''
- \\"\\"\\"
- \"\"\"
- )
- """
- ).strip()
- )
- assert (
- codes[1]
- == textwrap.dedent(
- """
- mo.md(
- r\"\"\"
- Here is some math
-
- $x \\approx 0$
- \"\"\"
- )
- """
- ).strip()
- )
- assert codes[2] == "import marimo as mo"
- assert [name == "_" for name in names]
-
-
-def test_arithmetic() -> None:
- codes, names = get_codes("arithmetic")
-
- assert len(codes) == 2
- assert codes[0] == "x = 0\nx"
- assert codes[1] == "x + 1"
- assert names == ["__", "__"]
-
-
-def test_blank() -> None:
- codes, names = get_codes("blank")
-
- assert len(codes) == 1
- assert codes[0] == ""
- assert names == ["__"]
-
-
-def test_unparsable() -> None:
- codes, names = get_codes("unparsable")
-
- assert len(codes) == 2
- assert codes[0] == "!echo hello, world\n\nx = 0"
- assert codes[1] == "x"
- assert names == ["__", "__"]
-
-
- sys.version_info < (3, 9), reason="Feature not supported in python 3.8"
-)
-def test_multiple_defs() -> None:
- codes, _ = get_codes("multiple_defs")
-
- assert len(codes) == 7
- assert codes[0] == "_x = 0\n_x"
- assert codes[1] == "_x = 1\n_x"
- assert codes[2] == "y = 0"
- assert codes[3] == "y_1 = 1"
- assert codes[4] == "y_1"
- assert (
- codes[5]
- == textwrap.dedent(
- """
- for _i in range(3):
- print(_i)
- """
- ).strip()
- )
- assert (
- codes[6]
- == textwrap.dedent(
- """
- for _i in range(4):
- print(_i)
-
- """
- ).strip()
[email protected]("ipynb_path", DIR_PATH.glob("*.ipynb.txt"))
+def test_ipynb_to_marimo_snapshots(ipynb_path: Path) -> None:
+ contents = ipynb_path.read_text()
+ converted = convert_from_ipynb(contents)
+ converted = re.sub(r"__generated_with = .*\n", "", converted)
+ converted = re.sub(r"# requires-python = .*\n", "", converted)
+ snapshot(
+ f"converted_{ipynb_path.name.replace('.ipynb.txt', '.py.txt')}",
+ converted,
)