mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-29 20:30:39 +00:00 
			
		
		
		
	Add unit test for new API serializer
This commit is contained in:
		| @@ -19,7 +19,8 @@ def add_default_reference(apps, schema_editor): | |||||||
|         build.save() |         build.save() | ||||||
|         count += 1 |         count += 1 | ||||||
|  |  | ||||||
|     print(f"\nUpdated build reference for {count} existing BuildOrder objects") |     if count > 0: | ||||||
|  |         print(f"\nUpdated build reference for {count} existing BuildOrder objects") | ||||||
|  |  | ||||||
|  |  | ||||||
| def reverse_default_reference(apps, schema_editor): | def reverse_default_reference(apps, schema_editor): | ||||||
|   | |||||||
| @@ -16,8 +16,9 @@ from InvenTree.status_codes import StockStatus | |||||||
| from InvenTree.api_tester import InvenTreeAPITestCase | from InvenTree.api_tester import InvenTreeAPITestCase | ||||||
|  |  | ||||||
| from common.models import InvenTreeSetting | from common.models import InvenTreeSetting | ||||||
|  | import company.models | ||||||
| from .models import StockItem, StockLocation | import part.models | ||||||
|  | from stock.models import StockItem, StockLocation | ||||||
|  |  | ||||||
|  |  | ||||||
| class StockAPITestCase(InvenTreeAPITestCase): | class StockAPITestCase(InvenTreeAPITestCase): | ||||||
| @@ -732,3 +733,110 @@ class StockTestResultTest(StockAPITestCase): | |||||||
|  |  | ||||||
|             # Check that an attachment has been uploaded |             # Check that an attachment has been uploaded | ||||||
|             self.assertIsNotNone(response.data['attachment']) |             self.assertIsNotNone(response.data['attachment']) | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class StockAssignTest(StockAPITestCase): | ||||||
|  |     """ | ||||||
|  |     Unit tests for the stock assignment API endpoint, | ||||||
|  |     where stock items are manually assigned to a customer | ||||||
|  |     """ | ||||||
|  |  | ||||||
|  |     URL = reverse('api-stock-assign') | ||||||
|  |  | ||||||
|  |     def test_invalid(self): | ||||||
|  |  | ||||||
|  |         # Test with empty data | ||||||
|  |         response = self.post( | ||||||
|  |             self.URL, | ||||||
|  |             data={}, | ||||||
|  |             expected_code=400, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertIn('This field is required', str(response.data['items'])) | ||||||
|  |         self.assertIn('This field is required', str(response.data['customer'])) | ||||||
|  |  | ||||||
|  |         # Test with an invalid customer | ||||||
|  |         response = self.post( | ||||||
|  |             self.URL, | ||||||
|  |             data={ | ||||||
|  |                 'customer': 999, | ||||||
|  |             }, | ||||||
|  |             expected_code=400, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertIn('object does not exist', str(response.data['customer'])) | ||||||
|  |  | ||||||
|  |         # Test with a company which is *not* a customer | ||||||
|  |         response = self.post( | ||||||
|  |             self.URL, | ||||||
|  |             data={ | ||||||
|  |                 'customer': 3, | ||||||
|  |             }, | ||||||
|  |             expected_code=400, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertIn('company is not a customer', str(response.data['customer'])) | ||||||
|  |  | ||||||
|  |         # Test with an empty items list | ||||||
|  |         response = self.post( | ||||||
|  |             self.URL, | ||||||
|  |             data={ | ||||||
|  |                 'items': [], | ||||||
|  |                 'customer': 4, | ||||||
|  |             }, | ||||||
|  |             expected_code=400, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertIn('A list of stock items must be provided', str(response.data)) | ||||||
|  |  | ||||||
|  |         stock_item = StockItem.objects.create( | ||||||
|  |             part=part.models.Part.objects.get(pk=1), | ||||||
|  |             status=StockStatus.DESTROYED, | ||||||
|  |             quantity=5, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         response = self.post( | ||||||
|  |             self.URL, | ||||||
|  |             data={ | ||||||
|  |                 'items': [ | ||||||
|  |                     { | ||||||
|  |                         'item': stock_item.pk, | ||||||
|  |                     }, | ||||||
|  |                 ], | ||||||
|  |                 'customer': 4, | ||||||
|  |             }, | ||||||
|  |             expected_code=400, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         self.assertIn('Item must be in stock', str(response.data['items'][0])) | ||||||
|  |  | ||||||
|  |     def test_valid(self): | ||||||
|  |  | ||||||
|  |         stock_items = [] | ||||||
|  |  | ||||||
|  |         for i in range(5): | ||||||
|  |  | ||||||
|  |             stock_item = StockItem.objects.create( | ||||||
|  |                 part=part.models.Part.objects.get(pk=1), | ||||||
|  |                 quantity=i+5, | ||||||
|  |             ) | ||||||
|  |  | ||||||
|  |             stock_items.append({ | ||||||
|  |                 'item': stock_item.pk | ||||||
|  |             }) | ||||||
|  |  | ||||||
|  |         customer = company.models.Company.objects.get(pk=4) | ||||||
|  |  | ||||||
|  |         self.assertEqual(customer.assigned_stock.count(), 0) | ||||||
|  |  | ||||||
|  |         response = self.post( | ||||||
|  |             self.URL, | ||||||
|  |             data={ | ||||||
|  |                 'items': stock_items, | ||||||
|  |                 'customer': 4, | ||||||
|  |             }, | ||||||
|  |             expected_code=201, | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |         # 5 stock items should now have been assigned to this customer | ||||||
|  |         self.assertEqual(customer.assigned_stock.count(), 5) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user