diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 81b8206469..d37d666be4 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -1009,6 +1009,10 @@ class Part(MPTTModel): tests = tests.filter(required=required) return tests + + def getRequiredTests(self): + # Return the tests which are required by this part + return self.getTestTemplates(required=True) @property def attachment_count(self): diff --git a/InvenTree/stock/fixtures/stock_tests.yaml b/InvenTree/stock/fixtures/stock_tests.yaml index 9a82b6ada7..8207ccaa8c 100644 --- a/InvenTree/stock/fixtures/stock_tests.yaml +++ b/InvenTree/stock/fixtures/stock_tests.yaml @@ -28,4 +28,41 @@ test: "Temperature Test" result: True date: 2020-05-17 - notes: 'Passed temperature test by making it cooler' \ No newline at end of file + notes: 'Passed temperature test by making it cooler' + +- model: stock.stockitemtestresult + fields: + stock_item: 522 + test: 'applypaint' + result: True + date: 2020-05-17 + +- model: stock.stockitemtestresult + fields: + stock_item: 522 + test: 'applypaint' + result: False + date: 2020-05-18 + +- model: stock.stockitemtestresult + fields: + stock_item: 522 + test: 'Attach Legs' + result: True + date: 2020-05-17 + +- model: stock.stockitemtestresult + fields: + stock_item: 522 + test: 'Check that chair is GreEn ' + result: True + date: 2020-05-17 + +- model: stock.stockitemtestresult + pk: 12345 + fields: + stock_item: 522 + test: 'test strength of chair' + result: False + value: 100kg + date: 2020-05-17 \ No newline at end of file diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 851c80a9ed..4df61b0ee3 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -967,6 +967,49 @@ class StockItem(MPTTModel): return result_map + def requiredTestStatus(self): + """ + Return the status of the tests required for this StockItem. + + return: + A dict containing the following items: + - total: Number of required tests + - passed: Number of tests that have passed + - failed: Number of tests that have failed + """ + + # All the tests required by the part object + required = self.part.getRequiredTests() + + results = self.testResultMap() + + total = len(required) + passed = 0 + failed = 0 + + for test in required: + key = helpers.generateTestKey(test.test_name) + + if key in results: + result = results[key] + + if result.result: + passed += 1 + else: + failed += 1 + + return { + 'total': total, + 'passed': passed, + 'failed': failed, + } + + def passedAllRequiredTests(self): + + status = self.requiredTestStatus() + + return status['passed'] >= status['total'] + @receiver(pre_delete, sender=StockItem, dispatch_uid='stock_item_pre_delete_log') def before_delete_stock_item(sender, instance, using, **kwargs): diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index cd927e0251..da46fee064 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -4,6 +4,7 @@ from django.contrib.auth import get_user_model from django.core.exceptions import ValidationError from .models import StockLocation, StockItem, StockItemTracking +from .models import StockItemTestResult from part.models import Part @@ -15,6 +16,7 @@ class StockTest(TestCase): fixtures = [ 'category', 'part', + 'test_templates', 'location', 'stock', 'stock_tests', @@ -432,3 +434,33 @@ class TestResultTest(StockTest): # Keys are all lower-case and do not contain spaces for test in ['firmwareversion', 'settingschecksum', 'temperaturetest']: self.assertIn(test, result_map.keys()) + + def test_test_results(self): + item = StockItem.objects.get(pk=522) + + status = item.requiredTestStatus() + + self.assertEqual(status['total'], 5) + self.assertEqual(status['passed'], 3) + self.assertEqual(status['failed'], 1) + + self.assertFalse(item.passedAllRequiredTests()) + + # Add some new test results to make it pass! + test = StockItemTestResult.objects.get(pk=12345) + test.result = True + test.save() + + StockItemTestResult.objects.create( + stock_item=item, + test='sew cushion', + result=True + ) + + results = item.testResultMap() + + for key in results.keys(): + result = results[key] + + self.assertTrue(item.passedAllRequiredTests()) +