from fastapi.testclient import TestClient from app.db import 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, product_variation_id: str) -> int: """Helper function to get the inventory amount of the given product with specified product_id """ for inventory in inventories: if inventory.product_variation_id == product_variation_id: 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") product_list = { "product_variation_id": "prod_RZBk650b1Tx95N__s", "product_name": "a-tshirt-01", "update_amount": 3, } tree = "products" # -- store current inventory amount pre-update pre_product = read_file( settings.git_repo, product_list["product_name"], settings.document_match, settings.block_types, tree, ) pre_inventory_amount = get_inventory_amount( pre_product.meta.inventory, product_list["product_variation_id"], ) # -- update inventory update_product_inventory( product_list["product_name"], product_list["update_amount"], product_list["product_variation_id"], settings, ) # -- store updated inventory amount post-update post_product = read_file( settings.git_repo, product_list["product_name"], settings.document_match, settings.block_types, tree, ) post_inventory_amount = get_inventory_amount( post_product.meta.inventory, product_list["product_variation_id"] ) # -- 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_list["update_amount"] ) == post_inventory_amount else: assert pre_inventory_amount == post_inventory_amount # Restore the amount to original after the test update_product_inventory( product_list["product_name"], -pre_inventory_amount, product_list["product_variation_id"], settings, )