mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-17 12:35:46 +00:00
Tree query improvements (#3443)
* Allow part category table to be ordered by part count * Add queryset annotation for part-category part-count - Uses subquery to annotate the part-count for sub-categories - Huge reduction in number of queries * Update 'pathstring' property of PartCategory and StockLocation - No longer a dynamically calculated value - Constructed when the model is saved, and then written to the database - Limited to 250 characters * Data migration to re-construct pathstring for PartCategory objects * Fix for tree model save() method * Add unit tests for pathstring construction * Data migration for StockLocation pathstring values * Update part API - Add new annotation to PartLocationDetail view * Update API version * Apply similar annotation to StockLocation API endpoints * Extra tests for PartCategory API * Unit test fixes * Allow PartCategory and StockLocation lists to be sorted by 'pathstring' * Further unit test fixes
This commit is contained in:
@ -77,6 +77,76 @@ class PartCategoryAPITest(InvenTreeAPITestCase):
|
||||
|
||||
self.assertEqual(len(response.data), 5)
|
||||
|
||||
# Check that the required fields are present
|
||||
fields = [
|
||||
'pk',
|
||||
'name',
|
||||
'description',
|
||||
'default_location',
|
||||
'level',
|
||||
'parent',
|
||||
'part_count',
|
||||
'pathstring',
|
||||
'url'
|
||||
]
|
||||
|
||||
for result in response.data:
|
||||
for f in fields:
|
||||
self.assertIn(f, result)
|
||||
|
||||
def test_part_count(self):
|
||||
"""Test that the 'part_count' field is annotated correctly"""
|
||||
|
||||
url = reverse('api-part-category-list')
|
||||
|
||||
# Create a parent category
|
||||
cat = PartCategory.objects.create(
|
||||
name='Parent Cat',
|
||||
description='Some name',
|
||||
parent=None
|
||||
)
|
||||
|
||||
# Create child categories
|
||||
for ii in range(10):
|
||||
child = PartCategory.objects.create(
|
||||
name=f"Child cat {ii}",
|
||||
description="A child category",
|
||||
parent=cat
|
||||
)
|
||||
|
||||
# Create parts in this category
|
||||
for jj in range(10):
|
||||
Part.objects.create(
|
||||
name=f"Part xyz {jj}",
|
||||
description="A test part",
|
||||
category=child
|
||||
)
|
||||
|
||||
# Filter by parent category
|
||||
response = self.get(
|
||||
url,
|
||||
{
|
||||
'parent': cat.pk,
|
||||
},
|
||||
expected_code=200
|
||||
)
|
||||
|
||||
# 10 child categories
|
||||
self.assertEqual(len(response.data), 10)
|
||||
|
||||
for result in response.data:
|
||||
self.assertEqual(result['parent'], cat.pk)
|
||||
self.assertEqual(result['part_count'], 10)
|
||||
|
||||
# Detail view for parent category
|
||||
response = self.get(
|
||||
f'/api/part/category/{cat.pk}/',
|
||||
expected_code=200
|
||||
)
|
||||
|
||||
# Annotation should include parts from all sub-categories
|
||||
self.assertEqual(response.data['part_count'], 100)
|
||||
|
||||
def test_category_metadata(self):
|
||||
"""Test metadata endpoint for the PartCategory."""
|
||||
cat = PartCategory.objects.get(pk=1)
|
||||
|
Reference in New Issue
Block a user