Unit testing for new feature

This commit is contained in:
Oliver Walters 2021-02-19 15:50:32 +11:00
parent beeb94785d
commit 6037f1452a
2 changed files with 64 additions and 5 deletions

View File

@ -1311,6 +1311,9 @@ class StockItem(MPTTModel):
as all named tests are accessible. 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 # Filter results by "date", so that newer results
# will override older ones. # will override older ones.
results = self.getTestResults(**kwargs).order_by('date') results = self.getTestResults(**kwargs).order_by('date')
@ -1321,17 +1324,14 @@ class StockItem(MPTTModel):
key = helpers.generateTestKey(result.test) key = helpers.generateTestKey(result.test)
result_map[key] = result 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? # Do we wish to "cascade" and include test results from installed stock items?
cascade = kwargs.get('cascade', False) cascade = kwargs.get('cascade', False)
if include_installed: if include_installed:
installed_items = get_installed_items(cascade=cascade) installed_items = self.get_installed_items(cascade=cascade)
for item in installed_items: for item in installed_items:
item_results = item.testResultMap item_results = item.testResultMap()
for key in item_results.keys(): for key in item_results.keys():
# Results from sub items should not override master ones # Results from sub items should not override master ones

View File

@ -622,3 +622,62 @@ class TestResultTest(StockTest):
item3 = StockItem.objects.get(serial=100, part=item2.part) item3 = StockItem.objects.get(serial=100, part=item2.part)
self.assertEqual(item3.test_results.count(), 4) 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)