Introduce product details table

This commit is contained in:
2026-04-22 12:25:35 +02:00
parent 6e6636e941
commit e8266ffdc1
11 changed files with 416 additions and 168 deletions

View File

@@ -12,6 +12,7 @@ from allmende_payment_system.models import (
Area,
Permission,
Product,
ProductDetails,
User,
UserGroup,
)
@@ -219,9 +220,49 @@ def test_add_product(test_db, client, admin_user):
follow_redirects=False,
)
assert response.status_code == 303
product = test_db.execute(
select(Product).where(Product.name == "Test Product")
).scalar()
product = test_db.execute(select(Product)).scalar()
assert product is not None
assert product.name == "Test Product"
assert product.price == Decimal("9.99")
assert len(product.details_history) == 1
assert product.current_details.name == "Test Product"
assert product.current_details.price == Decimal("9.99")
def test_change_product(test_db, client, admin_user):
area = Area(name="Test Area", description="An area for testing")
test_db.add(area)
product = Product(
area=area,
)
product.details_history.append(
ProductDetails(
name="Test Product",
price=Decimal("9.99"),
unit_of_measure="piece",
vat_rate=Decimal("19.00"),
)
)
test_db.add(product)
test_db.flush()
response = client.post(
f"/admin/products/edit/{product.id}",
data={
"name": "Updated Product",
"vat_rate": "7.00",
"unit_of_measure": "kg",
"price": 19.99,
"area_id": area.id,
},
user=admin_user,
follow_redirects=False,
)
assert response.status_code == 303
updated_product = test_db.execute(
select(Product).where(Product.id == product.id)
).scalar()
assert len(updated_product.details_history) == 2
assert updated_product.current_details.name == "Updated Product"
assert updated_product.current_details.price == Decimal("19.99")
assert updated_product.current_details.price == Decimal("19.99")

View File

@@ -32,9 +32,9 @@ def create_user_with_account(test_db, username: str, balance: float | None = Non
return user
def add_finalized_order_to_user(test_db, user, product) -> Order:
def add_finalized_order_to_user(test_db, user, product: Product) -> Order:
order = Order(user=user)
total_amount = product.price
total_amount = product.current_details.price
order.items.append(
OrderItem(product=product, quantity=1, total_amount=total_amount)
)
@@ -49,7 +49,7 @@ def add_finalized_order_to_user(test_db, user, product) -> Order:
def product(test_db):
area = Area(**fake.area())
test_db.add(area)
product = Product(**fake.product())
product = Product.create(**fake.product())
product.area = area
test_db.add(product)
test_db.flush()
@@ -96,15 +96,17 @@ def test_edit_item_in_cart(client: APSTestClient, test_db, product):
cart = test_db.scalar(select(Order))
assert len(cart.items) == 1
assert cart.items[0].quantity == 3
assert cart.items[0].total_amount == product.price * 3
assert cart.items[0].total_amount == product.current_details.price * 3
def test_remove_item_from_cart(client: APSTestClient, test_db, product):
def test_remove_item_from_cart(client: APSTestClient, test_db, product: Product):
user = create_user_with_account(test_db, "test")
user.shopping_cart.items.append(
OrderItem(product=product, quantity=2, total_amount=product.price * 2)
OrderItem(
product=product, quantity=2, total_amount=product.current_details.price * 2
)
)
test_db.flush()
@@ -122,11 +124,15 @@ def test_remove_item_from_cart(client: APSTestClient, test_db, product):
assert len(test_db.scalars(select(OrderItem)).all()) == 0
def test_remove_item_from_cart_wrong_user(client: APSTestClient, test_db, product):
def test_remove_item_from_cart_wrong_user(
client: APSTestClient, test_db, product: Product
):
user = create_user_with_account(test_db, "test")
user.shopping_cart.items.append(
OrderItem(product=product, quantity=2, total_amount=product.price * 2)
OrderItem(
product=product, quantity=2, total_amount=product.current_details.price * 2
)
)
test_db.flush()
@@ -144,7 +150,7 @@ def test_remove_item_from_cart_wrong_user(client: APSTestClient, test_db, produc
assert len(cart.items) == 1
def test_finalize_order(client: APSTestClient, test_db, product):
def test_finalize_order(client: APSTestClient, test_db, product: Product):
user = create_user_with_account(test_db, "test", balance=100.0)
second_account = Account(name=f"Another account")
@@ -155,7 +161,9 @@ def test_finalize_order(client: APSTestClient, test_db, product):
user.accounts.append(second_account)
user.shopping_cart.items.append(
OrderItem(product=product, quantity=2, total_amount=product.price * 2)
OrderItem(
product=product, quantity=2, total_amount=product.current_details.price * 2
)
)
test_db.flush()
@@ -170,10 +178,12 @@ def test_finalize_order(client: APSTestClient, test_db, product):
assert len(user.orders) == 2 # shopping cart + finalized order
assert user.accounts[0].balance == Decimal(100.0)
assert user.accounts[1].balance == Decimal(50.0) - (product.price * 2)
assert user.accounts[1].balance == Decimal(50.0) - (
product.current_details.price * 2
)
def test_view_order(client: APSTestClient, test_db, product):
def test_view_order(client: APSTestClient, test_db, product: Product):
user = create_user_with_account(test_db, "test")
order = add_finalized_order_to_user(test_db, user, product)
@@ -181,10 +191,10 @@ def test_view_order(client: APSTestClient, test_db, product):
response = client.get(f"/shop/order/{order.id}")
assert response.status_code == 200
assert f"Einkauf #{order.id}" in response.text
assert product.name in response.text
assert product.current_details.name in response.text
def test_view_order_wrong_user(client: APSTestClient, test_db, product):
def test_view_order_wrong_user(client: APSTestClient, test_db, product: Product):
user = create_user_with_account(test_db, "test")
order = add_finalized_order_to_user(test_db, user, product)