Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #443 by reverting a commit in #373 #444

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
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