mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 07:05:41 +00:00 
			
		
		
		
	Add unit testing for new API features
This commit is contained in:
		@@ -106,6 +106,7 @@
 | 
				
			|||||||
    name: 'Chair Template'
 | 
					    name: 'Chair Template'
 | 
				
			||||||
    description: 'A chair'
 | 
					    description: 'A chair'
 | 
				
			||||||
    is_template: True
 | 
					    is_template: True
 | 
				
			||||||
 | 
					    trackable: true
 | 
				
			||||||
    category: 7
 | 
					    category: 7
 | 
				
			||||||
    tree_id: 1
 | 
					    tree_id: 1
 | 
				
			||||||
    level: 0
 | 
					    level: 0
 | 
				
			||||||
@@ -117,6 +118,7 @@
 | 
				
			|||||||
  fields:
 | 
					  fields:
 | 
				
			||||||
    name: 'Blue Chair'
 | 
					    name: 'Blue Chair'
 | 
				
			||||||
    variant_of: 10000
 | 
					    variant_of: 10000
 | 
				
			||||||
 | 
					    trackable: true
 | 
				
			||||||
    category: 7
 | 
					    category: 7
 | 
				
			||||||
    tree_id: 1
 | 
					    tree_id: 1
 | 
				
			||||||
    level: 0
 | 
					    level: 0
 | 
				
			||||||
@@ -128,6 +130,7 @@
 | 
				
			|||||||
  fields:
 | 
					  fields:
 | 
				
			||||||
    name: 'Red chair'
 | 
					    name: 'Red chair'
 | 
				
			||||||
    variant_of: 10000
 | 
					    variant_of: 10000
 | 
				
			||||||
 | 
					    trackable: true
 | 
				
			||||||
    category: 7
 | 
					    category: 7
 | 
				
			||||||
    tree_id: 1
 | 
					    tree_id: 1
 | 
				
			||||||
    level: 0
 | 
					    level: 0
 | 
				
			||||||
@@ -140,6 +143,7 @@
 | 
				
			|||||||
    name: 'Green chair'
 | 
					    name: 'Green chair'
 | 
				
			||||||
    variant_of: 10000
 | 
					    variant_of: 10000
 | 
				
			||||||
    category: 7
 | 
					    category: 7
 | 
				
			||||||
 | 
					    trackable: true
 | 
				
			||||||
    tree_id: 1
 | 
					    tree_id: 1
 | 
				
			||||||
    level: 0
 | 
					    level: 0
 | 
				
			||||||
    lft: 0
 | 
					    lft: 0
 | 
				
			||||||
@@ -150,7 +154,8 @@
 | 
				
			|||||||
  fields:
 | 
					  fields:
 | 
				
			||||||
    name: 'Green chair variant'
 | 
					    name: 'Green chair variant'
 | 
				
			||||||
    variant_of: 10003
 | 
					    variant_of: 10003
 | 
				
			||||||
    category: 
 | 
					    category: 7
 | 
				
			||||||
 | 
					    trackable: true
 | 
				
			||||||
    tree_id: 1
 | 
					    tree_id: 1
 | 
				
			||||||
    level: 0
 | 
					    level: 0
 | 
				
			||||||
    lft: 0
 | 
					    lft: 0
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1146,17 +1146,26 @@ class PartTestTemplate(models.Model):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def save(self, *args, **kwargs):
 | 
					    def save(self, *args, **kwargs):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.validate_unique()
 | 
					 | 
				
			||||||
        self.clean()
 | 
					        self.clean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        super().save(*args, **kwargs)
 | 
					        super().save(*args, **kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def clean(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.test_name = self.test_name.strip()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.validate_unique()
 | 
				
			||||||
 | 
					        super().clean()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def validate_unique(self, exclude=None):
 | 
					    def validate_unique(self, exclude=None):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
        Test that this test template is 'unique' within this part tree.
 | 
					        Test that this test template is 'unique' within this part tree.
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        super().validate_unique(exclude)
 | 
					        if not self.part.trackable:
 | 
				
			||||||
 | 
					            raise ValidationError({
 | 
				
			||||||
 | 
					                'part': _('Test templates can only be created for trackable parts')
 | 
				
			||||||
 | 
					            })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # Get a list of all tests "above" this one
 | 
					        # Get a list of all tests "above" this one
 | 
				
			||||||
        tests = PartTestTemplate.objects.filter(
 | 
					        tests = PartTestTemplate.objects.filter(
 | 
				
			||||||
@@ -1175,6 +1184,8 @@ class PartTestTemplate(models.Model):
 | 
				
			|||||||
                    'test_name': _("Test with this name already exists for this part")
 | 
					                    'test_name': _("Test with this name already exists for this part")
 | 
				
			||||||
                })
 | 
					                })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        super().validate_unique(exclude)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @property
 | 
					    @property
 | 
				
			||||||
    def key(self):
 | 
					    def key(self):
 | 
				
			||||||
        """ Generate a key for this test """
 | 
					        """ Generate a key for this test """
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,7 @@ class PartAPITest(APITestCase):
 | 
				
			|||||||
        'part',
 | 
					        'part',
 | 
				
			||||||
        'location',
 | 
					        'location',
 | 
				
			||||||
        'bom',
 | 
					        'bom',
 | 
				
			||||||
 | 
					        'test_templates',
 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
@@ -159,3 +160,56 @@ class PartAPITest(APITestCase):
 | 
				
			|||||||
        data['part'] = 2
 | 
					        data['part'] = 2
 | 
				
			||||||
        data['sub_part'] = 2
 | 
					        data['sub_part'] = 2
 | 
				
			||||||
        response = self.client.post(url, data, format='json')
 | 
					        response = self.client.post(url, data, format='json')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_test_templates(self):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        url = reverse('api-part-test-template-list')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # List ALL items
 | 
				
			||||||
 | 
					        response = self.client.get(url)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, status.HTTP_200_OK)
 | 
				
			||||||
 | 
					        self.assertEqual(len(response.data), 7)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Request for a particular part
 | 
				
			||||||
 | 
					        response = self.client.get(url, data={'part': 10000})
 | 
				
			||||||
 | 
					        self.assertEqual(len(response.data), 5)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        response = self.client.get(url, data={'part': 10004})
 | 
				
			||||||
 | 
					        self.assertEqual(len(response.data), 7)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Try to post a new object (should succeed)
 | 
				
			||||||
 | 
					        response = self.client.post(
 | 
				
			||||||
 | 
					            url,
 | 
				
			||||||
 | 
					            data={
 | 
				
			||||||
 | 
					                'part': 10000,
 | 
				
			||||||
 | 
					                'test_name': 'New Test',
 | 
				
			||||||
 | 
					                'required': True,
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            format='json',
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Try to post a new test with the same name (should fail)
 | 
				
			||||||
 | 
					        response = self.client.post(
 | 
				
			||||||
 | 
					            url,
 | 
				
			||||||
 | 
					            data={
 | 
				
			||||||
 | 
					                'part': 10004,
 | 
				
			||||||
 | 
					                'test_name': "   newtest"
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            format='json',
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        # Try to post a new test against a non-trackable part (should fail)
 | 
				
			||||||
 | 
					        response = self.client.post(
 | 
				
			||||||
 | 
					            url,
 | 
				
			||||||
 | 
					            data={
 | 
				
			||||||
 | 
					                'part': 1,
 | 
				
			||||||
 | 
					                'test_name': 'A simple test',
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user