test_utils.py 5.5 KB

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