More unit tests

This commit is contained in:
Oliver 2021-10-04 20:44:15 +11:00
parent 957867f504
commit 75a35773c6
2 changed files with 115 additions and 2 deletions

View File

@ -23,6 +23,7 @@ class BuildAPITest(InvenTreeAPITestCase):
'location', 'location',
'bom', 'bom',
'build', 'build',
'stock',
] ]
# Required roles to access Build API endpoints # Required roles to access Build API endpoints
@ -116,7 +117,112 @@ class BuildAllocationTest(BuildAPITest):
# No new BuildItem objects have been created during this test # No new BuildItem objects have been created during this test
self.assertEqual(self.n, BuildItem.objects.count()) self.assertEqual(self.n, BuildItem.objects.count())
def test_missing(self):
"""
Test with missing data
"""
# Missing quantity
data = self.post(
self.url,
{
"items": [
{
"bom_item": 1, # M2x4 LPHS
"stock_item": 2, # 5,000 screws available
}
]
},
expected_code=400
).data
self.assertIn('This field is required', str(data["items"][0]["quantity"]))
# Missing bom_item
data = self.post(
self.url,
{
"items": [
{
"stock_item": 2,
"quantity": 5000,
}
]
},
expected_code=400
).data
self.assertIn("This field is required", str(data["items"][0]["bom_item"]))
# Missing stock_item
data = self.post(
self.url,
{
"items": [
{
"bom_item": 1,
"quantity": 5000,
}
]
},
expected_code=400
).data
self.assertIn("This field is required", str(data["items"][0]["stock_item"]))
# No new BuildItem objects have been created during this test
self.assertEqual(self.n, BuildItem.objects.count())
def test_invalid_bom_item(self):
"""
Test by passing an invalid BOM item
"""
data = self.post(
self.url,
{
"items": [
{
"bom_item": 5,
"stock_item": 11,
"quantity": 500,
}
]
},
expected_code=400
).data
self.assertIn('must point to the same part', str(data))
def test_valid_data(self):
"""
Test with valid data.
This should result in creation of a new BuildItem object
"""
data = self.post(
self.url,
{
"items": [
{
"bom_item": 1,
"stock_item": 2,
"quantity": 5000,
}
]
},
expected_code=201
).data
# A new BuildItem should have been created
self.assertEqual(self.n + 1, BuildItem.objects.count())
allocation = BuildItem.objects.last()
self.assertEqual(allocation.quantity, 5000)
self.assertEqual(allocation.bom_item.pk, 1)
self.assertEqual(allocation.stock_item.pk, 2)
class BuildListTest(BuildAPITest): class BuildListTest(BuildAPITest):
""" """

View File

@ -30,4 +30,11 @@
fields: fields:
part: 100 part: 100
sub_part: 50 sub_part: 50
quantity: 3 quantity: 3
- model: part.bomitem
pk: 5
fields:
part: 1
sub_part: 5
quantity: 3