mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-07 07:48:50 +00:00
Add unit testing framework for barcodes
This commit is contained in:
parent
22a8e82108
commit
0559946791
@ -79,30 +79,3 @@ class APITests(APITestCase):
|
|||||||
self.assertIn('instance', data)
|
self.assertIn('instance', data)
|
||||||
|
|
||||||
self.assertEquals('InvenTree', data['server'])
|
self.assertEquals('InvenTree', data['server'])
|
||||||
|
|
||||||
def test_barcode_fail(self):
|
|
||||||
# Test barcode endpoint without auth
|
|
||||||
response = self.client.post(reverse('api-barcode-plugin'), format='json')
|
|
||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
|
|
||||||
|
|
||||||
def test_barcode(self):
|
|
||||||
""" Test the barcode endpoint """
|
|
||||||
|
|
||||||
self.tokenAuth()
|
|
||||||
|
|
||||||
url = reverse('api-barcode-plugin')
|
|
||||||
|
|
||||||
data = {
|
|
||||||
'barcode': {
|
|
||||||
'asdlaksdfalsdkfsa;fdlkasd;'
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
response = self.client.post(url, format='json', data=data)
|
|
||||||
|
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
|
||||||
|
|
||||||
self.assertIn('error', response.data)
|
|
||||||
self.assertIn('barcode_data', response.data)
|
|
||||||
self.assertEqual(response.data['error'], 'Unknown barcode format')
|
|
||||||
|
@ -80,6 +80,9 @@ class BarcodeScan(APIView):
|
|||||||
# Try to associate with a stock item
|
# Try to associate with a stock item
|
||||||
item = plugin.getStockItem()
|
item = plugin.getStockItem()
|
||||||
|
|
||||||
|
if item is None:
|
||||||
|
item = plugin.getStockItemByHash()
|
||||||
|
|
||||||
if item is not None:
|
if item is not None:
|
||||||
response['stockitem'] = plugin.renderStockItem(item)
|
response['stockitem'] = plugin.renderStockItem(item)
|
||||||
match_found = True
|
match_found = True
|
||||||
@ -194,13 +197,11 @@ class BarcodeAssign(APIView):
|
|||||||
response['error'] = 'Barcode already matches Part object'
|
response['error'] = 'Barcode already matches Part object'
|
||||||
|
|
||||||
if not match_found:
|
if not match_found:
|
||||||
# Try to associate by hash
|
item = plugin.getStockItemByHash()
|
||||||
try:
|
|
||||||
item = StockItem.objects.get(uid=hash)
|
if item is not None:
|
||||||
response['error'] = 'Barcode hash already matches StockItem object'
|
response['error'] = 'Barcode hash already matches StockItem object'
|
||||||
match_found = True
|
match_found = True
|
||||||
except StockItem.DoesNotExist:
|
|
||||||
pass
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
hash = hash_barcode(barcode_data)
|
hash = hash_barcode(barcode_data)
|
||||||
@ -235,5 +236,5 @@ barcode_api_urls = [
|
|||||||
url(r'^assign/$', BarcodeAssign.as_view(), name='api-barcode-assign'),
|
url(r'^assign/$', BarcodeAssign.as_view(), name='api-barcode-assign'),
|
||||||
|
|
||||||
# Catch-all performs barcode 'scan'
|
# Catch-all performs barcode 'scan'
|
||||||
url(r'^.*$', BarcodeScan.as_view(), name='api-barcode-plugin'),
|
url(r'^.*$', BarcodeScan.as_view(), name='api-barcode-scan'),
|
||||||
]
|
]
|
@ -50,6 +50,18 @@ class BarcodePlugin:
|
|||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def getStockItemByHash(self):
|
||||||
|
"""
|
||||||
|
Attempt to retrieve a StockItem associated with this barcode,
|
||||||
|
based on the barcode hash.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
item = StockItem.objects.get(uid=self.hash())
|
||||||
|
return item
|
||||||
|
except StockItem.DoesNotExist:
|
||||||
|
return None
|
||||||
|
|
||||||
def renderStockItem(self, item):
|
def renderStockItem(self, item):
|
||||||
"""
|
"""
|
||||||
Render a stock item to JSON response
|
Render a stock item to JSON response
|
||||||
|
118
InvenTree/barcode/tests.py
Normal file
118
InvenTree/barcode/tests.py
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
"""
|
||||||
|
Unit tests for Barcode endpoints
|
||||||
|
"""
|
||||||
|
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
from rest_framework.test import APITestCase
|
||||||
|
from rest_framework import status
|
||||||
|
|
||||||
|
from stock.models import StockItem
|
||||||
|
|
||||||
|
|
||||||
|
class BarcodeAPITest(APITestCase):
|
||||||
|
|
||||||
|
fixtures = [
|
||||||
|
'category',
|
||||||
|
'part',
|
||||||
|
'location',
|
||||||
|
'stock'
|
||||||
|
]
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
# Create a user for auth
|
||||||
|
User = get_user_model()
|
||||||
|
User.objects.create_user('testuser', 'test@testing.com', 'password')
|
||||||
|
|
||||||
|
self.client.login(username='testuser', password='password')
|
||||||
|
|
||||||
|
self.scan_url = reverse('api-barcode-scan')
|
||||||
|
self.assign_url = reverse('api-barcode-assign')
|
||||||
|
|
||||||
|
def postBarcode(self, url, barcode):
|
||||||
|
|
||||||
|
return self.client.post(url, format='json', data={'barcode': str(barcode)})
|
||||||
|
|
||||||
|
def test_invalid(self):
|
||||||
|
|
||||||
|
response = self.client.post(self.scan_url, format='json', data={})
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
def test_empty(self):
|
||||||
|
|
||||||
|
response = self.postBarcode(self.scan_url, '')
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
data = response.data
|
||||||
|
self.assertIn('error', data)
|
||||||
|
|
||||||
|
self.assertIn('barcode_data', data)
|
||||||
|
self.assertIn('hash', data)
|
||||||
|
self.assertIn('plugin', data)
|
||||||
|
self.assertIsNone(data['plugin'])
|
||||||
|
|
||||||
|
def test_barcode_generation(self):
|
||||||
|
|
||||||
|
item = StockItem.objects.get(pk=522)
|
||||||
|
|
||||||
|
response = self.postBarcode(self.scan_url, item.format_barcode())
|
||||||
|
data = response.data
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
self.assertIn('stockitem', data)
|
||||||
|
|
||||||
|
pk = data['stockitem']['pk']
|
||||||
|
|
||||||
|
self.assertEqual(pk, item.pk)
|
||||||
|
|
||||||
|
def test_association(self):
|
||||||
|
"""
|
||||||
|
Test that a barcode can be associated with a StockItem
|
||||||
|
"""
|
||||||
|
|
||||||
|
item = StockItem.objects.get(pk=522)
|
||||||
|
|
||||||
|
self.assertEqual(len(item.uid), 0)
|
||||||
|
|
||||||
|
barcode_data = 'A-TEST-BARCODE-STRING'
|
||||||
|
|
||||||
|
response = self.client.post(
|
||||||
|
self.assign_url, format='json',
|
||||||
|
data = {
|
||||||
|
'barcode': barcode_data,
|
||||||
|
'stockitem': item.pk
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
data = response.data
|
||||||
|
|
||||||
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
self.assertIn('success', data)
|
||||||
|
|
||||||
|
hash = data['hash']
|
||||||
|
|
||||||
|
# Read the item out from the database again
|
||||||
|
item = StockItem.objects.get(pk=522)
|
||||||
|
|
||||||
|
self.assertEqual(hash, item.uid)
|
||||||
|
|
||||||
|
# Ensure that the same UID cannot be assigned to a different stock item!
|
||||||
|
response = self.client.post(
|
||||||
|
self.assign_url, format='json',
|
||||||
|
data = {
|
||||||
|
'barcode': barcode_data,
|
||||||
|
'stockitem': 521
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
data = response.data
|
||||||
|
|
||||||
|
self.assertIn('error', data)
|
||||||
|
self.assertNotIn('success', data)
|
4
Makefile
4
Makefile
@ -51,12 +51,12 @@ style:
|
|||||||
# Run unit tests
|
# Run unit tests
|
||||||
test:
|
test:
|
||||||
cd InvenTree && python3 manage.py check
|
cd InvenTree && python3 manage.py check
|
||||||
cd InvenTree && python3 manage.py test build common company order part report stock InvenTree
|
cd InvenTree && python3 manage.py test barcode build common company order part report stock InvenTree
|
||||||
|
|
||||||
# Run code coverage
|
# Run code coverage
|
||||||
coverage:
|
coverage:
|
||||||
cd InvenTree && python3 manage.py check
|
cd InvenTree && python3 manage.py check
|
||||||
coverage run InvenTree/manage.py test build common company order part report stock InvenTree
|
coverage run InvenTree/manage.py test barcode build common company order part report stock InvenTree
|
||||||
coverage html
|
coverage html
|
||||||
|
|
||||||
# Install packages required to generate code docs
|
# Install packages required to generate code docs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user