from unittest.mock import patch

import pytest
from sqlalchemy.exc import NoResultFound
from sqlmodel import Session, create_engine, delete

from ...conftest import get_testing_print_function


def test_tutorial(clear_sqlmodel):
    from docs_src.tutorial.one import tutorial005 as mod

    mod.sqlite_url = "sqlite://"
    mod.engine = create_engine(mod.sqlite_url)
    with pytest.raises(NoResultFound):
        mod.main()
    with Session(mod.engine) as session:
        # TODO: create delete() function
        # TODO: add overloads for .exec() with delete object
        session.exec(delete(mod.Hero))
        session.add(mod.Hero(name="<NAME>", secret_name="<NAME>", age=24))
        session.commit()

    calls = []

    new_print = get_testing_print_function(calls)

    with patch("builtins.print", new=new_print):
        mod.select_heroes()
    assert calls == [
        [
            "Hero:",
            {
                "id": 1,
                "name": "<NAME>",
                "secret_name": "<NAME>",
                "age": 24,
            },
        ]
    ]

sqlmodel.delete docs_src.tutorial.one.tutorial005.Hero pytest.raises sqlmodel.Session docs_src.tutorial.one.tutorial005.select_heroes unittest.mock.patch sqlmodel.create_engine docs_src.tutorial.one.tutorial005.main
[((337, 366), 'sqlmodel.create_engine', 'create_engine', (['mod.sqlite_url'], {}), '(mod.sqlite_url)\n', (350, 366), False, 'from sqlmodel import Session, create_engine, delete\n'), ((376, 404), 'pytest.raises', 'pytest.raises', (['NoResultFound'], {}), '(NoResultFound)\n', (389, 404), False, 'import pytest\n'), ((414, 424), 'docs_src.tutorial.one.tutorial005.main', 'mod.main', ([], {}), '()\n', (422, 424), True, 'from docs_src.tutorial.one import tutorial005 as mod\n'), ((434, 453), 'sqlmodel.Session', 'Session', (['mod.engine'], {}), '(mod.engine)\n', (441, 453), False, 'from sqlmodel import Session, create_engine, delete\n'), ((784, 822), 'unittest.mock.patch', 'patch', (['"""builtins.print"""'], {'new': 'new_print'}), "('builtins.print', new=new_print)\n", (789, 822), False, 'from unittest.mock import patch\n'), ((832, 851), 'docs_src.tutorial.one.tutorial005.select_heroes', 'mod.select_heroes', ([], {}), '()\n', (849, 851), True, 'from docs_src.tutorial.one import tutorial005 as mod\n'), ((589, 605), 'sqlmodel.delete', 'delete', (['mod.Hero'], {}), '(mod.Hero)\n', (595, 605), False, 'from sqlmodel import Session, create_engine, delete\n'), ((627, 680), 'docs_src.tutorial.one.tutorial005.Hero', 'mod.Hero', ([], {'name': '"""<NAME>"""', 'secret_name': '"""<NAME>"""', 'age': '(24)'}), "(name='<NAME>', secret_name='<NAME>', age=24)\n", (635, 680), True, 'from docs_src.tutorial.one import tutorial005 as mod\n')]