Skip to content

Commit 3d8c751

Browse files
Add unit testing for creating build outputs
1 parent 5289609 commit 3d8c751

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

inventree/build.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,16 @@ def getBuildOutputs(self, complete: bool = None, **kwargs):
7272

7373
def createBuildOutput(self, **kwargs):
7474
""" Create a new build output (stock item) associated with this build order """
75-
return self._api.post(
76-
f'{self.URL}create_output/',
75+
result = self._api.post(
76+
f'{self.URL}{self.pk}/create-output/',
7777
data={
7878
**kwargs
7979
}
8080
)
8181

82+
# Note: The response is a list of created stock items
83+
return [inventree.stock.StockItem(self._api, item['pk'], item) for item in result]
84+
8285
def cancelBuildOutputs(self, outputs):
8386
""" Cancel a build output item associated with this build order
8487

test/test_build.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,55 @@ def test_build_complete(self):
144144
# Check status
145145
self.assertEqual(build.status, 40)
146146
self.assertEqual(build.status_text, 'Complete')
147+
148+
149+
class BuildOrderOutputTests(InvenTreeTestCase):
150+
""" Unit tests for build output functionality """
151+
152+
def setUp(self):
153+
""" Ensure we have a base build order to work with """
154+
155+
super().setUp()
156+
157+
builds = Build.list(self.api)
158+
159+
self.build = Build.create(
160+
self.api,
161+
{
162+
"title": "A new build order",
163+
"part": 25,
164+
"quantity": 10,
165+
"reference": f"BO-{len(builds) + 1:04d}"
166+
}
167+
)
168+
169+
def test_create_build_output(self):
170+
"""Test that we can create a build output item"""
171+
172+
# Initially, there should be no build outputs
173+
outputs = self.build.getBuildOutputs()
174+
self.assertEqual(len(outputs), 0)
175+
176+
# Let's create 3 new outputs (with serial numbers)
177+
outputs = self.build.createBuildOutput(
178+
quantity=3,
179+
batch_code='TEST-BATCH-001',
180+
serial_numbers='300+'
181+
)
182+
183+
self.assertEqual(len(outputs), 3)
184+
self.assertEqual(len(self.build.getBuildOutputs()), 3)
185+
186+
for output in outputs:
187+
self.assertIsNotNone(output)
188+
self.assertEqual(output.quantity, 1)
189+
self.assertEqual(output.batch, 'TEST-BATCH-001')
190+
self.assertEqual(output.build, self.build.pk)
191+
self.assertEqual(output.part, self.build.part)
192+
self.assertTrue(output.is_building)
193+
194+
# Directly delete the build output
195+
output.delete()
196+
197+
# There should now be no build outputs again
198+
self.assertEqual(len(self.build.getBuildOutputs()), 0)

0 commit comments

Comments
 (0)