Files
inventree-stock-tool/test_stock_level.py
grabowski ab0d1ae0db Initial commit: InvenTree Stock Tool v2
A comprehensive barcode scanning application for InvenTree inventory management.

Features:
- Multi-mode operation (Add/Update/Check/Locate stock)
- Smart duplicate prevention when adding stock
- Barcode scanning with automatic part code cleaning
- Real-time server connection monitoring
- Part information display with images
- Debug mode for troubleshooting

Fixes applied:
- Fixed encoding issues with non-ASCII characters in barcodes
- Fixed API response handling for list and dict formats
- Implemented duplicate prevention using PATCH to update existing stock
- Added comprehensive error handling and logging

Includes test suite for verification of all fixes.
2025-10-28 16:31:48 +07:00

78 lines
2.1 KiB
Python

#!/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!")