test_snql_snuba.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. )
  37. return event_id
  38. @mock.patch("sentry.utils.metrics.incr")
  39. def test_basic(self, mock_metrics_incr) -> None:
  40. now = datetime.now()
  41. self._insert_event_for_time(now)
  42. query = (
  43. Query(match=Entity("events"))
  44. .set_select([Function("count", [], "count")])
  45. .set_groupby([Column("project_id")])
  46. .set_where(
  47. [
  48. Condition(Column("project_id"), Op.EQ, self.project.id),
  49. Condition(Column("timestamp"), Op.GTE, now - timedelta(days=1)),
  50. Condition(Column("timestamp"), Op.LT, now + timedelta(days=1)),
  51. ]
  52. )
  53. )
  54. request = Request(
  55. dataset="events",
  56. app_id="tests",
  57. query=query,
  58. tenant_ids={"referrer": "testing.test", "organization_id": 1},
  59. )
  60. result = snuba.raw_snql_query(request, referrer="referrer_not_in_enum")
  61. assert len(result["data"]) == 1
  62. assert result["data"][0] == {"count": 1, "project_id": self.project.id}
  63. mock_metrics_incr.assert_any_call(
  64. "snql.sdk.api.new_referrers", tags={"referrer": "referrer_not_in_enum"}
  65. )
  66. def test_cache(self):
  67. """Minimal test to verify if use_cache works"""
  68. results = snuba.raw_snql_query(
  69. Request(
  70. dataset="events",
  71. app_id="tests",
  72. tenant_ids={"referrer": "testing.test", "organization_id": 1},
  73. query=Query(
  74. Entity("events"),
  75. select=[Column("event_id")],
  76. where=[
  77. Condition(Column("project_id"), Op.EQ, self.project.id),
  78. Condition(Column("timestamp"), Op.GTE, timezone.now() - timedelta(days=1)),
  79. Condition(Column("timestamp"), Op.LT, timezone.now()),
  80. ],
  81. limit=Limit(1),
  82. ),
  83. ),
  84. use_cache=True,
  85. )
  86. assert results["data"] == []