Code Monkey home page Code Monkey logo

Comments (5)

euri10 avatar euri10 commented on April 28, 2024

seems like I'm making a mistake on my

@fastapi.post("/notes")
async def add_note_fastapi(note: NoteIn, raise_exc: bool = False):

as the equivalent starlette style includes the @transaction decorator which I forgot to include:

@starlette.route("/notes_starlette", methods=["POST"])
@transaction
async def add_note_starlette(request):
    """
    Create a note: Starlette style
    """
    data = await request.json()
    query = notes.insert().values(text=data["text"], completed=data["completed"])
    await request.database.execute(query)
    if "raise_exc" in request.query_params:
        raise RuntimeError()
    return JSONResponse({"text": data["text"], "completed": data["completed"]})

Now it leads to another issue but it's a step closer, I'll have to rewrite a decorator that suits FastAPI, or use the Depends system if I got it right ?

from fastapi.

euri10 avatar euri10 commented on April 28, 2024

I replaced the @transaction that decorates the async def add_note_starlette(request): with a
async with fastapi.error_middleware.app.backend.session().transaction(): within the fastapi route that creates a note solves the rollback issue for this test only.

I added TestStarletteStyleWithFastAPI that uses the starlette route style (@app.route(path, methods=['POST'])) decorators with a FastAPI instance and all tests pass, it's a lots of duplicate code but it's a beginning trying to use that DBMiddleWare

So my struggle here is really how you decorate a fastapi route with this @transaction from starlette : using an aysnc with inside the route makes the rollback effective only during one test but subsequent tests fail.

from fastapi.

euri10 avatar euri10 commented on April 28, 2024

closing per #20 (comment)

from fastapi.

tiangolo avatar tiangolo commented on April 28, 2024

Thanks for coming back and closing it 👍

from fastapi.

github-actions avatar github-actions commented on April 28, 2024

Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.

from fastapi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.