diff --git a/InvenTree/part/fixtures/part.yaml b/InvenTree/part/fixtures/part.yaml index d0a2d949b1..fd38036fa9 100644 --- a/InvenTree/part/fixtures/part.yaml +++ b/InvenTree/part/fixtures/part.yaml @@ -177,6 +177,7 @@ fields: name: 'Green chair variant' variant_of: 10003 + is_template: true category: 7 trackable: true tree_id: 1 diff --git a/InvenTree/part/test_api.py b/InvenTree/part/test_api.py index 5a8acbecd9..ddfaa44e94 100644 --- a/InvenTree/part/test_api.py +++ b/InvenTree/part/test_api.py @@ -567,6 +567,97 @@ class PartAPITest(InvenTreeAPITestCase): self.assertEqual(response.data['name'], name) self.assertEqual(response.data['description'], description) + def test_template_filters(self): + """ + Unit tests for API filters related to template parts: + + - variant_of : Return children of specified part + - ancestor : Return descendants of specified part + + Uses the 'chair template' part (pk=10000) + """ + + # Rebuild the MPTT structure before running these tests + Part.objects.rebuild() + + url = reverse('api-part-list') + + response = self.get( + url, + { + 'variant_of': 10000, + }, + expected_code=200 + ) + + # 3 direct children of template part + self.assertEqual(len(response.data), 3) + + response = self.get( + url, + { + 'ancestor': 10000, + }, + expected_code=200, + ) + + # 4 total descendants + self.assertEqual(len(response.data), 4) + + # Use the 'green chair' as our reference + response = self.get( + url, + { + 'variant_of': 10003, + }, + expected_code=200, + ) + + self.assertEqual(len(response.data), 1) + + response = self.get( + url, + { + 'ancestor': 10003, + }, + expected_code=200, + ) + + self.assertEqual(len(response.data), 1) + + # Add some more variants + + p = Part.objects.get(pk=10004) + + for i in range(100): + Part.objects.create( + name=f'Chair variant {i}', + description='A new chair variant', + variant_of=p, + ) + + # There should still be only one direct variant + response = self.get( + url, + { + 'variant_of': 10003, + }, + expected_code=200, + ) + + self.assertEqual(len(response.data), 1) + + # However, now should be 101 descendants + response = self.get( + url, + { + 'ancestor': 10003, + }, + expected_code=200, + ) + + self.assertEqual(len(response.data), 101) + class PartDetailTests(InvenTreeAPITestCase): """