| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- from fastapi.testclient import TestClient
- from app.db import (
- fetch_order_product_list,
- pick_last_order_id,
- update_product_inventory,
- )
- from app.main import app
- from app.parser import read_file
- from app.read_settings import read_settings
- client = TestClient(app)
- def get_inventory_amount(inventories, size: str, style: str) -> int:
- """Helper function to get the inventory amount of the given product
- with specified size and style.
- """
- for inventory in inventories:
- if inventory.size == size and inventory.style == style:
- return inventory.amount
- def test_update_product_inventory():
- """Test the function to update the inventory list of a product. The
- function receives a list of strings after the Stripe or
- NOWPayments checkout, and update each purchased product's
- inventory, by decreasing the inventory amount for each option with
- the amount of copies bought of each product.
- """
- settings = read_settings("settings.toml")
- order_id = pick_last_order_id(settings.git_repo, settings.local_db.filepath)
- product_list = fetch_order_product_list(
- order_id, settings.git_repo, settings.local_db.filepath
- )
- if product_list:
- for product_info in product_list:
- tree = "products"
- # -- store current inventory amount pre-update
- pre_product = read_file(
- settings.git_repo,
- product_info.filename,
- settings.document_match,
- settings.block_types,
- tree,
- )
- pre_inventory_amount = get_inventory_amount(
- pre_product.meta.inventory,
- product_info.size,
- product_info.style,
- )
- # -- update inventory
- update_product_inventory(
- product_info.filename,
- product_info.quantity,
- product_info.size,
- product_info.style,
- settings,
- )
- # -- store updated inventory amount post-update
- post_product = read_file(
- settings.git_repo,
- product_info.filename,
- settings.document_match,
- settings.block_types,
- tree,
- )
- post_inventory_amount = get_inventory_amount(
- post_product.meta.inventory,
- product_info.size,
- product_info.style,
- )
- # -- if pre_inventory_amount is already 0 make sure it
- # stays like that also after the update else do subtraction
- if pre_inventory_amount > 0:
- assert (
- pre_inventory_amount - product_info.quantity
- ) == post_inventory_amount
- else:
- assert pre_inventory_amount == post_inventory_amount
|