Add some unit testing for the new model

This commit is contained in:
Oliver Walters 2020-05-16 20:27:01 +10:00
parent f39f5e5825
commit 2bb9fd9955
5 changed files with 110 additions and 5 deletions

View File

@ -70,6 +70,18 @@
lft: 0 lft: 0
rght: 0 rght: 0
- model: stock.stockitem
pk: 105
fields:
part: 25
location: 7
quantity: 1
serial: 1000
level: 0
tree_id: 0
lft: 0
rght: 0
# Stock items for template / variant parts # Stock items for template / variant parts
- model: stock.stockitem - model: stock.stockitem
pk: 500 pk: 500

View File

@ -0,0 +1,31 @@
- model: stock.stockitemtestresult
fields:
stock_item: 105
test: "Firmware Version"
value: "0xA1B2C3D4"
result: True
date: 2020-02-02
- model: stock.stockitemtestresult
fields:
stock_item: 105
test: "Settings Checksum"
value: "0xAABBCCDD"
result: True
date: 2020-02-02
- model: stock.stockitemtestresult
fields:
stock_item: 105
test: "Temperature Test"
result: False
date: 2020-05-16
notes: 'Got too hot or something'
- model: stock.stockitemtestresult
fields:
stock_item: 105
test: "Temperature Test"
result: True
date: 2020-05-17
notes: 'Passed temperature test by making it cooler'

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.5 on 2020-05-16 10:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('stock', '0040_stockitemtestresult'),
]
operations = [
migrations.AddField(
model_name='stockitemtestresult',
name='notes',
field=models.CharField(blank=True, help_text='Test notes', max_length=500, verbose_name='Notes'),
),
]

View File

@ -921,6 +921,24 @@ class StockItem(MPTTModel):
return s return s
def getTestResults(self, test=None, result=None, user=None):
results = self.test_results
if test:
# Filter by test name
results = results.filter(test=test)
if result is not None:
# Filter by test status
results = results.filter(result=result)
if user:
# Filter by user
results = results.filter(user=user)
return results
@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):
@ -1009,6 +1027,7 @@ class StockItemTestResult(models.Model):
result: Test result value (pass / fail / etc) result: Test result value (pass / fail / etc)
value: Recorded test output value (optional) value: Recorded test output value (optional)
attachment: Link to StockItem attachment (optional) attachment: Link to StockItem attachment (optional)
notes: Extra user notes related to the test (optional)
user: User who uploaded the test result user: User who uploaded the test result
date: Date the test result was recorded date: Date the test result was recorded
""" """
@ -1059,6 +1078,12 @@ class StockItemTestResult(models.Model):
help_text=_('Test result attachment'), help_text=_('Test result attachment'),
) )
notes = models.CharField(
blank=True, max_length=500,
verbose_name=_('Notes'),
help_text=_("Test notes"),
)
user = models.ForeignKey( user = models.ForeignKey(
User, User,
on_delete = models.SET_NULL, on_delete = models.SET_NULL,

View File

@ -17,6 +17,7 @@ class StockTest(TestCase):
'part', 'part',
'location', 'location',
'stock', 'stock',
'stock_tests',
] ]
def setUp(self): def setUp(self):
@ -95,8 +96,8 @@ class StockTest(TestCase):
self.assertFalse(self.drawer2.has_items()) self.assertFalse(self.drawer2.has_items())
# Drawer 3 should have three stock items # Drawer 3 should have three stock items
self.assertEqual(self.drawer3.stock_items.count(), 15) self.assertEqual(self.drawer3.stock_items.count(), 16)
self.assertEqual(self.drawer3.item_count, 15) self.assertEqual(self.drawer3.item_count, 16)
def test_stock_count(self): def test_stock_count(self):
part = Part.objects.get(pk=1) part = Part.objects.get(pk=1)
@ -108,7 +109,7 @@ class StockTest(TestCase):
self.assertEqual(part.total_stock, 9000) self.assertEqual(part.total_stock, 9000)
# There should be 18 widgets in stock # There should be 18 widgets in stock
self.assertEqual(StockItem.objects.filter(part=25).aggregate(Sum('quantity'))['quantity__sum'], 18) self.assertEqual(StockItem.objects.filter(part=25).aggregate(Sum('quantity'))['quantity__sum'], 19)
def test_delete_location(self): def test_delete_location(self):
@ -168,12 +169,12 @@ class StockTest(TestCase):
self.assertEqual(w1.quantity, 4) self.assertEqual(w1.quantity, 4)
# There should also be a new object still in drawer3 # There should also be a new object still in drawer3
self.assertEqual(StockItem.objects.filter(part=25).count(), 4) self.assertEqual(StockItem.objects.filter(part=25).count(), 5)
widget = StockItem.objects.get(location=self.drawer3.id, part=25, quantity=4) widget = StockItem.objects.get(location=self.drawer3.id, part=25, quantity=4)
# Try to move negative units # Try to move negative units
self.assertFalse(widget.move(self.bathroom, 'Test', None, quantity=-100)) self.assertFalse(widget.move(self.bathroom, 'Test', None, quantity=-100))
self.assertEqual(StockItem.objects.filter(part=25).count(), 4) self.assertEqual(StockItem.objects.filter(part=25).count(), 5)
# Try to move to a blank location # Try to move to a blank location
self.assertFalse(widget.move(None, 'null', None)) self.assertFalse(widget.move(None, 'null', None))
@ -404,3 +405,21 @@ class VariantTest(StockTest):
item.serial += 1 item.serial += 1
item.save() item.save()
class TestResultTest(StockTest):
"""
Tests for the StockItemTestResult model.
"""
def test_test_count(self):
item = StockItem.objects.get(pk=105)
tests = item.test_results
self.assertEqual(tests.count(), 4)
results = item.getTestResults(test="Temperature Test")
self.assertEqual(results.count(), 2)
# Passing tests
self.assertEqual(item.getTestResults(result=True).count(), 3)
self.assertEqual(item.getTestResults(result=False).count(), 1)