mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Add unit test for stockitemtracking API (#7459)
This commit is contained in:
		| @@ -2,6 +2,7 @@ | |||||||
|  |  | ||||||
| import io | import io | ||||||
| import os | import os | ||||||
|  | import random | ||||||
| from datetime import datetime, timedelta | from datetime import datetime, timedelta | ||||||
| from enum import IntEnum | from enum import IntEnum | ||||||
|  |  | ||||||
| @@ -22,6 +23,7 @@ from part.models import Part, PartTestTemplate | |||||||
| from stock.models import ( | from stock.models import ( | ||||||
|     StockItem, |     StockItem, | ||||||
|     StockItemTestResult, |     StockItemTestResult, | ||||||
|  |     StockItemTracking, | ||||||
|     StockLocation, |     StockLocation, | ||||||
|     StockLocationType, |     StockLocationType, | ||||||
| ) | ) | ||||||
| @@ -1770,6 +1772,96 @@ class StockTestResultTest(StockAPITestCase): | |||||||
|         ) |         ) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 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): | class StockAssignTest(StockAPITestCase): | ||||||
|     """Unit tests for the stock assignment API endpoint, where stock items are manually assigned to a customer.""" |     """Unit tests for the stock assignment API endpoint, where stock items are manually assigned to a customer.""" | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user