test_util.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.testutils.cases import SnubaTestCase, TestCase
  5. from sentry.utils import snuba
  6. class SnubaUtilTest(TestCase, SnubaTestCase):
  7. def test_filter_keys_set(self):
  8. create_default_projects()
  9. snuba.raw_query(
  10. start=datetime.now(),
  11. end=datetime.now(),
  12. filter_keys={"project_id": {1}, "culprit": {"asdf"}},
  13. aggregations=[["count()", "", "count"]],
  14. tenant_ids={"referrer": "bleh", "organization_id": 123},
  15. )
  16. def test_shrink_timeframe(self):
  17. now = datetime.now(UTC)
  18. naive_now = now.replace(tzinfo=None)
  19. year_ago = naive_now - timedelta(days=365)
  20. # issues of None / empty list
  21. assert snuba.shrink_time_window(None, year_ago) == year_ago
  22. assert snuba.shrink_time_window([], year_ago) == year_ago
  23. group1 = self.create_group()
  24. group1.first_seen = now - timedelta(hours=1)
  25. group1.last_seen = now
  26. group1.save()
  27. GroupHash.objects.create(project_id=group1.project_id, group=group1, hash="a" * 32)
  28. group2 = self.create_group()
  29. GroupHash.objects.create(project_id=group2.project_id, group=group2, hash="b" * 32)
  30. issues = [group1.id]
  31. assert snuba.shrink_time_window(issues, year_ago) == naive_now - timedelta(
  32. hours=1, minutes=5
  33. )
  34. issues = [group1.id, group2.id]
  35. assert snuba.shrink_time_window(issues, year_ago) == year_ago
  36. # with pytest.raises(snuba.QueryOutsideGroupActivityError):
  37. # # query a group for a time range before it had any activity
  38. # snuba.raw_query(
  39. # start=group1.first_seen - timedelta(days=1, hours=1),
  40. # end=group1.first_seen - timedelta(days=1),
  41. # filter_keys={
  42. # 'project_id': [group1.project_id],
  43. # 'issue': [group1.id],
  44. # },
  45. # aggregations=[
  46. # ['count()', '', 'count'],
  47. # ],
  48. # )
  49. def test_override_options(self):
  50. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}
  51. with snuba.options_override({"foo": 1}):
  52. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  53. with snuba.options_override({"foo": 2}):
  54. assert snuba.OVERRIDE_OPTIONS == {"foo": 2, "consistent": False}
  55. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  56. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}