Skip to content

Commit

Permalink
fix: Use FLOAT_DECIMAL to return Decimal as float instead of as str. …
Browse files Browse the repository at this point in the history
…Ignore warnings when coercing float or int to Decimal.
  • Loading branch information
jpmckinney committed Dec 13, 2023
1 parent 38a609a commit 56ea76a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
9 changes: 6 additions & 3 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
from enum import StrEnum
from typing import Any, Optional, Self

from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, PlainSerializer
from sqlalchemy import DECIMAL, Column, DateTime, and_, desc, or_, select
from sqlalchemy.dialects.postgresql import JSON
from sqlalchemy.orm import Query, Session
from sqlalchemy.sql import Select, func
from sqlalchemy.sql.expression import true
from sqlmodel import Field, Relationship, SQLModel, col
from typing_extensions import Annotated

from app.settings import app_settings

FLOAT_DECIMAL = Annotated[Decimal, PlainSerializer(lambda x: float(x), return_type=float, when_used="json")]


def _get_missing_data_keys(input_dict: dict[str, Any]) -> dict[str, bool]:
"""
Expand Down Expand Up @@ -239,8 +242,8 @@ class StatisticCustomRange(StrEnum):

class CreditProductBase(SQLModel):
borrower_size: BorrowerSize = Field(nullable=False)
lower_limit: Decimal = Field(sa_column=Column(DECIMAL(precision=16, scale=2), nullable=False))
upper_limit: Decimal = Field(sa_column=Column(DECIMAL(precision=16, scale=2), nullable=False))
lower_limit: FLOAT_DECIMAL = Field(sa_column=Column(DECIMAL(precision=16, scale=2), nullable=False))
upper_limit: FLOAT_DECIMAL = Field(sa_column=Column(DECIMAL(precision=16, scale=2), nullable=False))
interest_rate: str = Field(default="", nullable=False)
additional_information: str = Field(default="", nullable=False)
type: CreditType = Field(nullable=False)
Expand Down
31 changes: 23 additions & 8 deletions tests/test_lenders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

from fastapi import status

from app.models import BorrowerSize, CreditType, UserType
Expand Down Expand Up @@ -109,17 +111,26 @@ def test_create_credit_product(client):
response = client.post("/lenders/1/credit-products", json=credit_product, headers=FI_headers)
assert response.status_code == status.HTTP_401_UNAUTHORIZED

response = client.post("/lenders/1/credit-products", json=credit_product, headers=OCP_headers)
assert response.status_code == status.HTTP_200_OK
with warnings.catch_warnings():
# "Pydantic serializer warnings" "Expected `decimal` but got `float` - serialized value may not be as expected"
warnings.filterwarnings("ignore")

response = client.post("/lenders/1/credit-products", json=credit_product, headers=OCP_headers)
assert response.status_code == status.HTTP_200_OK

# OCP user tries to create a credit product for a non existent lender
response = client.post("/lenders/100/credit-products", json=credit_product, headers=OCP_headers)
assert response.status_code == status.HTTP_404_NOT_FOUND

response = client.put("/credit-products/1", json=updated_credit_product, headers=OCP_headers)
assert response.json()["lower_limit"] == updated_credit_product["lower_limit"]
assert response.json()["upper_limit"] == updated_credit_product["upper_limit"]
assert response.status_code == status.HTTP_200_OK
with warnings.catch_warnings():
# "Pydantic serializer warnings" "Expected `decimal` but got `int` - serialized value may not be as expected"
warnings.filterwarnings("ignore")

response = client.put("/credit-products/1", json=updated_credit_product, headers=OCP_headers)
assert response.json()["lower_limit"] == updated_credit_product["lower_limit"]
assert response.json()["upper_limit"] == updated_credit_product["upper_limit"]
assert response.json()["other_fees_description"] == updated_credit_product["other_fees_description"]
assert response.status_code == status.HTTP_200_OK

# tries to update a credit product that does not exist
response = client.put("/credit-products/100", json=updated_credit_product, headers=OCP_headers)
Expand Down Expand Up @@ -148,8 +159,12 @@ def test_create_lender_with_credit_product(client):
OCP_headers = client.post("/create-test-user-headers", json=OCP_user).json()
FI_headers = client.post("/create-test-user-headers", json=FI_user).json()

response = client.post("/lenders/", json=lender_with_credit_product, headers=OCP_headers)
assert response.status_code == status.HTTP_200_OK
with warnings.catch_warnings():
# "Pydantic serializer warnings" "Expected `decimal` but got `float` - serialized value may not be as expected"
warnings.filterwarnings("ignore")

response = client.post("/lenders/", json=lender_with_credit_product, headers=OCP_headers)
assert response.status_code == status.HTTP_200_OK

# fi user tries to create lender
response = client.post("/lenders/", json=lender_with_credit_product, headers=FI_headers)
Expand Down

0 comments on commit 56ea76a

Please sign in to comment.