Files
inventree-stock-tool/test_add_stock.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

68 lines
2.3 KiB
Python

#!/usr/bin/env python3
"""Test script to verify the add stock response handling."""
def handle_add_stock_response(response_data):
"""
Simulate the add stock response handling logic.
Args:
response_data: The response from POST /api/stock/
Returns:
Stock item ID
"""
if isinstance(response_data, list):
# If it's a list, get the first item
stock_item = response_data[0] if response_data else {}
else:
# If it's a dict, use it directly
stock_item = response_data
sid = stock_item.get('pk', stock_item.get('id'))
return sid
print("Testing add stock response handling:\n")
# Test 1: Response is a dict with 'pk' key
print("Test 1: API returns dict with 'pk'")
response = {'pk': 123, 'quantity': 150, 'part': 456, 'location': 476}
sid = handle_add_stock_response(response)
print(f" Response: {response}")
print(f" Stock ID: {sid}")
print(f" Expected: 123, Got: {sid} - {'PASS' if sid == 123 else 'FAIL'}\n")
# Test 2: Response is a dict with 'id' key
print("Test 2: API returns dict with 'id'")
response = {'id': 456, 'quantity': 150, 'part': 789, 'location': 476}
sid = handle_add_stock_response(response)
print(f" Response: {response}")
print(f" Stock ID: {sid}")
print(f" Expected: 456, Got: {sid} - {'PASS' if sid == 456 else 'FAIL'}\n")
# Test 3: Response is a list with dict containing 'pk'
print("Test 3: API returns list with dict containing 'pk'")
response = [{'pk': 789, 'quantity': 150, 'part': 123, 'location': 476}]
sid = handle_add_stock_response(response)
print(f" Response: {response}")
print(f" Stock ID: {sid}")
print(f" Expected: 789, Got: {sid} - {'PASS' if sid == 789 else 'FAIL'}\n")
# Test 4: Response is a list with dict containing 'id'
print("Test 4: API returns list with dict containing 'id'")
response = [{'id': 999, 'quantity': 150, 'part': 123, 'location': 476}]
sid = handle_add_stock_response(response)
print(f" Response: {response}")
print(f" Stock ID: {sid}")
print(f" Expected: 999, Got: {sid} - {'PASS' if sid == 999 else 'FAIL'}\n")
# Test 5: Empty response
print("Test 5: API returns empty dict")
response = {}
sid = handle_add_stock_response(response)
print(f" Response: {response}")
print(f" Stock ID: {sid}")
print(f" Expected: None, Got: {sid} - {'PASS' if sid is None else 'FAIL'}\n")
print("All tests completed!")