test_util.py 2.5 KB

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