Skip to content

Commit

Permalink
chore: Revert "chore: Use cls.model_validate(payload) instead of cls(…
Browse files Browse the repository at this point in the history
…**payload.model_dump()), to avoid 'Expected enum but got str - serialized value may not be as expected'" #443
  • Loading branch information
jpmckinney committed Nov 1, 2024
1 parent 646f8e4 commit 849fc9c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
17 changes: 0 additions & 17 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,6 @@ def create(cls, session: Session, **data: Any) -> Self:
session.flush()
return obj

@classmethod
def create_from_object(cls, session: Session, obj: Self) -> Self:
"""
Insert a new instance into the database.
If the model has a ``missing_data`` field, indicate which fields are missing in the instance.
:param session: The database session.
:param data: The initial instance data.
:return: The inserted instance.
"""
# https://github.com/fastapi/sqlmodel/issues/348
obj = cls.model_validate(obj) # type: ignore[attr-defined]
session.add(obj)
session.flush()
return obj

def update(self, session: Session, **data: Any) -> Self:
"""
Update an existing instance in the database.
Expand Down
6 changes: 2 additions & 4 deletions app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
tags=[util.Tags.users],
)
async def create_user(
payload: models.User,
payload: models.UserBase,
session: Session = Depends(get_db),
client: aws.Client = Depends(dependencies.get_aws_client),
admin: models.User = Depends(dependencies.get_admin_user),
Expand All @@ -42,9 +42,7 @@ async def create_user(
UserAttributes=[{"Name": "email", "Value": payload.email}],
)

user = models.User.create_from_object(session, payload)
if payload.lender_id:
user.lender = models.Lender.get(session, payload.lender_id)
user = models.User.create(session, **payload.model_dump())

user.external_id = response["User"]["Username"]

Expand Down
9 changes: 7 additions & 2 deletions tests/routers/test_users.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import uuid

import pytest
from fastapi import status

from app.i18n import _
Expand All @@ -13,10 +14,14 @@ def test_get_me(client, admin_header):
assert response.json()["user"]["name"] == "OCP Test User"


def test_create_and_get_user(client, admin_header, lender_header, user_payload):
response = client.post("/users", json=user_payload, headers=admin_header)
@pytest.mark.parametrize("with_lender", [True, False])
def test_create_and_get_user(client, admin_header, lender_header, user_payload, lender, with_lender):
lender_id = lender.id if with_lender else None

response = client.post("/users", json=user_payload | {"lender_id": lender_id}, headers=admin_header)
assert_ok(response)
assert response.json()["name"] == user_payload["name"]
assert response.json()["lender_id"] == lender_id

# fetch second user since the first one is the OCP user created for headers
response = client.get("/users/2", headers=admin_header)
Expand Down

0 comments on commit 849fc9c

Please sign in to comment.