Fixed tests

- Tree classes now need extra configuration in the fixture
- Check for null pk when cleaning a tree node
This commit is contained in:
Oliver Walters 2019-09-08 19:41:54 +10:00
parent a5189b8f3f
commit dac61eafa2
6 changed files with 71 additions and 8 deletions

View File

@ -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):

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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,6 +59,10 @@
name: 'Drawer_2'
description: 'Also in my desk'
parent: 4
level: 0
tree_id: 2
lft: 4
rght: 5
- model: stock.stocklocation
pk: 7
@ -46,3 +70,7 @@
name: 'Drawer_3'
description: 'Again, in my desk'
parent: 4
level: 0
tree_id: 2
lft: 6
rght: 7

View File

@ -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)