test_util.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from __future__ import absolute_import
  2. from datetime import datetime, timedelta
  3. from sentry.models import GroupHash
  4. from sentry.testutils import TestCase, SnubaTestCase
  5. from sentry.utils import snuba
  6. class SnubaUtilTest(TestCase, SnubaTestCase):
  7. def test_filter_keys_set(self):
  8. snuba.raw_query(
  9. start=datetime.now(),
  10. end=datetime.now(),
  11. filter_keys={"project_id": set([1]), "logger": set(["asdf"])},
  12. aggregations=[["count()", "", "count"]],
  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}