Reverse lookup for part category URL

- Added some test cases for PartCategory model
This commit is contained in:
Oliver Walters 2019-04-25 00:28:40 +10:00
parent a2d4403968
commit f9db3b680d
3 changed files with 71 additions and 3 deletions

View File

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

View File

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

View File

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