From c6590066b865416e5761718e2a61dca06ad44e81 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Wed, 18 May 2022 22:46:15 +1000 Subject: [PATCH] Add tests for successful location - Sample plugin now updates metadata tag --- InvenTree/plugin/base/locate/api.py | 1 - InvenTree/plugin/base/locate/test_locate.py | 63 ++++++++++++++++++- .../plugin/samples/locate/locate_sample.py | 24 ++++++- 3 files changed, 83 insertions(+), 5 deletions(-) diff --git a/InvenTree/plugin/base/locate/api.py b/InvenTree/plugin/base/locate/api.py index a7effe91b3..3004abb262 100644 --- a/InvenTree/plugin/base/locate/api.py +++ b/InvenTree/plugin/base/locate/api.py @@ -74,4 +74,3 @@ class LocatePluginView(APIView): else: raise ParseError("Must supply either 'item' or 'location' parameter") - diff --git a/InvenTree/plugin/base/locate/test_locate.py b/InvenTree/plugin/base/locate/test_locate.py index 26fafcca49..e145c2360b 100644 --- a/InvenTree/plugin/base/locate/test_locate.py +++ b/InvenTree/plugin/base/locate/test_locate.py @@ -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)) \ No newline at end of file + 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']) diff --git a/InvenTree/plugin/samples/locate/locate_sample.py b/InvenTree/plugin/samples/locate/locate_sample.py index 458b84cfa5..32a2dd713c 100644 --- a/InvenTree/plugin/samples/locate/locate_sample.py +++ b/InvenTree/plugin/samples/locate/locate_sample.py @@ -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!")