Skip to content

Commit

Permalink
fix: improve price fetch performance (#118)
Browse files Browse the repository at this point in the history
we were performing heavy model validation on trusted input.
  • Loading branch information
raphael0202 authored Jan 3, 2024
1 parent a01a5a0 commit 1b65ea2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def get_price(
tags=["Prices"],
)
def create_price(
price: schemas.PriceCreate,
price: schemas.PriceCreateWithValidation,
background_tasks: BackgroundTasks,
current_user: schemas.UserBase = Depends(get_current_user),
db: Session = Depends(get_db),
Expand Down
9 changes: 9 additions & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@ class PriceCreate(BaseModel):
examples=[15],
)


class PriceCreateWithValidation(PriceCreate):
"""A version of `PriceCreate` with taxonomy validations.
These validations are not done in the `PriceCreate` model because they
they are time-consuming and only necessary when creating a price from
the API.
"""

@field_validator("labels_tags")
def labels_tags_is_valid(cls, v: list[str] | None):
if v is not None:
Expand Down
14 changes: 7 additions & 7 deletions tests/unit/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import pydantic
import pytest

from app.schemas import CurrencyEnum, LocationOSMEnum, PriceCreate
from app.schemas import CurrencyEnum, LocationOSMEnum, PriceCreateWithValidation


class TestPriceCreate:
def test_simple_price_with_barcode(self):
price = PriceCreate(
price = PriceCreateWithValidation(
product_code="5414661000456",
location_osm_id=123,
location_osm_type=LocationOSMEnum.NODE,
Expand All @@ -24,7 +24,7 @@ def test_simple_price_with_barcode(self):
assert price.date == datetime.date.fromisoformat("2021-01-01")

def test_simple_price_with_category(self):
price = PriceCreate(
price = PriceCreateWithValidation(
category_tag="en:Fresh-apricots",
labels_tags=["en:Organic", "fr:AB-agriculture-biologique"],
origins_tags=["en:California", "en:Sweden"],
Expand All @@ -40,7 +40,7 @@ def test_simple_price_with_category(self):

def test_simple_price_with_invalid_taxonomized_values(self):
with pytest.raises(pydantic.ValidationError, match="Invalid category tag"):
PriceCreate(
PriceCreateWithValidation(
category_tag="en:unknown-category",
location_osm_id=123,
location_osm_type=LocationOSMEnum.NODE,
Expand All @@ -50,7 +50,7 @@ def test_simple_price_with_invalid_taxonomized_values(self):
)

with pytest.raises(pydantic.ValidationError, match="Invalid label tag"):
PriceCreate(
PriceCreateWithValidation(
category_tag="en:carrots",
labels_tags=["en:invalid"],
location_osm_id=123,
Expand All @@ -61,7 +61,7 @@ def test_simple_price_with_invalid_taxonomized_values(self):
)

with pytest.raises(pydantic.ValidationError, match="Invalid origin tag"):
PriceCreate(
PriceCreateWithValidation(
category_tag="en:carrots",
origins_tags=["en:invalid"],
location_osm_id=123,
Expand All @@ -76,7 +76,7 @@ def test_simple_price_with_product_code_and_labels_tags_raise(self):
pydantic.ValidationError,
match="`labels_tags` can only be set for products without barcode",
):
PriceCreate(
PriceCreateWithValidation(
product_code="5414661000456",
labels_tags=["en:Organic", "fr:AB-agriculture-biologique"],
location_osm_id=123,
Expand Down

0 comments on commit 1b65ea2

Please sign in to comment.