From 646f8e46c3ec0506ea92950897aa10a9bc709433 Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:33:52 -0400 Subject: [PATCH 1/2] build: Upgrade pydantic --- requirements.txt | 4 ++-- requirements_dev.txt | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/requirements.txt b/requirements.txt index 0673482c..fe02553d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -100,14 +100,14 @@ psycopg2==2.9.6 # via sqlalchemy pycparser==2.21 # via cffi -pydantic==2.8.2 +pydantic==2.9.2 # via # -r requirements.in # fastapi # pydantic-extra-types # pydantic-settings # sqlmodel -pydantic-core==2.20.1 +pydantic-core==2.23.4 # via pydantic pydantic-extra-types==2.9.0 # via fastapi diff --git a/requirements_dev.txt b/requirements_dev.txt index 83ae3696..651d3d78 100644 --- a/requirements_dev.txt +++ b/requirements_dev.txt @@ -127,7 +127,7 @@ mdurl==0.1.2 # markdown-it-py minify-html==0.15.0 # via -r requirements.txt -moto[cognitoidp]==5.0.14 +moto==5.0.14 # via -r requirements_dev.in mypy==1.11.1 # via -r requirements_dev.in @@ -159,14 +159,14 @@ pycparser==2.21 # via # -r requirements.txt # cffi -pydantic==2.8.2 +pydantic==2.9.2 # via # -r requirements.txt # fastapi # pydantic-extra-types # pydantic-settings # sqlmodel -pydantic-core==2.20.1 +pydantic-core==2.23.4 # via # -r requirements.txt # pydantic @@ -296,7 +296,7 @@ urllib3==1.26.19 # requests # responses # sentry-sdk -uvicorn[standard]==0.22.0 +uvicorn==0.22.0 # via # -r requirements.txt # fastapi-cli From 849fc9c4253479cd452f42b124e83791b4e0c56f Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Fri, 1 Nov 2024 14:39:52 -0400 Subject: [PATCH 2/2] chore: Revert "chore: Use cls.model_validate(payload) instead of cls(**payload.model_dump()), to avoid 'Expected enum but got str - serialized value may not be as expected'" #443 --- app/models.py | 17 ----------------- app/routers/users.py | 6 ++---- tests/routers/test_users.py | 9 +++++++-- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/app/models.py b/app/models.py index adf21c55..0fbd714b 100644 --- a/app/models.py +++ b/app/models.py @@ -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. diff --git a/app/routers/users.py b/app/routers/users.py index 56204fbb..bd4ded23 100644 --- a/app/routers/users.py +++ b/app/routers/users.py @@ -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), @@ -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"] diff --git a/tests/routers/test_users.py b/tests/routers/test_users.py index 09ba66b4..9d2b4e1e 100644 --- a/tests/routers/test_users.py +++ b/tests/routers/test_users.py @@ -1,6 +1,7 @@ import datetime import uuid +import pytest from fastapi import status from app.i18n import _ @@ -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)