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.

2.6 KB
Raw Blame
import json

import pytest
from pydantic import TypeAdapter, ValidationError

from marimo._ai._tools.tools.cells import (
    GetCellOutputArgs,
    GetCellOutputOutput,
    GetCellRuntimeDataArgs,
    GetCellRuntimeDataOutput,
    GetLightweightCellMapArgs,
    GetLightweightCellMapOutput,
)
from marimo._ai._tools.tools.datasource import (
    GetDatabaseTablesArgs,
    GetDatabaseTablesOutput,
)
from marimo._ai._tools.tools.dependency_graph import (
    GetCellDependencyGraphArgs,
    GetCellDependencyGraphOutput,
)
from marimo._ai._tools.tools.errors import (
    GetNotebookErrorsArgs,
    GetNotebookErrorsOutput,
)
from marimo._ai._tools.tools.lint import (
    LintNotebookArgs,
    LintNotebookOutput,
)
from marimo._ai._tools.tools.notebooks import (
    GetActiveNotebooksOutput,
)
from marimo._ai._tools.tools.tables_and_variables import (
    TablesAndVariablesArgs,
    TablesAndVariablesOutput,
)
from marimo._core.protocol import ExecutionId, Revision
from marimo._messaging.notification import VariableValue

TOOL_IO_CLASSES = [
    GetCellRuntimeDataArgs,
    GetCellRuntimeDataOutput,
    GetLightweightCellMapArgs,
    GetLightweightCellMapOutput,
    TablesAndVariablesArgs,
    TablesAndVariablesOutput,
    GetDatabaseTablesArgs,
    GetDatabaseTablesOutput,
    GetNotebookErrorsArgs,
    GetNotebookErrorsOutput,
    GetActiveNotebooksOutput,
    LintNotebookArgs,
    LintNotebookOutput,
    GetCellOutputArgs,
    GetCellOutputOutput,
    GetCellDependencyGraphArgs,
    GetCellDependencyGraphOutput,
]


@pytest.mark.parametrize("tool_io_class", TOOL_IO_CLASSES)
def test_tool_io_exposes_pydantic_schema(tool_io_class: type) -> None:
    TypeAdapter(tool_io_class).json_schema()


def test_variable_value_revision_round_trip() -> None:
    adapter = TypeAdapter(VariableValue)
    value = VariableValue(
        name="x",
        value="1",
        datatype="int",
        execution_id="execution-1",
        revision=Revision(execution=ExecutionId("execution-1"), update=2),
    )
    expected = {
        "name": "x",
        "value": "1",
        "datatype": "int",
        "execution_id": "execution-1",
        "revision": {"execution": "execution-1", "update": 2},
    }

    assert adapter.validate_python(value) is value
    assert adapter.validate_json(json.dumps(expected)) == value
    assert adapter.dump_python(value, mode="json") == expected
    assert json.loads(adapter.dump_json(value)) == expected

    invalid = {
        **expected,
        "revision": {"execution": "execution-1", "update": -1},
    }
    with pytest.raises(ValidationError, match="Expected `int` >= 0"):
        adapter.validate_json(json.dumps(invalid))