-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): initial support for middleware
- Loading branch information
1 parent
47c9844
commit e87e2b4
Showing
12 changed files
with
980 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import Iterator | ||
|
||
import pytest | ||
|
||
from prisma import Prisma | ||
from prisma.middleware import ( | ||
MiddlewareParams, | ||
NextMiddleware, | ||
MiddlewareResult, | ||
) | ||
|
||
# TODO: more tests | ||
# TODO: test every action | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def cleanup_middlewares(client: Prisma) -> Iterator[None]: | ||
middlewares = client._middlewares.copy() | ||
|
||
client._middlewares.clear() | ||
|
||
try: | ||
yield | ||
finally: | ||
client._middlewares = middlewares | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_basic(client: Prisma) -> None: | ||
__ran__ = False | ||
|
||
async def middleware( | ||
params: MiddlewareParams, get_result: NextMiddleware | ||
) -> MiddlewareResult: | ||
nonlocal __ran__ | ||
|
||
__ran__ = True | ||
return await get_result(params) | ||
|
||
client.use(middleware) | ||
await client.user.create({'name': 'Robert'}) | ||
assert __ran__ is True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from ._middleware import ( | ||
MiddlewareParams as MiddlewareParams, | ||
MiddlewareResult as MiddlewareResult, | ||
NextMiddleware as NextMiddleware, | ||
MiddlewareFunc as MiddlewareFunc, | ||
) |
Oops, something went wrong.