#!/usr/bin/env python3 """Test script to verify find_stock_item and get_stock_level work correctly.""" from typing import Optional, Dict, Any def find_stock_item(data_response: Any) -> Optional[Dict[str, Any]]: """ Simulate find_stock_item parsing logic. Args: data_response: The response from the API Returns: Stock item dictionary or None if not found """ data = data_response results = data.get('results', data) if isinstance(data, dict) else data return results[0] if results else None def get_stock_level(item: Optional[Dict[str, Any]]) -> float: """ Get stock level from item. Args: item: Stock item dictionary Returns: Stock quantity """ return item.get('quantity', 0) if item else 0 # Test cases print("Test 1: API returns dict with 'results' key") api_response_1 = { 'results': [ {'pk': 123, 'quantity': 50, 'part': 456, 'location': 789} ] } item = find_stock_item(api_response_1) stock = get_stock_level(item) print(f" Item: {item}") print(f" Stock level: {stock}") print(f" Expected: 50, Got: {stock} - {'PASS' if stock == 50 else 'FAIL'}") print() print("Test 2: API returns dict with empty results") api_response_2 = {'results': []} item = find_stock_item(api_response_2) stock = get_stock_level(item) print(f" Item: {item}") print(f" Stock level: {stock}") print(f" Expected: 0, Got: {stock} - {'PASS' if stock == 0 else 'FAIL'}") print() print("Test 3: API returns list directly") api_response_3 = [ {'pk': 124, 'quantity': 100, 'part': 456, 'location': 789} ] item = find_stock_item(api_response_3) stock = get_stock_level(item) print(f" Item: {item}") print(f" Stock level: {stock}") print(f" Expected: 100, Got: {stock} - {'PASS' if stock == 100 else 'FAIL'}") print() print("Test 4: API returns empty list") api_response_4 = [] item = find_stock_item(api_response_4) stock = get_stock_level(item) print(f" Item: {item}") print(f" Stock level: {stock}") print(f" Expected: 0, Got: {stock} - {'PASS' if stock == 0 else 'FAIL'}") print() print("All tests completed!")