-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtest_stock.py
More file actions
498 lines (355 loc) · 14.4 KB
/
test_stock.py
File metadata and controls
498 lines (355 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# -*- coding: utf-8 -*-
import os
import sys
import requests
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
from test_api import InvenTreeTestCase # noqa: E402
from inventree import company # noqa: E402
from inventree import part # noqa: E402
from inventree.stock import StockItem, StockLocation # noqa: E402
class StockLocationTest(InvenTreeTestCase):
"""
Tests for the StockLocation model
Fixture data can be found in the InvenTree source:
- InvenTree/stock/fixtures/location.yaml
"""
def test_location_list(self):
"""
Test the LIST API endpoint for the StockLocation model
"""
locs = StockLocation.list(self.api)
self.assertGreaterEqual(len(locs), 4)
for loc in locs:
self.assertEqual(type(loc), StockLocation)
def test_location_create(self):
"""
Check that we can create a new stock location via the APi
"""
n = len(StockLocation.list(self.api))
parent = StockLocation(self.api, pk=7)
n_childs = len(parent.getChildLocations())
# Create a sublocation with a unique name
location = StockLocation.create(
self.api,
{
"name": f"My special location {n_childs}",
"description": "A location created with the API!",
"parent": 7
}
)
self.assertIsNotNone(location)
# Now, request back via the API using a secondary object
loc = StockLocation(self.api, pk=location.pk)
self.assertEqual(loc.name, f"My special location {n_childs}")
self.assertEqual(loc.parent, 7)
# Change the name of the location
loc.save({
"name": f"A whole new name {n_childs}",
})
# Reload the original object
location.reload()
self.assertEqual(location.name, f"A whole new name {n_childs}")
# Check that the number of locations has been updated
locs = StockLocation.list(self.api)
self.assertEqual(len(locs), n + 1)
def test_location_stock(self):
"""Query stock by location"""
location = StockLocation(self.api, pk=4)
self.assertEqual(location.pk, 4)
self.assertEqual(location.description, "Place of work")
items = location.getStockItems()
self.assertGreaterEqual(len(items), 15)
# Check specific part stock in location 1 (initially empty)
items = location.getStockItems(part=1)
n = len(items)
for i in range(5):
StockItem.create(
self.api,
{
"part": 1,
"quantity": (i + 1) * 50,
"location": location.pk,
}
)
items = location.getStockItems(part=1)
self.assertEqual(len(items), n + i + 1)
items = location.getStockItems(part=5)
self.assertGreaterEqual(len(items), 1)
for item in items:
self.assertEqual(item.location, location.pk)
self.assertEqual(item.part, 5)
def test_location_parent(self):
"""
Return the Parent location
"""
# This location does not have a parent
location = StockLocation(self.api, pk=4)
self.assertIsNone(location.parent)
parent = location.getParentLocation()
self.assertIsNone(parent)
# Now, get a location which *does* have a parent
location = StockLocation(self.api, pk=7)
self.assertIsNotNone(location.parent)
self.assertEqual(location.parent, 4)
parent = location.getParentLocation()
self.assertEqual(type(parent), StockLocation)
self.assertEqual(parent.pk, 4)
self.assertIsNone(parent.parent)
self.assertIsNone(parent.getParentLocation())
children = parent.getChildLocations()
self.assertGreaterEqual(len(children), 2)
for child in children:
self.assertEqual(type(child), StockLocation)
self.assertEqual(child.parent, parent.pk)
class StockTest(InvenTreeTestCase):
"""
Test alternative ways of getting StockItem objects.
Fixture data can be found in the InvenTree source:
- InvenTree/stock/fixtures/stock.yaml
"""
def test_stock(self):
items = StockItem.list(self.api, part=1)
n = len(items)
self.assertGreaterEqual(n, 2)
for item in items:
self.assertEqual(item.part, 1)
# Request via the Part instance (results should be the same!)
items = part.Part(self.api, 1).getStockItems()
self.assertEqual(len(items), n)
def test_get_stock_item(self):
"""
StockItem API tests.
Refer to fixture data in InvenTree/stock/fixtures/stock.yaml
"""
# Grab the first available stock item
item = StockItem.list(self.api, in_stock=True, limit=1)[0]
# Get the Part reference
prt = item.getPart()
self.assertEqual(type(prt), part.Part)
# Move the item to a known location
item.transferStock(3)
item.reload()
location = item.getLocation()
self.assertEqual(type(location), StockLocation)
self.assertEqual(location.pk, 3)
self.assertEqual(location.name, "Dining Room")
def test_bulk_delete(self):
"""Test bulk deletion of stock items"""
# Add some items to location 3
for i in range(10):
StockItem.create(self.api, {
'location': 3,
'part': 1,
'quantity': i + 50,
})
self.assertTrue(len(StockItem.list(self.api, location=3)) >= 10)
# Delete *all* items from location 3
items = [item.pk for item in StockItem.list(self.api, location=3)]
StockItem.bulkDelete(self.api, items=list(items))
loc = StockLocation(self.api, pk=3)
items = loc.getStockItems()
self.assertEqual(len(items), 0)
def test_barcode_support(self):
"""Test barcode support for the StockItem model"""
items = StockItem.list(self.api, limit=10)
for item in items:
# Delete any existing barcode
item.unassignBarcode()
# Perform lookup based on 'internal' barcode
response = self.api.scanBarcode(
{
"stockitem": item.pk,
}
)
self.assertEqual(response['stockitem']['pk'], item.pk)
self.assertEqual(response['plugin'], 'InvenTreeBarcode')
# Assign a custom barcode to this StockItem
barcode = f"custom-stock-item-{item.pk}"
item.assignBarcode(barcode)
response = self.api.scanBarcode(barcode)
self.assertEqual(response['stockitem']['pk'], item.pk)
self.assertEqual(response['plugin'], 'InvenTreeBarcode')
self.assertEqual(response['barcode_data'], barcode)
item.unassignBarcode()
def test_serialized(self):
"""Test serializing multiple objects on create"""
# Create items with serial numbers
items = StockItem.create(
self.api,
{
"part": 10004,
"quantity": 3,
"serial_numbers": "1005,1006,1007"
}
)
self.assertEqual(3, len(items))
self.assertEqual('1005', items[0].serial)
self.assertEqual('1006', items[1].serial)
self.assertEqual('1007', items[2].serial)
# Delete the items after the test
for item in items:
item.delete()
class StockAdjustTest(InvenTreeTestCase):
"""Unit tests for stock 'adjustment' actions"""
def test_count(self):
"""Test the 'count' action"""
# Find the first available stock item
item = StockItem.list(self.api, in_stock=True, limit=1)[0]
# Count number of tracking entries
n_tracking = len(item.getTrackingEntries())
q = item.quantity
item.countStock(q + 100)
item.reload()
self.assertEqual(item.quantity, q + 100)
item.countStock(q, notes='Why hello there')
item.reload()
self.assertEqual(item.quantity, q)
# 2 tracking entries should have been added
self.assertEqual(
len(item.getTrackingEntries()),
n_tracking + 2
)
# The most recent tracking entry should have a note
t = item.getTrackingEntries()[0]
self.assertEqual(t.label, 'Stock counted')
# Check error conditions
with self.assertRaises(requests.exceptions.HTTPError):
item.countStock('not a number')
with self.assertRaises(requests.exceptions.HTTPError):
item.countStock(-1)
def test_add_remove(self):
"""Test the 'add' and 'remove' actions"""
# Find the first available stock item
item = StockItem.list(self.api, in_stock=True, limit=1)[0]
n_tracking = len(item.getTrackingEntries())
q = item.quantity
# Add some items
item.addStock(10)
item.reload()
self.assertEqual(item.quantity, q + 10)
# Remove the items again
item.removeStock(10)
item.reload()
self.assertEqual(item.quantity, q)
# 2 additional tracking entries should have been added
self.assertTrue(len(item.getTrackingEntries()) > n_tracking)
# Test error conditions
for v in [-1, 'gg', None]:
with self.assertRaises(requests.exceptions.HTTPError):
item.addStock(v)
with self.assertRaises(requests.exceptions.HTTPError):
item.removeStock(v)
def test_transfer(self):
"""Unit test for 'transfer' action"""
item = StockItem(self.api, pk=2)
n_tracking = len(item.getTrackingEntries())
# Transfer to a StockLocation instance
location = StockLocation(self.api, pk=1)
item.transferStock(location)
item.reload()
self.assertEqual(item.location, 1)
# Transfer with a location ID
item.transferStock(2)
item.reload()
self.assertEqual(item.location, 2)
# 2 additional tracking entries should have been added
self.assertTrue(len(item.getTrackingEntries()) > n_tracking)
# Attempt to transfer to an invalid location
for loc in [-1, 'qqq', 99999, None]:
with self.assertRaises(requests.exceptions.HTTPError):
item.transferStock(loc)
# Attempt to transfer with an invalid quantity
for q in [-1, None, 'hhhh']:
with self.assertRaises(requests.exceptions.HTTPError):
item.transferStock(loc, quantity=q)
def test_transfer_multiple(self):
"""Test transfer of *multiple* items"""
items = StockItem.list(self.api, in_stock=True, location=1)
self.assertTrue(len(items) > 1)
# Construct data to send
data = []
for item in items:
data.append({
'pk': item.pk,
'quantity': item.quantity,
})
# Transfer all items into a new location
StockItem.transferStockItems(self.api, data, 2)
for item in items:
item.reload()
self.assertEqual(item.location, 2)
# Transfer back to the original location
StockItem.transferStockItems(self.api, data, 1)
for item in items:
item.reload()
self.assertEqual(item.location, 1)
history = item.getTrackingEntries()
self.assertTrue(len(history) >= 2)
self.assertEqual(history[0].label, 'Location changed')
def test_assign_stock(self):
"""Test assigning stock to customer"""
items = StockItem.list(self.api)
self.assertTrue(len(items) > 1)
# Get first Company which is a customer
customer = company.Company.list(self.api, is_customer=True)[0]
# Get first part which is salable
assignpart = part.Part.list(self.api, salable=True)[0]
# Create stock item which can be assigned
assignitem = StockItem.create(
self.api,
{
"part": assignpart.pk,
"quantity": 10,
}
)
# Verify a single result was returned
self.assertEqual(1, len(assignitem))
assignitem = assignitem[0]
# Assign the item
assignitem.assignStock(customer=customer, notes='Sell on the side')
# Reload the item
assignitem.reload()
# Check the item is assigned
self.assertTrue(assignitem.customer == customer.pk)
def test_install_stock(self):
"""Test install and uninstall a stock item from another"""
items = StockItem.list(self.api, available=True)
self.assertTrue(len(items) > 1)
# get a parent and a child part
parent_part = part.Part.list(
self.api,
trackable=True,
assembly=True,
has_stock=True
)[0]
parent_stock = parent_part.getStockItems()[0]
child_stock = items[0]
child_part = child_stock.getPart()
# make sure the child is in the bom of the parent
items = parent_part.getBomItems(search=child_part.name)
if not items:
part.BomItem.create(
self.api, {
'part': parent_part.pk,
'sub_part': child_part.pk,
'quantity': 1
}
)
self.assertIsNone(child_stock.belongs_to)
# Attempt to install with incorrect quantity
with self.assertRaises(requests.exceptions.HTTPError):
parent_stock.installStock(child_stock, quantity=child_stock.quantity * 2)
with self.assertRaises(requests.exceptions.HTTPError):
parent_stock.installStock(child_stock, quantity=-100)
# install the *entire* child item into the parent
parent_stock.installStock(child_stock, quantity=child_stock.quantity)
child_stock.reload()
self.assertIsNotNone(child_stock.belongs_to)
self.assertEqual(child_stock.belongs_to, parent_stock.pk)
# and uninstall it again
location = StockLocation.list(self.api)[0]
child_stock.uninstallStock(location)
# check if the location is set correctly to confirm the uninstall
child_stock.reload()
new_location = child_stock.getLocation()
self.assertTrue(new_location.pk == location.pk)