diff --git a/src/backend/InvenTree/stock/test_api.py b/src/backend/InvenTree/stock/test_api.py index 91dbccfbb0bb..a0cf28c3bfcf 100644 --- a/src/backend/InvenTree/stock/test_api.py +++ b/src/backend/InvenTree/stock/test_api.py @@ -2,6 +2,7 @@ import io import os +import random from datetime import datetime, timedelta from enum import IntEnum @@ -22,6 +23,7 @@ from stock.models import ( StockItem, StockItemTestResult, + StockItemTracking, StockLocation, StockLocationType, ) @@ -1770,6 +1772,96 @@ def test_value_choices(self): ) +class StockTrackingTest(StockAPITestCase): + """Tests for the StockTracking API endpoints.""" + + fixtures = [ + *StockAPITestCase.fixtures, + 'build', + 'order', + 'return_order', + 'sales_order', + ] + + @classmethod + def setUpTestData(cls): + """Initialize some test data for the StockTracking tests.""" + super().setUpTestData() + + import build.models + import company.models + import order.models + import stock.models + import stock.status_codes + + entries = [] + + N_BO = build.models.Build.objects.count() + N_PO = order.models.PurchaseOrder.objects.count() + N_RO = order.models.ReturnOrder.objects.count() + N_SO = order.models.SalesOrder.objects.count() + + N_COMPANY = company.models.Company.objects.count() + N_LOCATION = stock.models.StockLocation.objects.count() + + # Generate a large quantity of tracking items + # Note that the pk values are not guaranteed to exist in the database + for item in StockItem.objects.all(): + for i in range(50): + entries.append( + StockItemTracking( + item=item, + notes='This is a test entry', + tracking_type=stock.status_codes.StockHistoryCode.LEGACY.value, + deltas={ + 'quantity': 50 - i, + 'buildorder': random.randint(0, N_BO + 1), + 'purchaseorder': random.randint(0, N_PO + 1), + 'returnorder': random.randint(0, N_RO + 1), + 'salesorder': random.randint(0, N_SO + 1), + 'customer': random.randint(0, N_COMPANY + 1), + 'location': random.randint(0, N_LOCATION + 1), + }, + ) + ) + + StockItemTracking.objects.bulk_create(entries) + + def get_url(self): + """Helper function to get stock tracking api url.""" + return reverse('api-stock-tracking-list') + + def test_count(self): + """Test list endpoint with limit = 1.""" + url = self.get_url() + + N = StockItemTracking.objects.count() + + # Test counting + response = self.get(url, {'limit': 1}) + self.assertEqual(response.data['count'], N) + + def test_list(self): + """Test list endpoint.""" + url = self.get_url() + + N = StockItemTracking.objects.count() + self.assertGreater(N, 1000) + + response = self.client.get(url, max_query_count=25) + self.assertEqual(len(response.data), N) + + # Check expected delta values + keys = ['quantity', 'returnorder', 'buildorder', 'customer'] + + for item in response.data: + deltas = item['deltas'] + + for key in keys: + self.assertIn(key, deltas) + self.assertIsNotNone(deltas.get(key, None)) + + class StockAssignTest(StockAPITestCase): """Unit tests for the stock assignment API endpoint, where stock items are manually assigned to a customer."""