-
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.
Fix JSON serialization error for UUID primary keys when excluded from…
… list (#553) * Add forced PK serialization 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). * Add string field size for MySQL tests * Update tests/sqla/utils.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update tests/sqla/utils.py * Update tests/sqla/utils.py --------- Co-authored-by: Aleksey Gureiev <agureiev@shakuro.com> Co-authored-by: Jocelin Hounon <hounonj@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
13e902f
commit 056d976
Showing
4 changed files
with
149 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,92 @@ | ||
import uuid | ||
|
||
import pytest | ||
import pytest_asyncio | ||
from httpx import AsyncClient | ||
from sqlalchemy import Boolean, Column, ForeignKey, String | ||
from sqlalchemy.engine import Engine | ||
from sqlalchemy.orm import Session, declarative_base, relationship | ||
from starlette.applications import Starlette | ||
from starlette_admin.contrib.sqla import Admin | ||
from starlette_admin.contrib.sqla.view import ModelView | ||
|
||
from tests.sqla.utils import Uuid, get_test_engine | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class User(Base): | ||
__tablename__ = "user" | ||
|
||
id = Column(Uuid, primary_key=True, default=uuid.uuid1, unique=True) | ||
name = Column(String(50)) | ||
membership = relationship("Membership", back_populates="user", uselist=False) | ||
|
||
|
||
class Membership(Base): | ||
__tablename__ = "membership" | ||
|
||
id = Column(Uuid, primary_key=True, default=uuid.uuid1, unique=True) | ||
is_active = Column(Boolean, default=True) | ||
|
||
user_id = Column(Uuid, ForeignKey("user.id"), unique=True) | ||
user = relationship("User", back_populates="membership") | ||
|
||
|
||
class UserView(ModelView): | ||
fields = ["name", "membership"] | ||
|
||
|
||
@pytest.fixture | ||
def engine() -> Engine: | ||
engine = get_test_engine() | ||
|
||
Base.metadata.create_all(engine) | ||
|
||
try: | ||
yield engine | ||
|
||
finally: | ||
Base.metadata.drop_all(engine) | ||
|
||
|
||
@pytest.fixture | ||
def app(engine: Engine): | ||
app = Starlette() | ||
|
||
admin = Admin(engine) | ||
admin.add_view(UserView(User)) | ||
admin.add_view(ModelView(Membership)) | ||
admin.mount_to(app) | ||
|
||
return app | ||
|
||
|
||
@pytest_asyncio.fixture | ||
async def client(app): | ||
async with AsyncClient(app=app, base_url="http://testserver") as c: | ||
yield c | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_ensuring_pk(client: AsyncClient, engine: Engine): | ||
""" | ||
Ensures PK is present in the serialized data and properly serialized as a string. | ||
""" | ||
user_id = uuid.uuid1() | ||
membership_id = uuid.uuid1() | ||
|
||
user = User(id=user_id, name="Jack") | ||
membership = Membership(id=membership_id, is_active=True, user=user) | ||
|
||
with Session(engine) as session: | ||
session.add(user) | ||
session.add(membership) | ||
session.commit() | ||
|
||
response = await client.get("/admin/api/user") | ||
data = response.json() | ||
|
||
assert [(str(user_id), str(membership_id))] == [ | ||
(x["id"], x["membership"]["id"]) for x in data["items"] | ||
] |
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