test_util.py 2.4 KB

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