test_util.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from datetime import UTC, datetime, timedelta
  2. from sentry.models.grouphash import GroupHash
  3. from sentry.receivers import create_default_projects
  4. from sentry.snuba.models import QuerySubscription, SnubaQuery
  5. from sentry.snuba.utils import build_query_strings
  6. from sentry.testutils.cases import SnubaTestCase, TestCase
  7. from sentry.utils import snuba
  8. class SnubaUtilTest(TestCase, SnubaTestCase):
  9. def test_filter_keys_set(self):
  10. create_default_projects()
  11. snuba.raw_query(
  12. start=datetime.now(),
  13. end=datetime.now(),
  14. filter_keys={"project_id": {1}, "culprit": {"asdf"}},
  15. aggregations=[["count()", "", "count"]],
  16. tenant_ids={"referrer": "bleh", "organization_id": 123},
  17. )
  18. def test_shrink_timeframe(self):
  19. now = datetime.now(UTC)
  20. naive_now = now.replace(tzinfo=None)
  21. year_ago = naive_now - timedelta(days=365)
  22. # issues of None / empty list
  23. assert snuba.shrink_time_window(None, year_ago) == year_ago
  24. assert snuba.shrink_time_window([], year_ago) == year_ago
  25. group1 = self.create_group()
  26. group1.first_seen = now - timedelta(hours=1)
  27. group1.last_seen = now
  28. group1.save()
  29. GroupHash.objects.create(project_id=group1.project_id, group=group1, hash="a" * 32)
  30. group2 = self.create_group()
  31. GroupHash.objects.create(project_id=group2.project_id, group=group2, hash="b" * 32)
  32. issues = [group1.id]
  33. assert snuba.shrink_time_window(issues, year_ago) == naive_now - timedelta(
  34. hours=1, minutes=5
  35. )
  36. issues = [group1.id, group2.id]
  37. assert snuba.shrink_time_window(issues, year_ago) == year_ago
  38. # with pytest.raises(snuba.QueryOutsideGroupActivityError):
  39. # # query a group for a time range before it had any activity
  40. # snuba.raw_query(
  41. # start=group1.first_seen - timedelta(days=1, hours=1),
  42. # end=group1.first_seen - timedelta(days=1),
  43. # filter_keys={
  44. # 'project_id': [group1.project_id],
  45. # 'issue': [group1.id],
  46. # },
  47. # aggregations=[
  48. # ['count()', '', 'count'],
  49. # ],
  50. # )
  51. def test_override_options(self):
  52. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}
  53. with snuba.options_override({"foo": 1}):
  54. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  55. with snuba.options_override({"foo": 2}):
  56. assert snuba.OVERRIDE_OPTIONS == {"foo": 2, "consistent": False}
  57. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  58. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}
  59. def test_build_query_strings(self):
  60. snuba_query_no_query = SnubaQuery.objects.create(
  61. type=SnubaQuery.Type.ERROR.value,
  62. dataset="events",
  63. aggregate="count()",
  64. time_window=60,
  65. resolution=60,
  66. )
  67. subscription_noquery = QuerySubscription.objects.create(
  68. status=QuerySubscription.Status.CREATING.value,
  69. project=self.project,
  70. snuba_query=snuba_query_no_query,
  71. query_extra="foobar",
  72. )
  73. query_strings = build_query_strings(subscription_noquery, snuba_query_no_query)
  74. assert query_strings.query_string == "foobar"
  75. assert query_strings.query == ""
  76. assert query_strings.query_extra == "foobar"
  77. snuba_query_with_query = SnubaQuery.objects.create(
  78. type=SnubaQuery.Type.ERROR.value,
  79. dataset="events",
  80. aggregate="count()",
  81. time_window=60,
  82. resolution=60,
  83. query="event.type:error",
  84. )
  85. subscription_with_query = QuerySubscription.objects.create(
  86. status=QuerySubscription.Status.CREATING.value,
  87. project=self.project,
  88. snuba_query=snuba_query_with_query,
  89. query_extra="foobar",
  90. )
  91. query_strings = build_query_strings(subscription_with_query, snuba_query_with_query)
  92. assert query_strings.query_string == "event.type:error and foobar"
  93. assert query_strings.query == "event.type:error"
  94. assert query_strings.query_extra == " and foobar"