from unittest.mock import patch

from sqlmodel import create_engine

from ....conftest import get_testing_print_function

expected_calls = [
    [
        "Created hero:",
        {
            "age": None,
            "id": 1,
            "secret_name": "<NAME>",
            "team_id": 1,
            "name": "Deadpond",
        },
    ],
    [
        "Created hero:",
        {
            "age": 48,
            "id": 2,
            "secret_name": "<NAME>",
            "team_id": 2,
            "name": "Rusty-Man",
        },
    ],
    [
        "Created hero:",
        {
            "age": None,
            "id": 3,
            "secret_name": "<NAME>",
            "team_id": None,
            "name": "Spider-Boy",
        },
    ],
    [
        "Updated hero:",
        {
            "age": None,
            "id": 3,
            "secret_name": "<NAME>",
            "team_id": 2,
            "name": "Spider-Boy",
        },
    ],
    [
        "Team Wakaland:",
        {"id": 3, "headquarters": "Wakaland Capital City", "name": "Wakaland"},
    ],
    [
        "Preventers new hero:",
        {
            "age": 32,
            "id": 6,
            "secret_name": "<NAME>",
            "team_id": 2,
            "name": "Tarantula",
        },
    ],
    [
        "Preventers new hero:",
        {
            "age": 36,
            "id": 7,
            "secret_name": "<NAME>",
            "team_id": 2,
            "name": "Dr. Weird",
        },
    ],
    [
        "Preventers new hero:",
        {
            "age": 93,
            "id": 8,
            "secret_name": "<NAME>",
            "team_id": 2,
            "name": "Captain North America",
        },
    ],
]


def test_tutorial(clear_sqlmodel):
    from docs_src.tutorial.relationship_attributes.create_and_update_relationships import (
        tutorial001 as mod,
    )

    mod.sqlite_url = "sqlite://"
    mod.engine = create_engine(mod.sqlite_url)
    calls = []

    new_print = get_testing_print_function(calls)

    with patch("builtins.print", new=new_print):
        mod.main()
    assert calls == expected_calls

unittest.mock.patch sqlmodel.create_engine docs_src.tutorial.relationship_attributes.create_and_update_relationships.tutorial001.main
[((1912, 1941), 'sqlmodel.create_engine', 'create_engine', (['mod.sqlite_url'], {}), '(mod.sqlite_url)\n', (1925, 1941), False, 'from sqlmodel import create_engine\n'), ((2018, 2056), 'unittest.mock.patch', 'patch', (['"""builtins.print"""'], {'new': 'new_print'}), "('builtins.print', new=new_print)\n", (2023, 2056), False, 'from unittest.mock import patch\n'), ((2066, 2076), 'docs_src.tutorial.relationship_attributes.create_and_update_relationships.tutorial001.main', 'mod.main', ([], {}), '()\n', (2074, 2076), True, 'from docs_src.tutorial.relationship_attributes.create_and_update_relationships import tutorial001 as mod\n')]