import importlib

import pytest
from fastapi.testclient import TestClient
from sqlalchemy import inspect
from sqlalchemy.engine.reflection import Inspector
from sqlmodel import Session, create_engine

from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod
from docs_src.tutorial.fastapi.app_testing.tutorial001.test_main import (
    client_fixture,
    session_fixture,
)

assert session_fixture, "This keeps the session fixture used below"
assert client_fixture, "This keeps the client fixture used below"


@pytest.fixture(name="prepare", autouse=True)
def prepare_fixture(clear_sqlmodel):
    # Trigger side effects of registering table models in SQLModel
    # This has to be called after clear_sqlmodel, but before the session_fixture
    # That's why the extra custom fixture here
    importlib.reload(app_mod)
    importlib.reload(test_mod)


def test_create_hero(session: Session, client: TestClient):
    test_mod.test_create_hero(client)


def test_create_hero_incomplete(session: Session, client: TestClient):
    test_mod.test_create_hero_incomplete(client)


def test_create_hero_invalid(session: Session, client: TestClient):
    test_mod.test_create_hero_invalid(client)


def test_read_heroes(session: Session, client: TestClient):
    test_mod.test_read_heroes(session=session, client=client)


def test_read_hero(session: Session, client: TestClient):
    test_mod.test_read_hero(session=session, client=client)


def test_update_hero(session: Session, client: TestClient):
    test_mod.test_update_hero(session=session, client=client)


def test_delete_hero(session: Session, client: TestClient):
    test_mod.test_delete_hero(session=session, client=client)


def test_startup():
    app_mod.engine = create_engine("sqlite://")
    app_mod.on_startup()
    insp: Inspector = inspect(app_mod.engine)
    assert insp.has_table(str(app_mod.Hero.__tablename__))


def test_get_session():
    app_mod.engine = create_engine("sqlite://")
    for session in app_mod.get_session():
        assert isinstance(session, Session)
        assert session.bind == app_mod.engine


def test_read_hero_not_found(client: TestClient):
    response = client.get("/heroes/9000")
    assert response.status_code == 404


def test_update_hero_not_found(client: TestClient):
    response = client.patch("/heroes/9000", json={"name": "Very-Rusty-Man"})
    assert response.status_code == 404


def test_delete_hero_not_found(client: TestClient):
    response = client.delete("/heroes/9000")
    assert response.status_code == 404

sqlalchemy.inspect docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_delete_hero docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_create_hero sqlmodel.create_engine docs_src.tutorial.fastapi.app_testing.tutorial001.main.on_startup pytest.fixture docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_read_heroes importlib.reload docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_create_hero_incomplete docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_read_hero docs_src.tutorial.fastapi.app_testing.tutorial001.main.get_session docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_create_hero_invalid docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_update_hero
[((618, 662), 'pytest.fixture', 'pytest.fixture', ([], {'name': '"""prepare"""', 'autouse': '(True)'}), "(name='prepare', autouse=True)\n", (632, 662), False, 'import pytest\n'), ((899, 924), 'importlib.reload', 'importlib.reload', (['app_mod'], {}), '(app_mod)\n', (915, 924), False, 'import importlib\n'), ((929, 955), 'importlib.reload', 'importlib.reload', (['test_mod'], {}), '(test_mod)\n', (945, 955), False, 'import importlib\n'), ((1022, 1055), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_create_hero', 'test_mod.test_create_hero', (['client'], {}), '(client)\n', (1047, 1055), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1133, 1177), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_create_hero_incomplete', 'test_mod.test_create_hero_incomplete', (['client'], {}), '(client)\n', (1169, 1177), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1252, 1293), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_create_hero_invalid', 'test_mod.test_create_hero_invalid', (['client'], {}), '(client)\n', (1285, 1293), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1360, 1417), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_read_heroes', 'test_mod.test_read_heroes', ([], {'session': 'session', 'client': 'client'}), '(session=session, client=client)\n', (1385, 1417), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1482, 1537), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_read_hero', 'test_mod.test_read_hero', ([], {'session': 'session', 'client': 'client'}), '(session=session, client=client)\n', (1505, 1537), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1604, 1661), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_update_hero', 'test_mod.test_update_hero', ([], {'session': 'session', 'client': 'client'}), '(session=session, client=client)\n', (1629, 1661), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1728, 1785), 'docs_src.tutorial.fastapi.app_testing.tutorial001.test_main.test_delete_hero', 'test_mod.test_delete_hero', ([], {'session': 'session', 'client': 'client'}), '(session=session, client=client)\n', (1753, 1785), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import test_main as test_mod\n'), ((1829, 1855), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite://"""'], {}), "('sqlite://')\n", (1842, 1855), False, 'from sqlmodel import Session, create_engine\n'), ((1860, 1880), 'docs_src.tutorial.fastapi.app_testing.tutorial001.main.on_startup', 'app_mod.on_startup', ([], {}), '()\n', (1878, 1880), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod\n'), ((1903, 1926), 'sqlalchemy.inspect', 'inspect', (['app_mod.engine'], {}), '(app_mod.engine)\n', (1910, 1926), False, 'from sqlalchemy import inspect\n'), ((2033, 2059), 'sqlmodel.create_engine', 'create_engine', (['"""sqlite://"""'], {}), "('sqlite://')\n", (2046, 2059), False, 'from sqlmodel import Session, create_engine\n'), ((2079, 2100), 'docs_src.tutorial.fastapi.app_testing.tutorial001.main.get_session', 'app_mod.get_session', ([], {}), '()\n', (2098, 2100), True, 'from docs_src.tutorial.fastapi.app_testing.tutorial001 import main as app_mod\n')]