Add unit test for 'available_variant_stock' in BomItem API serializer

This commit is contained in:
Oliver 2022-04-26 17:05:21 +10:00
parent 7aaa4a5812
commit ee47be4c9e
2 changed files with 40 additions and 1 deletions

View File

@ -7,6 +7,7 @@
part: 100
sub_part: 1
quantity: 10
allow_variants: True
# 40 x R_2K2_0805
- model: part.bomitem

View File

@ -698,7 +698,7 @@ class PartAPITest(InvenTreeAPITestCase):
part=variant,
quantity=100,
)
response = self.get('/api/part/10000/', {}, expected_code=200)
self.assertEqual(response.data['in_stock'], 0)
@ -1629,6 +1629,44 @@ class BomItemTest(InvenTreeAPITestCase):
self.assertEqual(len(response.data), i)
def test_bom_variant_stock(self):
"""
Test for 'available_variant_stock' annotation
"""
Part.objects.rebuild()
# BOM item we are interested in
bom_item = BomItem.objects.get(pk=1)
response = self.get('/api/bom/1/', {}, expected_code=200)
# Initially, no variant stock available
self.assertEqual(response.data['available_variant_stock'], 0)
# Create some 'variants' of the referenced sub_part
bom_item.sub_part.is_template = True
bom_item.sub_part.save()
for i in range(10):
# Create a variant part
vp = Part.objects.create(
name=f"Var {i}",
description="Variant part",
variant_of=bom_item.sub_part,
)
# Create a stock item
StockItem.objects.create(
part=vp,
quantity=100,
)
# There should now be variant stock available
response = self.get('/api/bom/1/', {}, expected_code=200)
self.assertEqual(response.data['available_variant_stock'], 1000)
class PartParameterTest(InvenTreeAPITestCase):
"""