Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jun 27, 2024
1 parent 95e2096 commit b9b60b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
25 changes: 20 additions & 5 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ def test_get_prices_filters(db_session, user_session: SessionModel, clean_prices
user_session.user,
)
crud.create_price(
db_session, PRICE_1.model_copy(update={"price": 5.10}), user_session.user
db_session, PRICE_1.model_copy(update={"price": 4.1}), user_session.user
)
crud.create_price(
db_session,
Expand All @@ -653,8 +653,8 @@ def test_get_prices_filters(db_session, user_session: SessionModel, clean_prices
response = client.get("/api/v1/prices?category_tag=en:tomatoes")
assert response.status_code == 200
assert len(response.json()["items"]) == 1
# 1 price with price > 5
response = client.get("/api/v1/prices?price__gt=5")
# 1 price with price > 4
response = client.get("/api/v1/prices?price__gt=4")
assert response.status_code == 200
assert len(response.json()["items"]) == 1
# 1 price with currency USD
Expand Down Expand Up @@ -694,7 +694,7 @@ def test_update_price(
# create price
db_price = crud.create_price(db_session, PRICE_1, user_session.user)

new_price = 5.5
new_price = 4.5
PRICE_UPDATE_PARTIAL = {"price": new_price}
# without authentication
response = client.patch(f"/api/v1/prices/{db_price.id}")
Expand Down Expand Up @@ -748,7 +748,22 @@ def test_update_price(
headers={"Authorization": f"Bearer {user_session.token}"},
json=jsonable_encoder(PRICE_UPDATE_PARTIAL_WRONG),
)
print("coucou", response.__dict__)
assert response.status_code == 422
# with authentication and price owner but validation error
PRICE_UPDATE_PARTIAL_WRONG_LIST = [
{**PRICE_UPDATE_PARTIAL, "price": -1},
{"price_without_discount": True, "price_is_discounted": False},
{"price_without_discount": 3}
# {**PRICE_UPDATE_PARTIAL, "price_per": "UNIT"}
]
for PRICE_UPDATE_PARTIAL_WRONG in PRICE_UPDATE_PARTIAL_WRONG_LIST:
response = client.patch(
f"/api/v1/prices/{db_price.id}",
headers={"Authorization": f"Bearer {user_session.token}"},
json=jsonable_encoder(PRICE_UPDATE_PARTIAL_WRONG),
)
assert response.status_code == 422


def test_update_price_moderator(
Expand All @@ -757,7 +772,7 @@ def test_update_price_moderator(
# create price
db_price = crud.create_price(db_session, PRICE_1, user_session.user)

new_price = 5.5
new_price = 4.5
PRICE_UPDATE_PARTIAL = {"price": new_price}

# user_1 is moderator, not owner
Expand Down
18 changes: 9 additions & 9 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, PriceCreateWithValidation
from app.schemas import CurrencyEnum, LocationOSMEnum, PriceCreate


class TestPriceCreate:
def test_simple_price_with_barcode(self):
price = PriceCreateWithValidation(
price = PriceCreate(
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 = PriceCreateWithValidation(
price = PriceCreate(
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"):
PriceCreateWithValidation(
PriceCreate(
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"):
PriceCreateWithValidation(
PriceCreate(
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"):
PriceCreateWithValidation(
PriceCreate(
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",
):
PriceCreateWithValidation(
PriceCreate(
product_code="5414661000456",
labels_tags=["en:Organic", "fr:AB-agriculture-biologique"],
location_osm_id=123,
Expand All @@ -91,7 +91,7 @@ def test_price_discount_raise(self):
pydantic.ValidationError,
match="`price_is_discounted` must be true if `price_without_discount` is filled",
):
PriceCreateWithValidation(
PriceCreate(
product_code="5414661000456",
location_osm_id=123,
location_osm_type=LocationOSMEnum.NODE,
Expand All @@ -105,7 +105,7 @@ def test_price_discount_raise(self):
pydantic.ValidationError,
match="`price_without_discount` must be greater than `price`",
):
PriceCreateWithValidation(
PriceCreate(
product_code="5414661000456",
location_osm_id=123,
location_osm_type=LocationOSMEnum.NODE,
Expand Down

0 comments on commit b9b60b0

Please sign in to comment.