|
40 | 40 | ) |
41 | 41 | from sentry.tasks.summaries.weekly_reports import ( |
42 | 42 | OrganizationReportBatch, |
| 43 | + _pct_change, |
43 | 44 | date_format, |
44 | 45 | group_status_to_color, |
45 | 46 | prepare_organization_report, |
@@ -1536,3 +1537,123 @@ def test_enhanced_privacy_email_does_not_contain_sensitive_data(self) -> None: |
1536 | 1537 | assert "sensitive error title xyz123" not in html |
1537 | 1538 | assert "enhanced privacy" in html.lower() |
1538 | 1539 | assert "Total Project Errors" in html |
| 1540 | + |
| 1541 | + @with_feature("organizations:weekly-report-week-over-week-metric") |
| 1542 | + @mock.patch("sentry.tasks.summaries.weekly_reports.MessageBuilder") |
| 1543 | + def test_pct_change_with_previous_week(self, message_builder: mock.MagicMock) -> None: |
| 1544 | + user = self.create_user() |
| 1545 | + self.create_member(teams=[self.team], user=user, organization=self.organization) |
| 1546 | + |
| 1547 | + self.store_event_outcomes( |
| 1548 | + self.organization.id, self.project.id, self.three_days_ago, num_times=10 |
| 1549 | + ) |
| 1550 | + self.store_event_outcomes( |
| 1551 | + self.organization.id, |
| 1552 | + self.project.id, |
| 1553 | + self.three_days_ago, |
| 1554 | + num_times=20, |
| 1555 | + category=DataCategory.TRANSACTION, |
| 1556 | + ) |
| 1557 | + |
| 1558 | + prev_week = self.three_days_ago - timedelta(days=7) |
| 1559 | + self.store_event_outcomes(self.organization.id, self.project.id, prev_week, num_times=5) |
| 1560 | + self.store_event_outcomes( |
| 1561 | + self.organization.id, |
| 1562 | + self.project.id, |
| 1563 | + prev_week, |
| 1564 | + num_times=40, |
| 1565 | + category=DataCategory.TRANSACTION, |
| 1566 | + ) |
| 1567 | + |
| 1568 | + prepare_organization_report( |
| 1569 | + self.timestamp, ONE_DAY * 7, self.organization.id, self._dummy_batch_id |
| 1570 | + ) |
| 1571 | + |
| 1572 | + for call_args in message_builder.call_args_list: |
| 1573 | + context = call_args.kwargs["context"] |
| 1574 | + assert context["trends"]["error_pct_change"] == "▲ 100%" |
| 1575 | + assert context["trends"]["transaction_pct_change"] == "▼ 50%" |
| 1576 | + assert context["show_week_over_week_metric"] is True |
| 1577 | + |
| 1578 | + @with_feature("organizations:weekly-report-week-over-week-metric") |
| 1579 | + @mock.patch("sentry.tasks.summaries.weekly_reports.MessageBuilder") |
| 1580 | + def test_pct_change_no_previous_week(self, message_builder: mock.MagicMock) -> None: |
| 1581 | + user = self.create_user() |
| 1582 | + self.create_member(teams=[self.team], user=user, organization=self.organization) |
| 1583 | + |
| 1584 | + self.store_event_outcomes( |
| 1585 | + self.organization.id, self.project.id, self.three_days_ago, num_times=10 |
| 1586 | + ) |
| 1587 | + |
| 1588 | + prepare_organization_report( |
| 1589 | + self.timestamp, ONE_DAY * 7, self.organization.id, self._dummy_batch_id |
| 1590 | + ) |
| 1591 | + |
| 1592 | + for call_args in message_builder.call_args_list: |
| 1593 | + context = call_args.kwargs["context"] |
| 1594 | + assert context["trends"]["error_pct_change"] is None |
| 1595 | + assert context["trends"]["transaction_pct_change"] is None |
| 1596 | + assert context["show_week_over_week_metric"] is True |
| 1597 | + |
| 1598 | + @mock.patch("sentry.tasks.summaries.weekly_reports.MessageBuilder") |
| 1599 | + def test_pct_change_hidden_without_feature_flag(self, message_builder: mock.MagicMock) -> None: |
| 1600 | + user = self.create_user() |
| 1601 | + self.create_member(teams=[self.team], user=user, organization=self.organization) |
| 1602 | + |
| 1603 | + self.store_event_outcomes( |
| 1604 | + self.organization.id, self.project.id, self.three_days_ago, num_times=10 |
| 1605 | + ) |
| 1606 | + |
| 1607 | + prev_week = self.three_days_ago - timedelta(days=7) |
| 1608 | + self.store_event_outcomes(self.organization.id, self.project.id, prev_week, num_times=5) |
| 1609 | + |
| 1610 | + prepare_organization_report( |
| 1611 | + self.timestamp, ONE_DAY * 7, self.organization.id, self._dummy_batch_id |
| 1612 | + ) |
| 1613 | + |
| 1614 | + for call_args in message_builder.call_args_list: |
| 1615 | + context = call_args.kwargs["context"] |
| 1616 | + assert context["trends"]["error_pct_change"] is None |
| 1617 | + assert context["trends"]["transaction_pct_change"] is None |
| 1618 | + assert context["show_week_over_week_metric"] is False |
| 1619 | + |
| 1620 | + @with_feature("organizations:weekly-report-week-over-week-metric") |
| 1621 | + @mock.patch("sentry.tasks.summaries.weekly_reports.MessageBuilder") |
| 1622 | + def test_pct_change_from_cache(self, message_builder: mock.MagicMock) -> None: |
| 1623 | + from sentry.tasks.summaries.weekly_report_cache import cache_project_metrics |
| 1624 | + |
| 1625 | + user = self.create_user() |
| 1626 | + self.create_member(teams=[self.team], user=user, organization=self.organization) |
| 1627 | + |
| 1628 | + self.store_event_outcomes( |
| 1629 | + self.organization.id, self.project.id, self.three_days_ago, num_times=10 |
| 1630 | + ) |
| 1631 | + self.store_event_outcomes( |
| 1632 | + self.organization.id, |
| 1633 | + self.project.id, |
| 1634 | + self.three_days_ago, |
| 1635 | + num_times=20, |
| 1636 | + category=DataCategory.TRANSACTION, |
| 1637 | + ) |
| 1638 | + |
| 1639 | + cache_project_metrics( |
| 1640 | + self.organization.id, |
| 1641 | + {self.project.id: {"e": 5, "t": 40}}, |
| 1642 | + ) |
| 1643 | + |
| 1644 | + prepare_organization_report( |
| 1645 | + self.timestamp, ONE_DAY * 7, self.organization.id, self._dummy_batch_id |
| 1646 | + ) |
| 1647 | + |
| 1648 | + for call_args in message_builder.call_args_list: |
| 1649 | + context = call_args.kwargs["context"] |
| 1650 | + assert context["trends"]["error_pct_change"] == "▲ 100%" |
| 1651 | + assert context["trends"]["transaction_pct_change"] == "▼ 50%" |
| 1652 | + |
| 1653 | + def test_pct_change_helper(self) -> None: |
| 1654 | + assert _pct_change(150, 100) == "▲ 50%" |
| 1655 | + assert _pct_change(50, 100) == "▼ 50%" |
| 1656 | + assert _pct_change(0, 100) == "▼ 100%" |
| 1657 | + assert _pct_change(100, 0) is None |
| 1658 | + assert _pct_change(0, 0) is None |
| 1659 | + assert _pct_change(100, 100) is None |
0 commit comments