From 95d07cd02b6c56f64bceca04c1d0164ba8572261 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 17 May 2020 14:14:54 +1000 Subject: [PATCH] Add unit testing for new API features --- InvenTree/part/fixtures/part.yaml | 7 +++- InvenTree/part/models.py | 15 +++++++-- InvenTree/part/test_api.py | 54 +++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/InvenTree/part/fixtures/part.yaml b/InvenTree/part/fixtures/part.yaml index 035049fe81..76a073261b 100644 --- a/InvenTree/part/fixtures/part.yaml +++ b/InvenTree/part/fixtures/part.yaml @@ -106,6 +106,7 @@ name: 'Chair Template' description: 'A chair' is_template: True + trackable: true category: 7 tree_id: 1 level: 0 @@ -117,6 +118,7 @@ fields: name: 'Blue Chair' variant_of: 10000 + trackable: true category: 7 tree_id: 1 level: 0 @@ -128,6 +130,7 @@ fields: name: 'Red chair' variant_of: 10000 + trackable: true category: 7 tree_id: 1 level: 0 @@ -140,6 +143,7 @@ name: 'Green chair' variant_of: 10000 category: 7 + trackable: true tree_id: 1 level: 0 lft: 0 @@ -150,7 +154,8 @@ fields: name: 'Green chair variant' variant_of: 10003 - category: + category: 7 + trackable: true tree_id: 1 level: 0 lft: 0 diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 76a5965aae..81b8206469 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1146,17 +1146,26 @@ class PartTestTemplate(models.Model): def save(self, *args, **kwargs): - self.validate_unique() self.clean() 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): """ 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 tests = PartTestTemplate.objects.filter( @@ -1175,6 +1184,8 @@ class PartTestTemplate(models.Model): 'test_name': _("Test with this name already exists for this part") }) + super().validate_unique(exclude) + @property def key(self): """ Generate a key for this test """ diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index 61e7f4ab32..9fcf98d712 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -16,6 +16,7 @@ class PartAPITest(APITestCase): 'part', 'location', 'bom', + 'test_templates', ] def setUp(self): @@ -159,3 +160,56 @@ class PartAPITest(APITestCase): data['part'] = 2 data['sub_part'] = 2 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)