def test_create_product(): global product_id result = Products.create_one(valid_product) assert result['success'] is True product_id = result['value']['_id'] assert ObjectId(product_id) assert result['value']['name'] == valid_product["name"]
def update_product(*, product_id: str = Path( ..., title="The product ID as a valid ObjectId"), Product: ProductToUpdate, CurrentUser: UserOut = Depends(get_current_user)): # Exclude sellerId because it seems there is no reason to update it return format_result(Products.update_one(product_id, Product, {'sellerId'}))
def delete_product(product_id: str, CurrentUser: UserOut = Depends(get_current_user)): return format_result(Products.delete_one(product_id))
def create_product(Product: ProductToInsert, CurrentUser: UserOut = Depends(get_current_user)): if not (hasattr(CurrentUser, 'sellerId')) or CurrentUser.sellerId is None: raise mpapi_exceptions({'error': 'USER-HAS-NO-SELLERID'}) Product.sellerId = CurrentUser.sellerId return format_result(Products.create_one(Product))
def get_product_by_id(product_id: str = Path( ..., title="The product ID as a valid ObjectId")): return format_result(Products.get_one(product_id))
async def get_products(skip: int = 0, limit: int = 100): return format_result(Products.get_many(skip, limit))
def test_delete_product(): global product_id result = Products.delete_one(product_id) assert result['success'] is True result = Products.get_one(product_id) assert result['success'] is False
def test_update_product(): global product_id name = "Test Update" result = Products.update_one(product_id, {"name": name}) assert result['success'] is True assert result['value']['name'] == name