test_grouptype.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from dataclasses import dataclass
  2. from unittest.mock import patch
  3. from sentry.grouptype.grouptype import (
  4. GroupType,
  5. _group_type_registry,
  6. get_group_type_by_slug,
  7. get_group_types_by_category,
  8. )
  9. from sentry.testutils import TestCase
  10. from sentry.testutils.silo import region_silo_test
  11. from sentry.types.issues import GroupCategory
  12. @region_silo_test
  13. class GroupTypeTest(TestCase):
  14. def test_get_types_by_category(self):
  15. with patch.dict(_group_type_registry, {}, clear=True):
  16. @dataclass(frozen=True)
  17. class TestGroupType(GroupType):
  18. type_id = 1
  19. slug = "test"
  20. description = "Test"
  21. category = GroupCategory.ERROR.value
  22. ignore_limit = 0
  23. @dataclass(frozen=True)
  24. class TestGroupType2(GroupType):
  25. type_id = 2
  26. slug = "hellboy"
  27. description = "Hellboy"
  28. category = GroupCategory.PERFORMANCE.value
  29. @dataclass(frozen=True)
  30. class TestGroupType3(GroupType):
  31. type_id = 3
  32. slug = "angelgirl"
  33. description = "AngelGirl"
  34. category = GroupCategory.PERFORMANCE.value
  35. with patch(
  36. "sentry.grouptype.grouptype.GroupType.__subclasses__",
  37. return_value=[TestGroupType, TestGroupType2, TestGroupType3],
  38. ):
  39. assert get_group_types_by_category(GroupCategory.PERFORMANCE.value) == [2, 3]
  40. assert get_group_types_by_category(GroupCategory.ERROR.value) == [1]
  41. def test_get_group_type_by_slug(self):
  42. with patch.dict(_group_type_registry, {}, clear=True):
  43. @dataclass(frozen=True)
  44. class TestGroupType(GroupType):
  45. type_id = 1
  46. slug = "test"
  47. description = "Test"
  48. category = GroupCategory.ERROR.value
  49. ignore_limit = 0
  50. assert get_group_type_by_slug(TestGroupType.slug) == TestGroupType
  51. nonexistent_slug = "meow"
  52. with self.assertRaisesMessage(
  53. ValueError, f"No group type with the slug {nonexistent_slug} is registered."
  54. ):
  55. get_group_type_by_slug(nonexistent_slug)
  56. def test_category_validation(self):
  57. with patch.dict(_group_type_registry, {}, clear=True):
  58. @dataclass(frozen=True)
  59. class TestGroupType(GroupType):
  60. type_id = 1
  61. slug = "error"
  62. description = "Error"
  63. category = 22
  64. with self.assertRaisesMessage(
  65. ValueError,
  66. f"Category must be one of {[category.value for category in GroupCategory]} from GroupCategory",
  67. ):
  68. TestGroupType(1, "error", "Error", 22)