From 6037f1452acfd5a63acf475cfe9d4761959433ee Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Fri, 19 Feb 2021 15:50:32 +1100 Subject: [PATCH] Unit testing for new feature --- InvenTree/stock/models.py | 10 +++---- InvenTree/stock/tests.py | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 2cfaf2ce68..4573036732 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -1311,6 +1311,9 @@ class StockItem(MPTTModel): as all named tests are accessible. """ + # Do we wish to include test results from installed items? + include_installed = kwargs.pop('include_installed', False) + # Filter results by "date", so that newer results # will override older ones. results = self.getTestResults(**kwargs).order_by('date') @@ -1321,17 +1324,14 @@ class StockItem(MPTTModel): key = helpers.generateTestKey(result.test) result_map[key] = result - # Do we wish to include test results from installed items? - include_installed = kwargs.get('include_installed', False) - # Do we wish to "cascade" and include test results from installed stock items? cascade = kwargs.get('cascade', False) if include_installed: - installed_items = get_installed_items(cascade=cascade) + installed_items = self.get_installed_items(cascade=cascade) for item in installed_items: - item_results = item.testResultMap + item_results = item.testResultMap() for key in item_results.keys(): # Results from sub items should not override master ones diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index b54411b0d2..f3ca949bbf 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -622,3 +622,62 @@ class TestResultTest(StockTest): item3 = StockItem.objects.get(serial=100, part=item2.part) self.assertEqual(item3.test_results.count(), 4) + + def test_installed_tests(self): + """ + Test test results for stock in stock. + + Or, test "test results" for "stock items" installed "inside" a "stock item" + """ + + # Get a "master" stock item + item = StockItem.objects.get(pk=105) + + tests = item.testResultMap(include_installed=False) + self.assertEqual(len(tests), 3) + + # There are no "sub items" intalled at this stage + tests = item.testResultMap(include_installed=False) + self.assertEqual(len(tests), 3) + + # Create a stock item which is installed *inside* the master item + sub_item = StockItem.objects.create( + part=item.part, + quantity=1, + belongs_to=item, + location=None + ) + + # Now, create some test results against the sub item + + # First test is overshadowed by the same test for the parent part + StockItemTestResult.objects.create( + stock_item=sub_item, + test='firmware version', + date=datetime.datetime.now().date(), + result=True + ) + + # Should return the same number of tests as before + tests = item.testResultMap(include_installed=True) + self.assertEqual(len(tests), 3) + + # Now, add a *unique* test result for the sub item + StockItemTestResult.objects.create( + stock_item=sub_item, + test='some new test', + date=datetime.datetime.now().date(), + result=False, + value='abcde', + ) + + tests = item.testResultMap(include_installed=True) + self.assertEqual(len(tests), 4) + + self.assertIn('somenewtest', tests) + self.assertEqual(sub_item.test_results.count(), 2) + + # Check that asking for test result map for *top item only* still works + tests = item.testResultMap(include_installed=False) + self.assertEqual(len(tests), 3) + self.assertNotIn('somenewtest', tests)