test_util.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from datetime import datetime, timedelta
  2. from sentry.models import GroupHash
  3. from sentry.receivers import create_default_projects
  4. from sentry.testutils 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 = None
  20. assert snuba.shrink_time_window(issues, year_ago) == year_ago
  21. issues = []
  22. assert snuba.shrink_time_window(issues, 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) == now - timedelta(hours=1, minutes=5)
  32. issues = [group1.id, group2.id]
  33. assert snuba.shrink_time_window(issues, year_ago) == year_ago
  34. # with pytest.raises(snuba.QueryOutsideGroupActivityError):
  35. # # query a group for a time range before it had any activity
  36. # snuba.raw_query(
  37. # start=group1.first_seen - timedelta(days=1, hours=1),
  38. # end=group1.first_seen - timedelta(days=1),
  39. # filter_keys={
  40. # 'project_id': [group1.project_id],
  41. # 'issue': [group1.id],
  42. # },
  43. # aggregations=[
  44. # ['count()', '', 'count'],
  45. # ],
  46. # )
  47. def test_override_options(self):
  48. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}
  49. with snuba.options_override({"foo": 1}):
  50. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  51. with snuba.options_override({"foo": 2}):
  52. assert snuba.OVERRIDE_OPTIONS == {"foo": 2, "consistent": False}
  53. assert snuba.OVERRIDE_OPTIONS == {"foo": 1, "consistent": False}
  54. assert snuba.OVERRIDE_OPTIONS == {"consistent": False}