From 2bb9fd9955a98edeeedb656b0ba27dd291c371b8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sat, 16 May 2020 20:27:01 +1000 Subject: [PATCH] Add some unit testing for the new model --- InvenTree/stock/fixtures/stock.yaml | 12 +++++++ InvenTree/stock/fixtures/stock_tests.yaml | 31 +++++++++++++++++++ .../0041_stockitemtestresult_notes.py | 18 +++++++++++ InvenTree/stock/models.py | 25 +++++++++++++++ InvenTree/stock/tests.py | 29 ++++++++++++++--- 5 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 InvenTree/stock/fixtures/stock_tests.yaml create mode 100644 InvenTree/stock/migrations/0041_stockitemtestresult_notes.py diff --git a/InvenTree/stock/fixtures/stock.yaml b/InvenTree/stock/fixtures/stock.yaml index cac34fb01b..156aa8db53 100644 --- a/InvenTree/stock/fixtures/stock.yaml +++ b/InvenTree/stock/fixtures/stock.yaml @@ -70,6 +70,18 @@ lft: 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 - model: stock.stockitem pk: 500 diff --git a/InvenTree/stock/fixtures/stock_tests.yaml b/InvenTree/stock/fixtures/stock_tests.yaml new file mode 100644 index 0000000000..9a82b6ada7 --- /dev/null +++ b/InvenTree/stock/fixtures/stock_tests.yaml @@ -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' \ No newline at end of file diff --git a/InvenTree/stock/migrations/0041_stockitemtestresult_notes.py b/InvenTree/stock/migrations/0041_stockitemtestresult_notes.py new file mode 100644 index 0000000000..1f258a8f6f --- /dev/null +++ b/InvenTree/stock/migrations/0041_stockitemtestresult_notes.py @@ -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'), + ), + ] diff --git a/InvenTree/stock/models.py b/InvenTree/stock/models.py index 0d113e8d9e..e6571fa89e 100644 --- a/InvenTree/stock/models.py +++ b/InvenTree/stock/models.py @@ -921,6 +921,24 @@ class StockItem(MPTTModel): 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') def before_delete_stock_item(sender, instance, using, **kwargs): @@ -1009,6 +1027,7 @@ class StockItemTestResult(models.Model): result: Test result value (pass / fail / etc) value: Recorded test output value (optional) attachment: Link to StockItem attachment (optional) + notes: Extra user notes related to the test (optional) user: User who uploaded the test result date: Date the test result was recorded """ @@ -1059,6 +1078,12 @@ class StockItemTestResult(models.Model): help_text=_('Test result attachment'), ) + notes = models.CharField( + blank=True, max_length=500, + verbose_name=_('Notes'), + help_text=_("Test notes"), + ) + user = models.ForeignKey( User, on_delete = models.SET_NULL, diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index edb9660000..2c8640d48a 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -17,6 +17,7 @@ class StockTest(TestCase): 'part', 'location', 'stock', + 'stock_tests', ] def setUp(self): @@ -95,8 +96,8 @@ class StockTest(TestCase): self.assertFalse(self.drawer2.has_items()) # Drawer 3 should have three stock items - self.assertEqual(self.drawer3.stock_items.count(), 15) - self.assertEqual(self.drawer3.item_count, 15) + self.assertEqual(self.drawer3.stock_items.count(), 16) + self.assertEqual(self.drawer3.item_count, 16) def test_stock_count(self): part = Part.objects.get(pk=1) @@ -108,7 +109,7 @@ class StockTest(TestCase): self.assertEqual(part.total_stock, 9000) # 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): @@ -168,12 +169,12 @@ class StockTest(TestCase): self.assertEqual(w1.quantity, 4) # 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) # Try to move negative units 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 self.assertFalse(widget.move(None, 'null', None)) @@ -404,3 +405,21 @@ class VariantTest(StockTest): item.serial += 1 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)