0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-24 18:07:17 +01:00
posthog/ee/tasks/test/subscriptions/test_subscriptions_utils.py
Ben White 1484b846ce
feat(subscriptions): Add Slack Subscriptions (#10388)
* Added slack configuration options
* Added slack selection UI
2022-06-24 10:29:45 +02:00

55 lines
2.4 KiB
Python

from typing import List
from unittest.mock import MagicMock, patch
import pytest
from ee.tasks.subscriptions.subscription_utils import DEFAULT_MAX_ASSET_COUNT, generate_assets
from ee.tasks.test.subscriptions.subscriptions_test_factory import create_subscription
from posthog.models.dashboard import Dashboard
from posthog.models.dashboard_tile import DashboardTile
from posthog.models.exported_asset import ExportedAsset
from posthog.models.insight import Insight
from posthog.test.base import APIBaseTest
@patch("ee.tasks.subscriptions.subscription_utils.group")
@patch("ee.tasks.subscriptions.subscription_utils.export_task")
class TestSubscriptionsTasksUtils(APIBaseTest):
dashboard: Dashboard
insight: Insight
asset: ExportedAsset
tiles: List[DashboardTile]
def setUp(self) -> None:
self.dashboard = Dashboard.objects.create(team=self.team, name="private dashboard", created_by=self.user)
self.insight = Insight.objects.create(team=self.team, short_id="123456", name="My Test subscription")
self.tiles = []
for _ in range(10):
self.tiles.append(DashboardTile.objects.create(dashboard=self.dashboard, insight=self.insight))
self.subscription = create_subscription(team=self.team, insight=self.insight, created_by=self.user)
def test_generate_assets_for_insight(self, mock_export_task: MagicMock, mock_group: MagicMock) -> None:
insights, assets = generate_assets(self.subscription)
assert insights == [self.insight]
assert len(assets) == 1
assert mock_export_task.s.call_count == 1
def test_generate_assets_for_dashboard(self, mock_export_task: MagicMock, mock_group: MagicMock) -> None:
subscription = create_subscription(team=self.team, dashboard=self.dashboard, created_by=self.user)
insights, assets = generate_assets(subscription)
assert len(insights) == len(self.tiles)
assert len(assets) == DEFAULT_MAX_ASSET_COUNT
assert mock_export_task.s.call_count == DEFAULT_MAX_ASSET_COUNT
def test_raises_if_missing_resource(self, mock_export_task: MagicMock, mock_group: MagicMock) -> None:
subscription = create_subscription(team=self.team, created_by=self.user)
with pytest.raises(Exception) as e:
generate_assets(subscription)
assert str(e.value) == "There are no insights to be sent for this Subscription"