Add tests for successful location

- Sample plugin now updates metadata tag
This commit is contained in:
Oliver Walters 2022-05-18 22:46:15 +10:00
parent dd476ce796
commit c6590066b8
3 changed files with 83 additions and 5 deletions

View File

@ -74,4 +74,3 @@ class LocatePluginView(APIView):
else:
raise ParseError("Must supply either 'item' or 'location' parameter")

View File

@ -7,6 +7,7 @@ from django.urls import reverse
from InvenTree.api_tester import InvenTreeAPITestCase
from plugin.registry import registry
from stock.models import StockItem, StockLocation
class LocatePluginTests(InvenTreeAPITestCase):
@ -29,7 +30,7 @@ class LocatePluginTests(InvenTreeAPITestCase):
def test_locate_fail(self):
"""Test various API failure modes"""
url = reverse('api-locate-plugin')
# Post without a plugin
@ -86,4 +87,62 @@ class LocatePluginTests(InvenTreeAPITestCase):
expected_code=404,
)
self.assertIn(f"StockLocation matching PK '{pk}' not found", str(response.data))
self.assertIn(f"StockLocation matching PK '{pk}' not found", str(response.data))
def test_locate_item(self):
"""
Test that the plugin correctly 'locates' a StockItem
As the background worker is not running during unit testing,
the sample 'locate' function will be called 'inline'
"""
url = reverse('api-locate-plugin')
item = StockItem.objects.get(pk=1)
# The sample plugin will set the 'located' metadata tag
item.set_metadata('located', False)
response = self.post(
url,
{
'plugin': 'samplelocate',
'item': 1,
},
expected_code=200
)
self.assertEqual(response.data['item'], 1)
item.refresh_from_db()
# Item metadata should have been altered!
self.assertTrue(item.metadata['located'])
def test_locate_location(self):
"""
Test that the plugin correctly 'locates' a StockLocation
"""
url = reverse('api-locate-plugin')
for location in StockLocation.objects.all():
location.set_metadata('located', False)
response = self.post(
url,
{
'plugin': 'samplelocate',
'location': location.pk,
},
expected_code=200
)
self.assertEqual(response.data['location'], location.pk)
location.refresh_from_db()
# Item metadata should have been altered!
self.assertTrue(location.metadata['located'])

View File

@ -23,7 +23,23 @@ class SampleLocatePlugin(LocateMixin, InvenTreePlugin):
SLUG = "samplelocate"
TITLE = "Sample plugin for locating items"
VERSION = "0.1"
VERSION = "0.2"
def locate_stock_item(self, item_pk):
from stock.models import StockItem
logger.info(f"SampleLocatePlugin attempting to locate item ID {item_pk}")
try:
item = StockItem.objects.get(pk=item_pk)
logger.info(f"StockItem {item_pk} located!")
# Tag metadata
item.set_metadata('located', True)
except (ValueError, StockItem.DoesNotExist):
logger.error(f"StockItem ID {item_pk} does not exist!")
def locate_stock_location(self, location_pk):
@ -34,5 +50,9 @@ class SampleLocatePlugin(LocateMixin, InvenTreePlugin):
try:
location = StockLocation.objects.get(pk=location_pk)
logger.info(f"Location exists at '{location.pathstring}'")
except StockLocation.DoesNotExist:
# Tag metadata
location.set_metadata('located', True)
except (ValueError, StockLocation.DoesNotExist):
logger.error(f"Location ID {location_pk} does not exist!")