Path name bug fix (#3694)

* bug fix: Ensure that child tree items update their pathstring when requried

* Add unit tests for fix

* Unit test fix

* Tweak for unit test
This commit is contained in:
Oliver 2022-09-20 19:02:08 +10:00 committed by GitHub
parent 06ba4099b3
commit 04b3f022a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 22 deletions

View File

@ -68,28 +68,10 @@ def constructPathString(path, max_chars=250):
pathstring = '/'.join(path) pathstring = '/'.join(path)
idx = 0
# Replace middle elements to limit the pathstring # Replace middle elements to limit the pathstring
if len(pathstring) > max_chars: if len(pathstring) > max_chars:
mid = len(path) // 2 n = int(max_chars / 2 - 2)
path_l = path[0:mid] pathstring = pathstring[:n] + "..." + pathstring[-n:]
path_r = path[mid:]
# Ensure the pathstring length is limited
while len(pathstring) > max_chars:
# Remove an element from the list
if idx % 2 == 0:
path_l = path_l[:-1]
else:
path_r = path_r[1:]
subpath = path_l + ['...'] + path_r
pathstring = '/'.join(subpath)
idx += 1
return pathstring return pathstring

View File

@ -516,8 +516,18 @@ class InvenTreeTree(MPTTModel):
) )
if pathstring != self.pathstring: if pathstring != self.pathstring:
if 'force_insert' in kwargs:
del kwargs['force_insert']
kwargs['force_update'] = True
self.pathstring = pathstring self.pathstring = pathstring
super().save(force_update=True) super().save(*args, **kwargs)
# Ensure that the pathstring changes are propagated down the tree also
for child in self.get_children():
child.save(*args, **kwargs)
class Meta: class Meta:
"""Metaclass defines extra model properties.""" """Metaclass defines extra model properties."""

View File

@ -118,7 +118,7 @@ class CategoryTest(TestCase):
self.assertTrue(len(child.path), 26) self.assertTrue(len(child.path), 26)
self.assertEqual( self.assertEqual(
child.pathstring, child.pathstring,
"Cat/AAAAAAAAAA/BBBBBBBBBB/CCCCCCCCCC/DDDDDDDDDD/EEEEEEEEEE/FFFFFFFFFF/GGGGGGGGGG/HHHHHHHHHH/IIIIIIIIII/JJJJJJJJJJ/.../OOOOOOOOOO/PPPPPPPPPP/QQQQQQQQQQ/RRRRRRRRRR/SSSSSSSSSS/TTTTTTTTTT/UUUUUUUUUU/VVVVVVVVVV/WWWWWWWWWW/XXXXXXXXXX/YYYYYYYYYY/ZZZZZZZZZZ" "Cat/AAAAAAAAAA/BBBBBBBBBB/CCCCCCCCCC/DDDDDDDDDD/EEEEEEEEEE/FFFFFFFFFF/GGGGGGGGGG/HHHHHHHHHH/IIIIIIIIII/JJJJJJJJJJ/KKKKKKKKK...OO/PPPPPPPPPP/QQQQQQQQQQ/RRRRRRRRRR/SSSSSSSSSS/TTTTTTTTTT/UUUUUUUUUU/VVVVVVVVVV/WWWWWWWWWW/XXXXXXXXXX/YYYYYYYYYY/ZZZZZZZZZZ"
) )
self.assertTrue(len(child.pathstring) <= 250) self.assertTrue(len(child.pathstring) <= 250)

View File

@ -48,6 +48,82 @@ class StockTestBase(InvenTreeTestCase):
class StockTest(StockTestBase): class StockTest(StockTestBase):
"""Tests to ensure that the stock location tree functions correcly.""" """Tests to ensure that the stock location tree functions correcly."""
def test_pathstring(self):
"""Check that pathstring updates occur as expected"""
a = StockLocation.objects.create(name="A")
b = StockLocation.objects.create(name="B", parent=a)
c = StockLocation.objects.create(name="C", parent=b)
d = StockLocation.objects.create(name="D", parent=c)
def refresh():
a.refresh_from_db()
b.refresh_from_db()
c.refresh_from_db()
d.refresh_from_db()
# Initial checks
self.assertEqual(a.pathstring, "A")
self.assertEqual(b.pathstring, "A/B")
self.assertEqual(c.pathstring, "A/B/C")
self.assertEqual(d.pathstring, "A/B/C/D")
c.name = "Cc"
c.save()
refresh()
self.assertEqual(a.pathstring, "A")
self.assertEqual(b.pathstring, "A/B")
self.assertEqual(c.pathstring, "A/B/Cc")
self.assertEqual(d.pathstring, "A/B/Cc/D")
b.name = "Bb"
b.save()
refresh()
self.assertEqual(a.pathstring, "A")
self.assertEqual(b.pathstring, "A/Bb")
self.assertEqual(c.pathstring, "A/Bb/Cc")
self.assertEqual(d.pathstring, "A/Bb/Cc/D")
a.name = "Aa"
a.save()
refresh()
self.assertEqual(a.pathstring, "Aa")
self.assertEqual(b.pathstring, "Aa/Bb")
self.assertEqual(c.pathstring, "Aa/Bb/Cc")
self.assertEqual(d.pathstring, "Aa/Bb/Cc/D")
d.name = "Dd"
d.save()
refresh()
self.assertEqual(a.pathstring, "Aa")
self.assertEqual(b.pathstring, "Aa/Bb")
self.assertEqual(c.pathstring, "Aa/Bb/Cc")
self.assertEqual(d.pathstring, "Aa/Bb/Cc/Dd")
# Test a really long name
# (it will be clipped to < 250 characters)
a.name = "A" * 100
a.save()
b.name = "B" * 100
b.save()
c.name = "C" * 100
c.save()
d.name = "D" * 100
d.save()
refresh()
self.assertEqual(len(a.pathstring), 100)
self.assertEqual(len(b.pathstring), 201)
self.assertEqual(len(c.pathstring), 249)
self.assertEqual(len(d.pathstring), 249)
self.assertTrue(d.pathstring.startswith("AAAAAAAA"))
self.assertTrue(d.pathstring.endswith("DDDDDDDD"))
def test_link(self): def test_link(self):
"""Test the link URL field validation""" """Test the link URL field validation"""