mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
add tests
This commit is contained in:
parent
832130f6cd
commit
05c8db8c8e
@ -789,33 +789,6 @@ class TestIncrement(TestCase):
|
||||
self.assertEqual(result, b)
|
||||
|
||||
|
||||
class TestMakeBarcode(TestCase):
|
||||
"""Tests for barcode string creation."""
|
||||
|
||||
def test_barcode_extended(self):
|
||||
"""Test creation of barcode with extended data."""
|
||||
bc = helpers.MakeBarcode(
|
||||
'part', 3, {'id': 3, 'url': 'www.google.com'}, brief=False
|
||||
)
|
||||
|
||||
self.assertIn('part', bc)
|
||||
self.assertIn('tool', bc)
|
||||
self.assertIn('"tool": "InvenTree"', bc)
|
||||
|
||||
data = json.loads(bc)
|
||||
|
||||
self.assertEqual(data['part']['id'], 3)
|
||||
self.assertEqual(data['part']['url'], 'www.google.com')
|
||||
|
||||
def test_barcode_brief(self):
|
||||
"""Test creation of simple barcode."""
|
||||
bc = helpers.MakeBarcode('stockitem', 7)
|
||||
|
||||
data = json.loads(bc)
|
||||
self.assertEqual(len(data), 1)
|
||||
self.assertEqual(data['stockitem'], 7)
|
||||
|
||||
|
||||
class TestDownloadFile(TestCase):
|
||||
"""Tests for DownloadFile."""
|
||||
|
||||
|
@ -169,7 +169,7 @@ class PartTest(TestCase):
|
||||
self.assertEqual(Part.barcode_model_type(), 'part')
|
||||
|
||||
p = Part.objects.get(pk=1)
|
||||
barcode = p.format_barcode(brief=True)
|
||||
barcode = p.format_barcode()
|
||||
self.assertEqual(barcode, '{"part": 1}')
|
||||
|
||||
def test_tree(self):
|
||||
@ -270,9 +270,8 @@ class PartTest(TestCase):
|
||||
|
||||
def test_barcode(self):
|
||||
"""Test barcode format functionality."""
|
||||
barcode = self.r1.format_barcode(brief=False)
|
||||
self.assertIn('InvenTree', barcode)
|
||||
self.assertIn('"part": {"id": 3}', barcode)
|
||||
barcode = self.r1.format_barcode()
|
||||
self.assertEqual('{"part": 3}', barcode)
|
||||
|
||||
def test_sell_pricing(self):
|
||||
"""Check that the sell pricebreaks were loaded."""
|
||||
|
@ -21,6 +21,7 @@ class BarcodeAPITest(InvenTreeAPITestCase):
|
||||
super().setUp()
|
||||
|
||||
self.scan_url = reverse('api-barcode-scan')
|
||||
self.generate_url = reverse('api-barcode-generate')
|
||||
self.assign_url = reverse('api-barcode-link')
|
||||
self.unassign_url = reverse('api-barcode-unlink')
|
||||
|
||||
@ -30,6 +31,14 @@ class BarcodeAPITest(InvenTreeAPITestCase):
|
||||
url, data={'barcode': str(barcode)}, expected_code=expected_code
|
||||
)
|
||||
|
||||
def generateBarcode(self, model: str, pk: int, expected_code: int):
|
||||
"""Post barcode generation and return barcode contents."""
|
||||
return self.post(
|
||||
self.generate_url,
|
||||
data={'model': model, 'pk': pk},
|
||||
expected_code=expected_code,
|
||||
)
|
||||
|
||||
def test_invalid(self):
|
||||
"""Test that invalid requests fail."""
|
||||
# test scan url
|
||||
@ -130,7 +139,7 @@ class BarcodeAPITest(InvenTreeAPITestCase):
|
||||
data = response.data
|
||||
self.assertIn('error', data)
|
||||
|
||||
def test_barcode_generation(self):
|
||||
def test_barcode_scan(self):
|
||||
"""Test that a barcode is generated with a scan."""
|
||||
item = StockItem.objects.get(pk=522)
|
||||
|
||||
@ -145,6 +154,18 @@ class BarcodeAPITest(InvenTreeAPITestCase):
|
||||
|
||||
self.assertEqual(pk, item.pk)
|
||||
|
||||
def test_barcode_generation(self):
|
||||
"""Test that a barcode can be generated for a StockItem."""
|
||||
item = StockItem.objects.get(pk=522)
|
||||
|
||||
data = self.generateBarcode('stockitem', item.pk, expected_code=200).data
|
||||
self.assertEqual(data['barcode'], '{"stockitem": 522}')
|
||||
|
||||
def test_barcode_generation_invalid(self):
|
||||
"""Test barcode generation for invalid model/pk."""
|
||||
self.generateBarcode('invalidmodel', 1, expected_code=400)
|
||||
self.generateBarcode('stockitem', 99999999, expected_code=400)
|
||||
|
||||
def test_association(self):
|
||||
"""Test that a barcode can be associated with a StockItem."""
|
||||
item = StockItem.objects.get(pk=522)
|
||||
|
@ -52,6 +52,20 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
|
||||
reverse('api-barcode-scan'), data=data, expected_code=expected_code
|
||||
)
|
||||
|
||||
def generate(self, model: str, pk: int, expected_code: int):
|
||||
"""Generate a barcode for a given model instance."""
|
||||
return self.post(
|
||||
reverse('api-barcode-generate', kwargs={'model': model, 'pk': pk}),
|
||||
expected_code=expected_code,
|
||||
)
|
||||
|
||||
def set_plugin_setting(self, key: str, value: str):
|
||||
"""Set the internal barcode format for the plugin."""
|
||||
from plugin import registry
|
||||
|
||||
plugin = registry.get_plugin('inventreebarcode')
|
||||
plugin.set_setting(key, value)
|
||||
|
||||
def test_unassign_errors(self):
|
||||
"""Test various error conditions for the barcode unassign endpoint."""
|
||||
# Fail without any fields provided
|
||||
@ -248,8 +262,8 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
|
||||
self.assertIn('success', response.data)
|
||||
self.assertEqual(response.data['stockitem']['pk'], 1)
|
||||
|
||||
def test_scan_inventree(self):
|
||||
"""Test scanning of first-party barcodes."""
|
||||
def test_scan_inventree_json(self):
|
||||
"""Test scanning of first-party json barcodes."""
|
||||
# Scan a StockItem object (which does not exist)
|
||||
response = self.scan({'barcode': '{"stockitem": 5}'}, expected_code=400)
|
||||
|
||||
@ -290,3 +304,72 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
|
||||
self.assertIn('success', response.data)
|
||||
self.assertIn('barcode_data', response.data)
|
||||
self.assertIn('barcode_hash', response.data)
|
||||
|
||||
def test_scan_inventree_short(self):
|
||||
"""Test scanning of first-party short barcodes."""
|
||||
# Scan a StockItem object (which does not exist)
|
||||
response = self.scan({'barcode': 'INV-SI5'}, expected_code=400)
|
||||
|
||||
self.assertIn('No match found for barcode data', str(response.data))
|
||||
|
||||
# Scan a StockItem object (which does exist)
|
||||
response = self.scan({'barcode': 'INV-SI1'}, expected_code=200)
|
||||
|
||||
self.assertIn('success', response.data)
|
||||
self.assertIn('stockitem', response.data)
|
||||
self.assertEqual(response.data['stockitem']['pk'], 1)
|
||||
|
||||
# Scan a StockLocation object
|
||||
response = self.scan({'barcode': 'INV-SL5'}, expected_code=200)
|
||||
|
||||
self.assertIn('success', response.data)
|
||||
self.assertEqual(response.data['stocklocation']['pk'], 5)
|
||||
self.assertEqual(
|
||||
response.data['stocklocation']['api_url'], '/api/stock/location/5/'
|
||||
)
|
||||
if settings.ENABLE_CLASSIC_FRONTEND:
|
||||
self.assertEqual(
|
||||
response.data['stocklocation']['web_url'], '/stock/location/5/'
|
||||
)
|
||||
self.assertEqual(response.data['plugin'], 'InvenTreeBarcode')
|
||||
|
||||
# Scan a Part object
|
||||
response = self.scan({'barcode': 'INV-PA5'}, expected_code=200)
|
||||
|
||||
self.assertEqual(response.data['part']['pk'], 5)
|
||||
|
||||
# Scan a SupplierPart instance with custom prefix
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'TEST')
|
||||
response = self.scan({'barcode': 'TESTSP1'}, expected_code=200)
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-')
|
||||
|
||||
self.assertEqual(response.data['supplierpart']['pk'], 1)
|
||||
self.assertEqual(response.data['plugin'], 'InvenTreeBarcode')
|
||||
|
||||
self.assertIn('success', response.data)
|
||||
self.assertIn('barcode_data', response.data)
|
||||
self.assertIn('barcode_hash', response.data)
|
||||
|
||||
def test_generation_inventree_json(self):
|
||||
"""Test JSON barcode generation."""
|
||||
item = stock.models.StockLocation.objects.get(pk=522)
|
||||
data = self.generate('stocklocation', item.pk, expected_code=200).data
|
||||
self.assertEqual(data['barcode'], '{"location": 522}')
|
||||
|
||||
def test_generation_inventree_short(self):
|
||||
"""Test short barcode generation."""
|
||||
self.set_plugin_setting('INTERNAL_BARCODE_FORMAT', 'short')
|
||||
|
||||
item = stock.models.StockLocation.objects.get(pk=522)
|
||||
|
||||
# test with default prefix
|
||||
data = self.generate('stocklocation', item.pk, expected_code=200).data
|
||||
self.assertEqual(data['barcode'], 'INV-SL522')
|
||||
|
||||
# test with custom prefix
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'TEST')
|
||||
data = self.generate('stocklocation', item.pk, expected_code=200).data
|
||||
self.assertEqual(data['barcode'], 'TESTSL522')
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-')
|
||||
|
||||
self.set_internal_barcode_format('INTERNAL_BARCODE_FORMAT', 'json')
|
||||
|
@ -952,12 +952,6 @@ class StockBarcodeTest(StockTestBase):
|
||||
|
||||
self.assertEqual(StockItem.barcode_model_type(), 'stockitem')
|
||||
|
||||
# Call format_barcode method
|
||||
barcode = item.format_barcode(brief=False)
|
||||
|
||||
for key in ['tool', 'version', 'instance', 'stockitem']:
|
||||
self.assertIn(key, barcode)
|
||||
|
||||
# Render simple barcode data for the StockItem
|
||||
barcode = item.barcode
|
||||
self.assertEqual(barcode, '{"stockitem": 1}')
|
||||
@ -968,7 +962,7 @@ class StockBarcodeTest(StockTestBase):
|
||||
|
||||
loc = StockLocation.objects.get(pk=1)
|
||||
|
||||
barcode = loc.format_barcode(brief=True)
|
||||
barcode = loc.format_barcode()
|
||||
self.assertEqual('{"stocklocation": 1}', barcode)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user