|
| 1 | +import pytest |
| 2 | +from datetime import datetime, UTC |
| 3 | +from unittest.mock import Mock, patch, call |
| 4 | + |
| 5 | +import dagster |
| 6 | +from dagster import TimeWindow |
| 7 | + |
| 8 | +from dags.web_preaggregated_daily import ( |
| 9 | + pre_aggregate_web_analytics_data, |
| 10 | +) |
| 11 | +from posthog.models.web_preaggregated.sql import DROP_PARTITION_SQL |
| 12 | + |
| 13 | + |
| 14 | +class TestPartitionHandling: |
| 15 | + def setup_method(self): |
| 16 | + self.mock_context = Mock(spec=dagster.AssetExecutionContext) |
| 17 | + self.mock_context.log = Mock() |
| 18 | + self.mock_context.op_config = {"team_ids": [1, 2], "extra_clickhouse_settings": ""} |
| 19 | + |
| 20 | + @pytest.mark.parametrize( |
| 21 | + "start_date_str,end_date_str,expected_partitions", |
| 22 | + [ |
| 23 | + # Single day partition |
| 24 | + ("2024-01-01", "2024-01-02", ["2024-01-01"]), |
| 25 | + # Two day partition |
| 26 | + ("2024-01-01", "2024-01-03", ["2024-01-01", "2024-01-02"]), |
| 27 | + # Week-long partition |
| 28 | + ( |
| 29 | + "2024-01-01", |
| 30 | + "2024-01-08", |
| 31 | + ["2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05", "2024-01-06", "2024-01-07"], |
| 32 | + ), |
| 33 | + # Month-long partition (testing edge case) |
| 34 | + ("2024-01-01", "2024-02-01", [f"2024-01-{day:02d}" for day in range(1, 32)]), |
| 35 | + ], |
| 36 | + ) |
| 37 | + @patch("dags.web_preaggregated_daily.sync_execute") |
| 38 | + def test_partition_dropping_for_different_time_windows( |
| 39 | + self, mock_sync_execute, start_date_str, end_date_str, expected_partitions |
| 40 | + ): |
| 41 | + start_datetime = datetime.fromisoformat(start_date_str).replace(tzinfo=UTC) |
| 42 | + end_datetime = datetime.fromisoformat(end_date_str).replace(tzinfo=UTC) |
| 43 | + self.mock_context.partition_time_window = TimeWindow(start_datetime, end_datetime) |
| 44 | + |
| 45 | + mock_sql_generator = Mock(return_value="INSERT INTO test_table ...") |
| 46 | + pre_aggregate_web_analytics_data( |
| 47 | + context=self.mock_context, |
| 48 | + table_name="web_stats_daily", |
| 49 | + sql_generator=mock_sql_generator, |
| 50 | + ) |
| 51 | + |
| 52 | + actual_drop_calls = [ |
| 53 | + call_args for call_args in mock_sync_execute.call_args_list if "DROP PARTITION" in str(call_args) |
| 54 | + ] |
| 55 | + assert len(actual_drop_calls) == len(expected_partitions) |
| 56 | + |
| 57 | + @patch("dags.web_preaggregated_daily.sync_execute") |
| 58 | + def test_partition_drop_error_handling(self, mock_sync_execute): |
| 59 | + start_datetime = datetime(2024, 1, 1, tzinfo=UTC) |
| 60 | + end_datetime = datetime(2024, 1, 3, tzinfo=UTC) |
| 61 | + self.mock_context.partition_time_window = TimeWindow(start_datetime, end_datetime) |
| 62 | + |
| 63 | + def side_effect(sql): |
| 64 | + if "DROP PARTITION" in sql: |
| 65 | + raise Exception("Partition doesn't exist") |
| 66 | + return None |
| 67 | + |
| 68 | + mock_sync_execute.side_effect = side_effect |
| 69 | + mock_sql_generator = Mock(return_value="INSERT INTO test_table ...") |
| 70 | + |
| 71 | + pre_aggregate_web_analytics_data( |
| 72 | + context=self.mock_context, |
| 73 | + table_name="web_stats_daily", |
| 74 | + sql_generator=mock_sql_generator, |
| 75 | + ) |
| 76 | + |
| 77 | + assert self.mock_context.log.info.call_count >= 4 |
| 78 | + insert_calls = [call_args for call_args in mock_sync_execute.call_args_list if "INSERT INTO" in str(call_args)] |
| 79 | + assert len(insert_calls) == 1 |
| 80 | + |
| 81 | + @patch("dags.web_preaggregated_daily.sync_execute") |
| 82 | + def test_granularity_parameter_usage(self, mock_sync_execute): |
| 83 | + start_datetime = datetime(2024, 1, 1, tzinfo=UTC) |
| 84 | + end_datetime = datetime(2024, 1, 2, tzinfo=UTC) |
| 85 | + self.mock_context.partition_time_window = TimeWindow(start_datetime, end_datetime) |
| 86 | + |
| 87 | + mock_sql_generator = Mock(return_value="INSERT INTO test_table ...") |
| 88 | + pre_aggregate_web_analytics_data( |
| 89 | + context=self.mock_context, |
| 90 | + table_name="web_stats_daily", |
| 91 | + sql_generator=mock_sql_generator, |
| 92 | + ) |
| 93 | + |
| 94 | + expected_sql = DROP_PARTITION_SQL("web_stats_daily", "2024-01-01", granularity="daily") |
| 95 | + assert call(expected_sql) in mock_sync_execute.call_args_list |
| 96 | + assert "'20240101'" in expected_sql |
| 97 | + |
| 98 | + def test_missing_partition_time_window_raises_error(self): |
| 99 | + self.mock_context.partition_time_window = None |
| 100 | + mock_sql_generator = Mock() |
| 101 | + |
| 102 | + with pytest.raises(dagster.Failure, match="This asset should only be run with a partition_time_window"): |
| 103 | + pre_aggregate_web_analytics_data( |
| 104 | + context=self.mock_context, |
| 105 | + table_name="web_stats_daily", |
| 106 | + sql_generator=mock_sql_generator, |
| 107 | + ) |
| 108 | + |
| 109 | + @patch("dags.web_preaggregated_daily.sync_execute") |
| 110 | + def test_insert_query_failure_raises_dagster_failure(self, mock_sync_execute): |
| 111 | + start_datetime = datetime(2024, 1, 1, tzinfo=UTC) |
| 112 | + end_datetime = datetime(2024, 1, 2, tzinfo=UTC) |
| 113 | + self.mock_context.partition_time_window = TimeWindow(start_datetime, end_datetime) |
| 114 | + |
| 115 | + def side_effect(sql): |
| 116 | + if "INSERT INTO" in sql: |
| 117 | + raise Exception("Insert failed") |
| 118 | + return None |
| 119 | + |
| 120 | + mock_sync_execute.side_effect = side_effect |
| 121 | + mock_sql_generator = Mock(return_value="INSERT INTO test_table ...") |
| 122 | + |
| 123 | + with pytest.raises(dagster.Failure, match="Failed to pre-aggregate web_stats_daily"): |
| 124 | + pre_aggregate_web_analytics_data( |
| 125 | + context=self.mock_context, |
| 126 | + table_name="web_stats_daily", |
| 127 | + sql_generator=mock_sql_generator, |
| 128 | + ) |
| 129 | + |
| 130 | + @patch("dags.web_preaggregated_daily.sync_execute") |
| 131 | + def test_same_start_and_end_date_drops_partition(self, mock_sync_execute): |
| 132 | + start_datetime = datetime(2024, 1, 1, tzinfo=UTC) |
| 133 | + end_datetime = datetime(2024, 1, 1, tzinfo=UTC) |
| 134 | + self.mock_context.partition_time_window = TimeWindow(start_datetime, end_datetime) |
| 135 | + |
| 136 | + mock_sql_generator = Mock(return_value="INSERT INTO test_table ...") |
| 137 | + pre_aggregate_web_analytics_data( |
| 138 | + context=self.mock_context, |
| 139 | + table_name="web_stats_daily", |
| 140 | + sql_generator=mock_sql_generator, |
| 141 | + ) |
| 142 | + |
| 143 | + drop_calls = [call_args for call_args in mock_sync_execute.call_args_list if "DROP PARTITION" in str(call_args)] |
| 144 | + assert len(drop_calls) == 1 |
| 145 | + |
| 146 | + insert_calls = [call_args for call_args in mock_sync_execute.call_args_list if "INSERT INTO" in str(call_args)] |
| 147 | + assert len(insert_calls) == 1 |
0 commit comments