-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When PK is not listed among fields it needs proper serialization in case it's not of a JSON-serializable type already (for example, UUID).
- Loading branch information
Aleksey Gureiev
committed
Jul 1, 2024
1 parent
41a59cc
commit d58e84d
Showing
3 changed files
with
104 additions
and
17 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
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,72 @@ | ||
import uuid | ||
|
||
import pytest | ||
from sqlalchemy import Column, String | ||
from sqlalchemy.dialects.postgresql import UUID | ||
from sqlalchemy.orm import declarative_base | ||
from starlette.applications import Starlette | ||
from starlette.requests import Request | ||
from starlette_admin import RequestAction, StringField | ||
from starlette_admin.base import BaseAdmin | ||
from starlette_admin.contrib.sqla.view import ModelView | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "user" | ||
|
||
id: UUID = Column( | ||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True | ||
) | ||
name: str = Column(String()) | ||
|
||
|
||
class UserView(ModelView): | ||
fields = [ | ||
StringField("name"), | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def user_view(): | ||
return UserView(User) | ||
|
||
|
||
@pytest.fixture | ||
def app(user_view: UserView): | ||
app = Starlette() | ||
app.state.ROUTE_NAME = "admin" | ||
|
||
admin = BaseAdmin() | ||
admin.add_view(user_view) | ||
admin.mount_to(app) | ||
|
||
return app | ||
|
||
|
||
@pytest.fixture | ||
def req(app: Starlette): | ||
return Request( | ||
{ | ||
"app": app, | ||
"router": app.router, | ||
"type": "http", | ||
"headers": [], | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_ensuring_pk(req: Request, user_view: UserView): | ||
""" | ||
Ensures PK is present in the serialized data and properly serialized as a string. | ||
""" | ||
|
||
user_id = uuid.uuid1() | ||
user = User(id=user_id, name="Jack") | ||
|
||
req.state.action = RequestAction.LIST | ||
data = await user_view.serialize(user, req, RequestAction.LIST) | ||
|
||
assert data["id"] == str(user_id) |