From dac61eafa23d64ba37e53887d6e28979f7b2440a Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 8 Sep 2019 19:41:54 +1000 Subject: [PATCH] Fixed tests - Tree classes now need extra configuration in the fixture - Check for null pk when cleaning a tree node --- InvenTree/InvenTree/models.py | 4 +-- InvenTree/part/fixtures/category.yaml | 34 +++++++++++++++++++++++++- InvenTree/part/models.py | 2 +- InvenTree/part/test_category.py | 4 +-- InvenTree/stock/fixtures/location.yaml | 30 ++++++++++++++++++++++- InvenTree/stock/tests.py | 5 +++- 6 files changed, 71 insertions(+), 8 deletions(-) diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index d5b1cddc17..38539d0118 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -82,7 +82,7 @@ class InvenTreeTree(MPTTModel): @property def has_children(self): """ True if there are any children under this item """ - return self.getUniqueChildren().count() > 0 + return self.getUniqueChildren(include_self=False).count() > 0 def getAcceptableParents(self): """ Returns a list of acceptable parent items within this model @@ -157,7 +157,7 @@ class InvenTreeTree(MPTTModel): pass # Ensure that the new parent is not already a child - if self.id in self.getUniqueChildren(include_self=False): + if self.pk is not None and self.id in self.getUniqueChildren(include_self=False): raise ValidationError("Category cannot set a child as parent") def __str__(self): diff --git a/InvenTree/part/fixtures/category.yaml b/InvenTree/part/fixtures/category.yaml index 2f46dcce65..ad40b6b1fd 100644 --- a/InvenTree/part/fixtures/category.yaml +++ b/InvenTree/part/fixtures/category.yaml @@ -6,7 +6,11 @@ name: Electronics description: Electronic components parent: null - default_location: 1 # Home + default_location: 1 + level: 0 + tree_id: 1 + lft: 1 + rght: 12 - model: part.partcategory pk: 2 @@ -15,6 +19,10 @@ description: Resistors parent: 1 default_location: null + level: 1 + tree_id: 1 + lft: 2 + rght: 3 - model: part.partcategory pk: 3 @@ -23,6 +31,10 @@ description: Capacitors parent: 1 default_location: null + level: 1 + tree_id: 1 + lft: 4 + rght: 5 - model: part.partcategory pk: 4 @@ -31,6 +43,10 @@ description: Integrated Circuits parent: 1 default_location: null + level: 1 + tree_id: 1 + lft: 6 + rght: 11 - model: part.partcategory pk: 5 @@ -39,6 +55,10 @@ description: Microcontrollers parent: 4 default_location: null + level: 2 + tree_id: 1 + lft: 7 + rght: 8 - model: part.partcategory pk: 6 @@ -47,6 +67,10 @@ description: Communication interfaces parent: 4 default_location: null + level: 2 + tree_id: 1 + lft: 9 + rght: 10 - model: part.partcategory pk: 7 @@ -54,6 +78,10 @@ name: Mechanical description: Mechanical componenets default_location: null + level: 0 + tree_id: 2 + lft: 1 + rght: 4 - model: part.partcategory pk: 8 @@ -62,3 +90,7 @@ description: Screws, bolts, etc parent: 7 default_location: 5 + level: 1 + tree_id: 2 + lft: 2 + rght: 3 diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 73cfa96e2c..1831962a44 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -385,7 +385,7 @@ class Part(models.Model): cats = self.category.get_ancestors(ascending=True, include_self=True) for cat in cats: - if cat.defaul_location: + if cat.default_location: return cat.default_location # Default case - no default category found diff --git a/InvenTree/part/test_category.py b/InvenTree/part/test_category.py index d9bb42d546..ef8060afd0 100644 --- a/InvenTree/part/test_category.py +++ b/InvenTree/part/test_category.py @@ -48,7 +48,7 @@ class CategoryTest(TestCase): def test_unique_childs(self): """ Test the 'unique_children' functionality """ - childs = self.electronics.getUniqueChildren() + childs = [item.pk for item in self.electronics.getUniqueChildren()] self.assertIn(self.transceivers.id, childs) self.assertIn(self.ic.id, childs) @@ -58,7 +58,7 @@ class CategoryTest(TestCase): def test_unique_parents(self): """ Test the 'unique_parents' functionality """ - parents = self.transceivers.getUniqueParents() + parents = [item.pk for item in self.transceivers.getUniqueParents()] self.assertIn(self.electronics.id, parents) self.assertIn(self.ic.id, parents) diff --git a/InvenTree/stock/fixtures/location.yaml b/InvenTree/stock/fixtures/location.yaml index f34490af29..1d5d1300af 100644 --- a/InvenTree/stock/fixtures/location.yaml +++ b/InvenTree/stock/fixtures/location.yaml @@ -5,6 +5,10 @@ fields: name: 'Home' description: 'My house' + level: 0 + tree_id: 1 + lft: 1 + rght: 6 - model: stock.stocklocation pk: 2 @@ -12,6 +16,10 @@ name: 'Bathroom' description: 'Where I keep my bath' parent: 1 + level: 1 + tree_id: 1 + lft: 2 + rght: 3 - model: stock.stocklocation pk: 3 @@ -19,12 +27,20 @@ name: 'Dining Room' description: 'A table lives here' parent: 1 + level: 0 + tree_id: 1 + lft: 4 + rght: 5 - model: stock.stocklocation pk: 4 fields: name: 'Office' description: 'Place of work' + level: 0 + tree_id: 2 + lft: 1 + rght: 8 - model: stock.stocklocation pk: 5 @@ -32,6 +48,10 @@ name: 'Drawer_1' description: 'In my desk' parent: 4 + level: 0 + tree_id: 2 + lft: 2 + rght: 3 - model: stock.stocklocation pk: 6 @@ -39,10 +59,18 @@ name: 'Drawer_2' description: 'Also in my desk' parent: 4 + level: 0 + tree_id: 2 + lft: 4 + rght: 5 - model: stock.stocklocation pk: 7 fields: name: 'Drawer_3' description: 'Again, in my desk' - parent: 4 \ No newline at end of file + parent: 4 + level: 0 + tree_id: 2 + lft: 6 + rght: 7 \ No newline at end of file diff --git a/InvenTree/stock/tests.py b/InvenTree/stock/tests.py index 437464a77a..95eb1002ad 100644 --- a/InvenTree/stock/tests.py +++ b/InvenTree/stock/tests.py @@ -67,15 +67,18 @@ class StockTest(TestCase): # Move one of the drawers self.drawer3.parent = self.home + self.drawer3.save() + self.assertNotEqual(self.drawer3.parent, self.office) self.assertEqual(self.drawer3.pathstring, 'Home/Drawer_3') def test_children(self): self.assertTrue(self.office.has_children) + self.assertFalse(self.drawer2.has_children) - childs = self.office.getUniqueChildren() + childs = [item.pk for item in self.office.getUniqueChildren()] self.assertIn(self.drawer1.id, childs) self.assertIn(self.drawer2.id, childs)