test_fixcategoriestree.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.core.management import call_command
  2. from django.test import TestCase
  3. from django.utils.six import StringIO
  4. from misago.categories.management.commands import fixcategoriestree
  5. from misago.categories.models import Category
  6. def run_command():
  7. """Run the management command"""
  8. command = fixcategoriestree.Command()
  9. out = StringIO()
  10. call_command(command, stdout=out)
  11. class FixCategoriesTreeTests(TestCase):
  12. """
  13. The purpose is the verify that the management command
  14. fixes the lft/rght values of the thread category tree.
  15. """
  16. def setUp(self):
  17. Category.objects.create(name='Test', slug='test', parent=Category.objects.root_category())
  18. self.fetch_categories()
  19. def assertValidTree(self, expected_tree):
  20. root = Category.objects.root_category()
  21. queryset = Category.objects.filter(tree_id=root.tree_id).order_by('lft')
  22. current_tree = []
  23. for category in queryset:
  24. current_tree.append((category, category.get_level(), category.lft, category.rght))
  25. for i, category in enumerate(expected_tree):
  26. _category = current_tree[i]
  27. if category[0] != _category[0]:
  28. self.fail(('expected category at index #%s to be %s, '
  29. 'found %s instead') % (i, category[0], _category[0]))
  30. if category[1] != _category[1]:
  31. self.fail(('expected level at index #%s to be %s, '
  32. 'found %s instead') % (i, category[1], _category[1]))
  33. if category[2] != _category[2]:
  34. self.fail(('expected lft at index #%s to be %s, '
  35. 'found %s instead') % (i, category[2], _category[2]))
  36. if category[3] != _category[3]:
  37. self.fail(('expected lft at index #%s to be %s, '
  38. 'found %s instead') % (i, category[3], _category[3]))
  39. def fetch_categories(self):
  40. """gets a fresh version from the database"""
  41. self.root = Category.objects.root_category()
  42. self.first_category = Category.objects.get(slug='first-category')
  43. self.test_category = Category.objects.get(slug='test')
  44. def test_fix_categories_tree_unaffected(self):
  45. """Command should not affect a healthy three"""
  46. tree_id = self.root.tree_id
  47. run_command()
  48. self.fetch_categories()
  49. self.assertValidTree([
  50. (self.root, 0, 1, 6),
  51. (self.first_category, 1, 2, 3),
  52. (self.test_category, 1, 4, 5),
  53. ])
  54. self.assertEqual(self.root.tree_id, tree_id, msg="tree_id changed by command")
  55. def test_fix_categories_tree_affected(self):
  56. """Command should fix a broken tree"""
  57. # Root node with too narrow lft/rght range
  58. Category.objects.filter(id=self.root.id).update(lft=1, rght=4)
  59. # Make conflicting/identical lft/rght range
  60. Category.objects.filter(id=self.test_category.id).update(lft=2, rght=3)
  61. run_command()
  62. self.fetch_categories()
  63. self.assertValidTree([
  64. (self.root, 0, 1, 6),
  65. (self.test_category, 1, 2, 3),
  66. (self.first_category, 1, 4, 5),
  67. ])