test_util.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from datetime import 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()
  18. year_ago = now - timedelta(days=365)
  19. # issues of None / empty list
  20. assert snuba.shrink_time_window(None, year_ago) == year_ago
  21. assert snuba.shrink_time_window([], year_ago) == year_ago
  22. group1 = self.create_group()
  23. group1.first_seen = now - timedelta(hours=1)
  24. group1.last_seen = now
  25. group1.save()
  26. GroupHash.objects.create(project_id=group1.project_id, group=group1, hash="a" * 32)
  27. group2 = self.create_group()
  28. GroupHash.objects.create(project_id=group2.project_id, group=group2, hash="b" * 32)
  29. issues = [group1.id]
  30. assert snuba.shrink_time_window(issues, year_ago) == now - timedelta(hours=1, minutes=5)
  31. issues = [group1.id, group2.id]
  32. assert snuba.shrink_time_window(issues, year_ago) == year_ago
  33. # with pytest.raises(snuba.QueryOutsideGroupActivityError):
  34. # # query a group for a time range before it had any activity
  35. # snuba.raw_query(
  36. # start=group1.first_seen - timedelta(days=1, hours=1),
  37. # end=group1.first_seen - timedelta(days=1),
  38. # filter_keys={
  39. # 'project_id': [group1.project_id],
  40. # 'issue': [group1.id],
  41. # },
  42. # aggregations=[
  43. # ['count()', '', 'count'],
  44. # ],
  45. # )
  46. def test_override_options(self):
  47. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}
  48. with snuba.options_override({"foo": 1}):
  49. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  50. with snuba.options_override({"foo": 2}):
  51. assert snuba.OVERRIDE_OPTIONS == {"foo": 2, "consistent": False}
  52. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  53. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}