From f9db3b680d8765938e36ad880d725cfc2511cdd8 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 25 Apr 2019 00:28:40 +1000 Subject: [PATCH] Reverse lookup for part category URL - Added some test cases for PartCategory model --- InvenTree/InvenTree/models.py | 4 +- InvenTree/part/models.py | 3 +- InvenTree/part/test_category.py | 67 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 InvenTree/part/test_category.py diff --git a/InvenTree/InvenTree/models.py b/InvenTree/InvenTree/models.py index 94dee8e95c..7129a8599a 100644 --- a/InvenTree/InvenTree/models.py +++ b/InvenTree/InvenTree/models.py @@ -74,9 +74,9 @@ class InvenTreeTree(models.Model): @property def children(self): contents = ContentType.objects.get_for_model(type(self)) - children = contents.get_all_objects_for_this_type(parent=self.id) + childs = contents.get_all_objects_for_this_type(parent=self.id) - return children + return childs def getAcceptableParents(self): """ Returns a list of acceptable parent items within this model diff --git a/InvenTree/part/models.py b/InvenTree/part/models.py index 80a4a5da35..c9e4ae0500 100644 --- a/InvenTree/part/models.py +++ b/InvenTree/part/models.py @@ -9,6 +9,7 @@ import tablib from django.utils.translation import gettext_lazy as _ from django.core.exceptions import ValidationError +from django.urls import reverse from django.db import models from django.core.validators import MinValueValidator @@ -25,7 +26,7 @@ class PartCategory(InvenTreeTree): """ def get_absolute_url(self): - return '/part/category/{id}/'.format(id=self.id) + return reverse('category-detail', kwargs={'pk': self.id}) class Meta: verbose_name = "Part Category" diff --git a/InvenTree/part/test_category.py b/InvenTree/part/test_category.py new file mode 100644 index 0000000000..34efa4b2d1 --- /dev/null +++ b/InvenTree/part/test_category.py @@ -0,0 +1,67 @@ +from django.test import TestCase + +from .models import Part, PartCategory +from .models import rename_part_image + + +class CategoryTest(TestCase): + """ + Tests to ensure that the relational category tree functions correctly. + """ + + def setUp(self): + self.p1 = PartCategory.objects.create(name='A', + description='Most highest level', + parent=None) + + self.p2 = PartCategory.objects.create(name='B', + description='Sits under second', + parent=self.p1) + + self.p3 = PartCategory.objects.create(name='C', + description='Third tier category', + parent=self.p2) + + # Add two parts in p2 + Part.objects.create(name='Flange', category=self.p2) + Part.objects.create(name='Flob', category=self.p2) + + # Add one part in p3 + Part.objects.create(name='Blob', category=self.p3) + + def test_parents(self): + self.assertEqual(self.p1.parent, None) + self.assertEqual(self.p2.parent, self.p1) + self.assertEqual(self.p3.parent, self.p2) + + def test_children_count(self): + self.assertEqual(self.p1.has_children, True) + self.assertEqual(self.p2.has_children, True) + self.assertEqual(self.p3.has_children, False) + + def test_unique_childs(self): + childs = self.p1.getUniqueChildren() + + self.assertIn(self.p2.id, childs) + self.assertIn(self.p2.id, childs) + + def test_unique_parents(self): + parents = self.p2.getUniqueParents() + + self.assertIn(self.p1.id, parents) + + def test_path_string(self): + self.assertEqual(str(self.p3), 'A/B/C') + + def test_url(self): + self.assertEqual(self.p1.get_absolute_url(), '/part/category/1/') + + def test_part_count(self): + # No direct parts in the top-level category + self.assertEqual(self.p1.has_parts, False) + self.assertEqual(self.p2.has_parts, True) + self.assertEqual(self.p3.has_parts, True) + + self.assertEqual(self.p1.partcount, 3) + self.assertEqual(self.p2.partcount, 3) + self.assertEqual(self.p3.partcount, 1) \ No newline at end of file