test_snql_snuba.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import time
  2. import uuid
  3. from datetime import datetime, timedelta
  4. from unittest import mock
  5. from django.utils import timezone
  6. from snuba_sdk import Request
  7. from snuba_sdk.column import Column
  8. from snuba_sdk.conditions import Condition, Op
  9. from snuba_sdk.entity import Entity
  10. from snuba_sdk.expressions import Limit
  11. from snuba_sdk.function import Function
  12. from snuba_sdk.query import Query
  13. from sentry.testutils.cases import SnubaTestCase, TestCase
  14. from sentry.utils import snuba
  15. class SnQLTest(TestCase, SnubaTestCase):
  16. def _insert_event_for_time(
  17. self, ts: datetime, group_hash: str = "a" * 32, group_id: int | None = None
  18. ) -> str:
  19. event_id = uuid.uuid4().hex
  20. self.snuba_insert(
  21. (
  22. 2,
  23. "insert",
  24. {
  25. "event_id": event_id,
  26. "primary_hash": group_hash,
  27. "group_id": group_id if group_id else int(group_hash[:16], 16),
  28. "project_id": self.project.id,
  29. "message": "message",
  30. "platform": "python",
  31. "datetime": ts.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
  32. "data": {"received": time.mktime(ts.timetuple())},
  33. },
  34. )
  35. )
  36. return event_id
  37. @mock.patch("sentry.utils.metrics.incr")
  38. def test_basic(self, mock_metrics_incr) -> None:
  39. now = datetime.now()
  40. self._insert_event_for_time(now)
  41. query = (
  42. Query(match=Entity("events"))
  43. .set_select([Function("count", [], "count")])
  44. .set_groupby([Column("project_id")])
  45. .set_where(
  46. [
  47. Condition(Column("project_id"), Op.EQ, self.project.id),
  48. Condition(Column("timestamp"), Op.GTE, now - timedelta(days=1)),
  49. Condition(Column("timestamp"), Op.LT, now + timedelta(days=1)),
  50. ]
  51. )
  52. )
  53. request = Request(
  54. dataset="events",
  55. app_id="tests",
  56. query=query,
  57. tenant_ids={"referrer": "testing.test", "organization_id": 1},
  58. )
  59. result = snuba.raw_snql_query(request, referrer="referrer_not_in_enum")
  60. assert len(result["data"]) == 1
  61. assert result["data"][0] == {"count": 1, "project_id": self.project.id}
  62. mock_metrics_incr.assert_any_call(
  63. "snql.sdk.api.new_referrers", tags={"referrer": "referrer_not_in_enum"}
  64. )
  65. def test_cache(self):
  66. """Minimal test to verify if use_cache works"""
  67. results = snuba.raw_snql_query(
  68. Request(
  69. dataset="events",
  70. app_id="tests",
  71. tenant_ids={"referrer": "testing.test", "organization_id": 1},
  72. query=Query(
  73. Entity("events"),
  74. select=[Column("event_id")],
  75. where=[
  76. Condition(Column("project_id"), Op.EQ, self.project.id),
  77. Condition(Column("timestamp"), Op.GTE, timezone.now() - timedelta(days=1)),
  78. Condition(Column("timestamp"), Op.LT, timezone.now()),
  79. ],
  80. limit=Limit(1),
  81. ),
  82. ),
  83. use_cache=True,
  84. )
  85. assert results["data"] == []