mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Adds unit testing for barcode field
This commit is contained in:
parent
bf05c9cfae
commit
f38bf6e20a
@ -311,6 +311,7 @@ class POReceive(generics.CreateAPIView):
|
|||||||
item['quantity'],
|
item['quantity'],
|
||||||
self.request.user,
|
self.request.user,
|
||||||
status=item['status'],
|
status=item['status'],
|
||||||
|
barcode=item.get('barcode', ''),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -411,6 +411,11 @@ class PurchaseOrder(Order):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
notes = kwargs.get('notes', '')
|
notes = kwargs.get('notes', '')
|
||||||
|
barcode = kwargs.get('barcode', '')
|
||||||
|
|
||||||
|
# Prevent null values for barcode
|
||||||
|
if barcode is None:
|
||||||
|
barcode = ''
|
||||||
|
|
||||||
if not self.status == PurchaseOrderStatus.PLACED:
|
if not self.status == PurchaseOrderStatus.PLACED:
|
||||||
raise ValidationError({"status": _("Lines can only be received against an order marked as 'Placed'")})
|
raise ValidationError({"status": _("Lines can only be received against an order marked as 'Placed'")})
|
||||||
@ -434,6 +439,7 @@ class PurchaseOrder(Order):
|
|||||||
purchase_order=self,
|
purchase_order=self,
|
||||||
status=status,
|
status=status,
|
||||||
purchase_price=line.purchase_price,
|
purchase_price=line.purchase_price,
|
||||||
|
uid=barcode
|
||||||
)
|
)
|
||||||
|
|
||||||
stock.save(add_note=False)
|
stock.save(add_note=False)
|
||||||
|
@ -233,6 +233,8 @@ class POLineItemReceiveSerializer(serializers.Serializer):
|
|||||||
barcode = serializers.CharField(
|
barcode = serializers.CharField(
|
||||||
label=_('Barcode Hash'),
|
label=_('Barcode Hash'),
|
||||||
help_text=_('Unique identifier field'),
|
help_text=_('Unique identifier field'),
|
||||||
|
default='',
|
||||||
|
required=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
def validate_barcode(self, barcode):
|
def validate_barcode(self, barcode):
|
||||||
@ -247,6 +249,8 @@ class POLineItemReceiveSerializer(serializers.Serializer):
|
|||||||
if stock.models.StockItem.objects.filter(uid=barcode).exists():
|
if stock.models.StockItem.objects.filter(uid=barcode).exists():
|
||||||
raise ValidationError(_('Barcode is already in use'))
|
raise ValidationError(_('Barcode is already in use'))
|
||||||
|
|
||||||
|
return barcode
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
fields = [
|
fields = [
|
||||||
'barcode',
|
'barcode',
|
||||||
@ -288,7 +292,7 @@ class POReceiveSerializer(serializers.Serializer):
|
|||||||
unique_barcodes = set()
|
unique_barcodes = set()
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
barcode = item.get('barcode', None)
|
barcode = item.get('barcode', '')
|
||||||
|
|
||||||
if barcode:
|
if barcode:
|
||||||
if barcode in unique_barcodes:
|
if barcode in unique_barcodes:
|
||||||
|
@ -332,6 +332,61 @@ class PurchaseOrderReceiveTest(OrderTest):
|
|||||||
# No new stock items have been created
|
# No new stock items have been created
|
||||||
self.assertEqual(self.n, StockItem.objects.count())
|
self.assertEqual(self.n, StockItem.objects.count())
|
||||||
|
|
||||||
|
def test_invalid_barcodes(self):
|
||||||
|
"""
|
||||||
|
Tests for checking in items with invalid barcodes:
|
||||||
|
|
||||||
|
- Cannot check in "duplicate" barcodes
|
||||||
|
- Barcodes cannot match UID field for existing StockItem
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Set stock item barcode
|
||||||
|
item = StockItem.objects.get(pk=1)
|
||||||
|
item.uid = 'MY-BARCODE-HASH'
|
||||||
|
item.save()
|
||||||
|
|
||||||
|
response = self.post(
|
||||||
|
self.url,
|
||||||
|
{
|
||||||
|
'items': [
|
||||||
|
{
|
||||||
|
'line_item': 1,
|
||||||
|
'quantity': 50,
|
||||||
|
'barcode': 'MY-BARCODE-HASH',
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'location': 1,
|
||||||
|
},
|
||||||
|
expected_code=400
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIn('Barcode is already in use', str(response.data))
|
||||||
|
|
||||||
|
response = self.post(
|
||||||
|
self.url,
|
||||||
|
{
|
||||||
|
'items': [
|
||||||
|
{
|
||||||
|
'line_item': 1,
|
||||||
|
'quantity': 5,
|
||||||
|
'barcode': 'MY-BARCODE-HASH-1',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'line_item': 1,
|
||||||
|
'quantity': 5,
|
||||||
|
'barcode': 'MY-BARCODE-HASH-1'
|
||||||
|
},
|
||||||
|
],
|
||||||
|
'location': 1,
|
||||||
|
},
|
||||||
|
expected_code=400
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIn('barcode values must be unique', str(response.data))
|
||||||
|
|
||||||
|
# No new stock items have been created
|
||||||
|
self.assertEqual(self.n, StockItem.objects.count())
|
||||||
|
|
||||||
def test_valid(self):
|
def test_valid(self):
|
||||||
"""
|
"""
|
||||||
Test receipt of valid data
|
Test receipt of valid data
|
||||||
@ -354,11 +409,13 @@ class PurchaseOrderReceiveTest(OrderTest):
|
|||||||
{
|
{
|
||||||
'line_item': 1,
|
'line_item': 1,
|
||||||
'quantity': 50,
|
'quantity': 50,
|
||||||
|
'barcode': 'MY-UNIQUE-BARCODE-123',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'line_item': 2,
|
'line_item': 2,
|
||||||
'quantity': 200,
|
'quantity': 200,
|
||||||
'location': 2, # Explicit location
|
'location': 2, # Explicit location
|
||||||
|
'barcode': 'MY-UNIQUE-BARCODE-456',
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
'location': 1, # Default location
|
'location': 1, # Default location
|
||||||
@ -386,6 +443,10 @@ class PurchaseOrderReceiveTest(OrderTest):
|
|||||||
self.assertEqual(stock_1.last().location.pk, 1)
|
self.assertEqual(stock_1.last().location.pk, 1)
|
||||||
self.assertEqual(stock_2.last().location.pk, 2)
|
self.assertEqual(stock_2.last().location.pk, 2)
|
||||||
|
|
||||||
|
# Barcodes should have been assigned to the stock items
|
||||||
|
self.assertTrue(StockItem.objects.filter(uid='MY-UNIQUE-BARCODE-123').exists())
|
||||||
|
self.assertTrue(StockItem.objects.filter(uid='MY-UNIQUE-BARCODE-456').exists())
|
||||||
|
|
||||||
|
|
||||||
class SalesOrderTest(OrderTest):
|
class SalesOrderTest(OrderTest):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user