Add functions to test if a stock item has passed all tests

This commit is contained in:
Oliver Walters 2020-05-17 22:03:55 +10:00
parent 5f318799c1
commit 6cb017bbfd
4 changed files with 117 additions and 1 deletions

View File

@ -1010,6 +1010,10 @@ class Part(MPTTModel):
return tests return tests
def getRequiredTests(self):
# Return the tests which are required by this part
return self.getTestTemplates(required=True)
@property @property
def attachment_count(self): def attachment_count(self):
""" Count the number of attachments for this part. """ Count the number of attachments for this part.

View File

@ -29,3 +29,40 @@
result: True result: True
date: 2020-05-17 date: 2020-05-17
notes: 'Passed temperature test by making it cooler' 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

View File

@ -967,6 +967,49 @@ class StockItem(MPTTModel):
return result_map 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') @receiver(pre_delete, sender=StockItem, dispatch_uid='stock_item_pre_delete_log')
def before_delete_stock_item(sender, instance, using, **kwargs): def before_delete_stock_item(sender, instance, using, **kwargs):

View File

@ -4,6 +4,7 @@ from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from .models import StockLocation, StockItem, StockItemTracking from .models import StockLocation, StockItem, StockItemTracking
from .models import StockItemTestResult
from part.models import Part from part.models import Part
@ -15,6 +16,7 @@ class StockTest(TestCase):
fixtures = [ fixtures = [
'category', 'category',
'part', 'part',
'test_templates',
'location', 'location',
'stock', 'stock',
'stock_tests', 'stock_tests',
@ -432,3 +434,33 @@ class TestResultTest(StockTest):
# Keys are all lower-case and do not contain spaces # Keys are all lower-case and do not contain spaces
for test in ['firmwareversion', 'settingschecksum', 'temperaturetest']: for test in ['firmwareversion', 'settingschecksum', 'temperaturetest']:
self.assertIn(test, result_map.keys()) 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())