| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 | import pytestfrom clean_python import ctxfrom clean_python import DoesNotExistfrom clean_python import Jsonfrom clean_python import Tenantfrom clean_python.api_client import ApiGatewayfrom clean_python.api_client import ApiProviderclass BooksGateway(ApiGateway, path="v1/books/{id}"):    passasync def fake_token():    return {"Authorization": "Bearer token"}@pytest.fixturedef provider(fastapi_example_app) -> ApiProvider:    ctx.tenant = Tenant(id=2, name="")    yield ApiProvider(fastapi_example_app + "/", fake_token)    ctx.tenant = None@pytest.fixturedef gateway(provider) -> ApiGateway:    return BooksGateway(provider)@pytest.fixtureasync def book(gateway: ApiGateway):    return await gateway.add({"title": "fixture", "author": {"name": "foo"}})async def test_add(gateway: ApiGateway):    response = await gateway.add({"title": "test_add", "author": {"name": "foo"}})    assert isinstance(response["id"], int)    assert response["title"] == "test_add"    assert response["author"] == {"name": "foo"}    assert response["created_at"] == response["updated_at"]async def test_get(gateway: ApiGateway, book: Json):    response = await gateway.get(book["id"])    assert response == bookasync def test_remove_and_404(gateway: ApiGateway, book: Json):    assert await gateway.remove(book["id"]) is True    assert await gateway.get(book["id"]) is None    assert await gateway.remove(book["id"]) is Falseasync def test_update(gateway: ApiGateway, book: Json):    response = await gateway.update({"id": book["id"], "title": "test_update"})    assert response["id"] == book["id"]    assert response["title"] == "test_update"    assert response["author"] == {"name": "foo"}    assert response["created_at"] != response["updated_at"]async def test_update_404(gateway: ApiGateway):    with pytest.raises(DoesNotExist):        await gateway.update({"id": 123456, "title": "test_update_404"})
 |