test_util.py 2.4 KB

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