test_utils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from django.test import TestCase
  2. from misago.categories.models import Category
  3. from misago.core import threadstore
  4. from misago.core.cache import cache
  5. from misago.threads import testutils
  6. from misago.threads.utils import add_categories_to_threads
  7. class AddCategoriesToThreadsTests(TestCase):
  8. def setUp(self):
  9. super(AddCategoriesToThreadsTests, self).setUp()
  10. cache.clear()
  11. threadstore.clear()
  12. self.root = Category.objects.root_category()
  13. """
  14. Create categories tree for test cases:
  15. First category (created by migration)
  16. Category A
  17. + Category B
  18. + Subcategory C
  19. + Subcategory D
  20. Category E
  21. + Subcategory F
  22. """
  23. Category(
  24. name='Category A',
  25. slug='category-a',
  26. css_class='showing-category-a',
  27. ).insert_at(self.root, position='last-child', save=True)
  28. Category(
  29. name='Category E',
  30. slug='category-e',
  31. css_class='showing-category-e',
  32. ).insert_at(self.root, position='last-child', save=True)
  33. self.root = Category.objects.root_category()
  34. self.category_a = Category.objects.get(slug='category-a')
  35. Category(
  36. name='Category B',
  37. slug='category-b',
  38. css_class='showing-category-b',
  39. ).insert_at(self.category_a, position='last-child', save=True)
  40. self.category_b = Category.objects.get(slug='category-b')
  41. Category(
  42. name='Category C',
  43. slug='category-c',
  44. css_class='showing-category-c',
  45. ).insert_at(self.category_b, position='last-child', save=True)
  46. Category(
  47. name='Category D',
  48. slug='category-d',
  49. css_class='showing-category-d',
  50. ).insert_at(self.category_b, position='last-child', save=True)
  51. self.category_c = Category.objects.get(slug='category-c')
  52. self.category_d = Category.objects.get(slug='category-d')
  53. self.category_e = Category.objects.get(slug='category-e')
  54. Category(
  55. name='Category F',
  56. slug='category-f',
  57. css_class='showing-category-f',
  58. ).insert_at(self.category_e, position='last-child', save=True)
  59. cache.clear()
  60. threadstore.clear()
  61. Category.objects.partial_rebuild(self.root.tree_id)
  62. self.root = Category.objects.root_category()
  63. self.category_a = Category.objects.get(slug='category-a')
  64. self.category_b = Category.objects.get(slug='category-b')
  65. self.category_c = Category.objects.get(slug='category-c')
  66. self.category_d = Category.objects.get(slug='category-d')
  67. self.category_e = Category.objects.get(slug='category-e')
  68. self.category_f = Category.objects.get(slug='category-f')
  69. self.categories = list(Category.objects.all_categories(
  70. include_root=True))
  71. def test_root_thread_from_root(self):
  72. """thread in root category is handled"""
  73. thread = testutils.post_thread(category=self.root)
  74. add_categories_to_threads(self.root, self.categories, [thread])
  75. self.assertIsNone(thread.top_category)
  76. self.assertEqual(thread.category, self.root)
  77. def test_root_thread_from_elsewhere(self):
  78. """thread in root category is handled"""
  79. thread = testutils.post_thread(category=self.root)
  80. add_categories_to_threads(self.category_e, self.categories, [thread])
  81. self.assertIsNone(thread.top_category)
  82. self.assertEqual(thread.category, self.root)
  83. def test_direct_child_thread_from_parent(self):
  84. """thread in direct child category is handled"""
  85. thread = testutils.post_thread(category=self.category_e)
  86. add_categories_to_threads(self.root, self.categories, [thread])
  87. self.assertEqual(thread.top_category, self.category_e)
  88. self.assertEqual(thread.category, self.category_e)
  89. def test_direct_child_thread_from_elsewhere(self):
  90. """thread in direct child category is handled"""
  91. thread = testutils.post_thread(category=self.category_e)
  92. add_categories_to_threads(self.category_b, self.categories, [thread])
  93. self.assertEqual(thread.top_category, self.category_e)
  94. self.assertEqual(thread.category, self.category_e)
  95. def test_child_thread_from_root(self):
  96. """thread in child category is handled"""
  97. thread = testutils.post_thread(category=self.category_d)
  98. add_categories_to_threads(self.root, self.categories, [thread])
  99. self.assertEqual(thread.top_category, self.category_a)
  100. self.assertEqual(thread.category, self.category_d)
  101. def test_child_thread_from_parent(self):
  102. """thread in child category is handled"""
  103. thread = testutils.post_thread(category=self.category_d)
  104. add_categories_to_threads(self.category_a, self.categories, [thread])
  105. self.assertEqual(thread.top_category, self.category_b)
  106. self.assertEqual(thread.category, self.category_d)
  107. def test_child_thread_from_category(self):
  108. """thread in child category is handled"""
  109. thread = testutils.post_thread(category=self.category_d)
  110. add_categories_to_threads(self.category_d, self.categories, [thread])
  111. self.assertIsNone(thread.top_category)
  112. self.assertEqual(thread.category, self.category_d)
  113. def test_child_thread_from_elsewhere(self):
  114. """thread in child category is handled"""
  115. thread = testutils.post_thread(category=self.category_d)
  116. add_categories_to_threads(self.category_f, self.categories, [thread])
  117. self.assertEqual(thread.top_category, self.category_a)
  118. self.assertEqual(thread.category, self.category_d)