test_snql_snuba.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import time
  2. import uuid
  3. from datetime import datetime, timedelta
  4. from typing import Optional
  5. from unittest import mock
  6. from django.utils import timezone
  7. from snuba_sdk import Request
  8. from snuba_sdk.column import Column
  9. from snuba_sdk.conditions import Condition, Op
  10. from snuba_sdk.entity import Entity
  11. from snuba_sdk.expressions import Limit
  12. from snuba_sdk.function import Function
  13. from snuba_sdk.query import Query
  14. from sentry.testutils.cases import SnubaTestCase, TestCase
  15. from sentry.utils import snuba
  16. class SnQLTest(TestCase, SnubaTestCase):
  17. def _insert_event_for_time(
  18. self, ts: datetime, group_hash: str = "a" * 32, group_id: Optional[int] = None
  19. ) -> str:
  20. event_id = uuid.uuid4().hex
  21. self.snuba_insert(
  22. (
  23. 2,
  24. "insert",
  25. {
  26. "event_id": event_id,
  27. "primary_hash": group_hash,
  28. "group_id": group_id if group_id else int(group_hash[:16], 16),
  29. "project_id": self.project.id,
  30. "message": "message",
  31. "platform": "python",
  32. "datetime": ts.strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
  33. "data": {"received": time.mktime(ts.timetuple())},
  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"] == []