mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add functions to test if a stock item has passed all tests
This commit is contained in:
parent
5f318799c1
commit
6cb017bbfd
@ -1010,6 +1010,10 @@ class Part(MPTTModel):
|
||||
|
||||
return tests
|
||||
|
||||
def getRequiredTests(self):
|
||||
# Return the tests which are required by this part
|
||||
return self.getTestTemplates(required=True)
|
||||
|
||||
@property
|
||||
def attachment_count(self):
|
||||
""" Count the number of attachments for this part.
|
||||
|
@ -29,3 +29,40 @@
|
||||
result: True
|
||||
date: 2020-05-17
|
||||
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
|
@ -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):
|
||||
|
@ -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())
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user