Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions tests/test_homepage_examples.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# License: BSD 3-Clause
from __future__ import annotations

import re
from pathlib import Path
from unittest.mock import patch

import numpy as np
import pandas as pd
import pytest

import openml
from openml.tasks.split import OpenMLSplit

HOMEPAGE = Path(__file__).resolve().parents[1] / "docs" / "index.md"
EXAMPLES = re.findall(r"^```python\n(.*?)^```$", HOMEPAGE.read_text(encoding="utf-8"), re.M | re.S)


pytestmark = pytest.mark.parametrize("example", EXAMPLES)


def test_homepage_example(example: str) -> None:
"""Run each standalone snippet with local data in place of server downloads."""
dataset = openml.datasets.OpenMLDataset("credit-g", description=None)
data = pd.DataFrame({"duration": [6, 12], "class": ["good", "bad"]})
task = openml.tasks.OpenMLClassificationTask(
task_type_id=openml.tasks.TaskType.SUPERVISED_CLASSIFICATION,
task_type="Supervised Classification",
data_set_id=31,
target_name="class",
)
task.split = OpenMLSplit("folds", "", {0: {0: {0: (np.array([0]), np.array([1]))}}})
suite = openml.study.create_benchmark_suite("example", "", [31])

with (
patch.object(dataset, "_load_data", return_value=(data, [False, True], list(data))),
patch.object(
openml.datasets,
"get_dataset",
autospec=True,
side_effect={"credit-g": dataset, 31: dataset}.__getitem__,
),
patch.object(openml.tasks, "get_task", autospec=True, side_effect={31: task}.__getitem__),
patch.object(
openml.study,
"get_suite",
autospec=True,
side_effect={"amlb-classification-all": suite}.__getitem__,
),
):
exec(compile(example, str(HOMEPAGE), "exec"), {}) # noqa: S102