test_org_stats_v2.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from datetime import datetime, timedelta, timezone
  2. from django.test.client import RequestFactory
  3. from django.urls import reverse
  4. from fixtures.apidocs_test_case import APIDocsTestCase
  5. from sentry.constants import DataCategory
  6. from sentry.testutils.cases import OutcomesSnubaTest
  7. from sentry.utils.outcomes import Outcome
  8. class OrganizationStatsDocs(APIDocsTestCase, OutcomesSnubaTest):
  9. def setUp(self):
  10. super().setUp()
  11. self.now = datetime(2021, 3, 14, 12, 27, 28, tzinfo=timezone.utc)
  12. self.login_as(user=self.user)
  13. self.store_outcomes(
  14. {
  15. "org_id": self.organization.id,
  16. "timestamp": self.now - timedelta(hours=1),
  17. "project_id": self.project.id,
  18. "outcome": Outcome.ACCEPTED,
  19. "reason": "none",
  20. "category": DataCategory.ERROR,
  21. "quantity": 1,
  22. },
  23. 5,
  24. )
  25. self.store_outcomes(
  26. {
  27. "org_id": self.organization.id,
  28. "timestamp": self.now - timedelta(hours=1),
  29. "project_id": self.project.id,
  30. "outcome": Outcome.ACCEPTED,
  31. "reason": "none",
  32. "category": DataCategory.PROFILE_DURATION,
  33. "quantity": 1000, # Duration in milliseconds
  34. },
  35. 3,
  36. )
  37. self.url = reverse(
  38. "sentry-api-0-organization-stats-v2",
  39. kwargs={"organization_id_or_slug": self.organization.slug},
  40. )
  41. def test_get(self):
  42. """
  43. Test that the organization stats endpoint returns valid schema.
  44. This test verifies that the endpoint correctly handles basic queries with interval, field and groupBy parameters.
  45. """
  46. query = {"interval": "1d", "field": "sum(quantity)", "groupBy": "category"}
  47. response = self.client.get(self.url, query, format="json")
  48. request = RequestFactory().get(self.url)
  49. self.validate_schema(request, response)
  50. def test_profile_duration_category(self):
  51. """
  52. Test that the organization stats endpoint correctly handles profile duration category.
  53. This test verifies that the endpoint returns valid schema when filtering by profile_duration category
  54. and aggregating quantity values.
  55. """
  56. query = {
  57. "interval": "1d",
  58. "field": "sum(quantity)",
  59. "groupBy": "category",
  60. "category": "profile_duration",
  61. }
  62. response = self.client.get(self.url, query, format="json")
  63. request = RequestFactory().get(self.url)
  64. self.validate_schema(request, response)