-
Notifications
You must be signed in to change notification settings - Fork 696
Expand file tree
/
Copy pathtest_tasks.py
More file actions
200 lines (160 loc) · 6.48 KB
/
Copy pathtest_tasks.py
File metadata and controls
200 lines (160 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Unit tests for Dashboard Metrics Celery tasks."""
from datetime import datetime, timedelta
from django.test import TestCase, TransactionTestCase
from django.utils import timezone
from account_v2.models import Organization
from dashboard_metrics.models import (
EventMetricsDaily,
EventMetricsHourly,
MetricType,
)
from dashboard_metrics.tasks import (
_truncate_to_day,
_truncate_to_hour,
_truncate_to_month,
cleanup_daily_metrics,
cleanup_hourly_metrics,
)
class TestTimeHelpers(TestCase):
"""Tests for time truncation helper functions."""
def test_truncate_to_hour_from_timestamp(self):
"""Test truncating a Unix timestamp to the hour."""
# 2024-01-15 14:35:22 UTC
timestamp = 1705329322.0
result = _truncate_to_hour(timestamp)
assert result.hour == 14
assert result.minute == 0
assert result.second == 0
assert result.microsecond == 0
assert result.tzinfo == timezone.utc
def test_truncate_to_hour_from_datetime(self):
"""Test truncating a datetime to the hour."""
dt = datetime(2024, 1, 15, 14, 35, 22, tzinfo=timezone.utc)
result = _truncate_to_hour(dt)
assert result.hour == 14
assert result.minute == 0
assert result.second == 0
assert result.microsecond == 0
def test_truncate_to_hour_naive_datetime(self):
"""Test truncating a naive datetime makes it aware."""
dt = datetime(2024, 1, 15, 14, 35, 22)
result = _truncate_to_hour(dt)
assert result.tzinfo is not None
assert result.hour == 14
assert result.minute == 0
def test_truncate_to_day(self):
"""Test truncating a datetime to midnight."""
dt = datetime(2024, 1, 15, 14, 35, 22, tzinfo=timezone.utc)
result = _truncate_to_day(dt)
assert result.day == 15
assert result.hour == 0
assert result.minute == 0
assert result.second == 0
assert result.microsecond == 0
def test_truncate_to_month(self):
"""Test truncating a datetime to first day of month."""
dt = datetime(2024, 1, 15, 14, 35, 22, tzinfo=timezone.utc)
result = _truncate_to_month(dt)
assert result.day == 1
assert result.hour == 0
assert result.minute == 0
assert result.second == 0
assert result.microsecond == 0
class TestCleanupTasks(TransactionTestCase):
"""Tests for cleanup tasks."""
def setUp(self):
"""Set up test fixtures."""
# organization FK targets Organization's int PK, not a UUID.
self.org = Organization.objects.create(
organization_id="test-org", name="test-org", display_name="Test Org"
)
def test_cleanup_hourly_metrics_deletes_old_records(self):
"""Test that cleanup deletes hourly records older than retention."""
now = timezone.now()
old_timestamp = now - timedelta(days=35) # Older than 30 days
recent_timestamp = now - timedelta(days=5) # Within retention
# Create old record
EventMetricsHourly.objects.create(
organization=self.org,
timestamp=old_timestamp,
metric_name="old_metric",
metric_type=MetricType.COUNTER,
metric_value=10,
metric_count=1,
project="default",
)
# Create recent record
EventMetricsHourly.objects.create(
organization=self.org,
timestamp=recent_timestamp,
metric_name="recent_metric",
metric_type=MetricType.COUNTER,
metric_value=20,
metric_count=1,
project="default",
)
result = cleanup_hourly_metrics(retention_days=30)
assert result["success"] is True
assert result["deleted"] == 1
assert result["retention_days"] == 30
# _base_manager bypasses the org-scoped default manager, which filters
# by UserContext.get_organization() — None here, so .objects sees nothing.
assert not EventMetricsHourly._base_manager.filter(metric_name="old_metric").exists()
assert EventMetricsHourly._base_manager.filter(metric_name="recent_metric").exists()
def test_cleanup_daily_metrics_deletes_old_records(self):
"""Test that cleanup deletes daily records older than retention."""
now = timezone.now()
old_date = (now - timedelta(days=400)).date() # Older than 365 days
recent_date = (now - timedelta(days=30)).date() # Within retention
# Create old record
EventMetricsDaily.objects.create(
organization=self.org,
date=old_date,
metric_name="old_daily_metric",
metric_type=MetricType.COUNTER,
metric_value=100,
metric_count=10,
project="default",
)
# Create recent record
EventMetricsDaily.objects.create(
organization=self.org,
date=recent_date,
metric_name="recent_daily_metric",
metric_type=MetricType.COUNTER,
metric_value=200,
metric_count=20,
project="default",
)
result = cleanup_daily_metrics(retention_days=365)
assert result["success"] is True
assert result["deleted"] == 1
# Verify old is deleted, recent remains
assert not EventMetricsDaily._base_manager.filter(
metric_name="old_daily_metric"
).exists()
assert EventMetricsDaily._base_manager.filter(
metric_name="recent_daily_metric"
).exists()
def test_cleanup_hourly_with_custom_retention(self):
"""Test cleanup with custom retention period."""
now = timezone.now()
old_timestamp = now - timedelta(days=10)
EventMetricsHourly.objects.create(
organization=self.org,
timestamp=old_timestamp,
metric_name="custom_retention_metric",
metric_type=MetricType.COUNTER,
metric_value=10,
metric_count=1,
project="default",
)
# With 7-day retention, the 10-day-old record should be deleted
result = cleanup_hourly_metrics(retention_days=7)
assert result["success"] is True
assert result["deleted"] == 1
def test_cleanup_no_records_to_delete(self):
"""Test cleanup when there are no old records."""
result = cleanup_hourly_metrics(retention_days=30)
assert result["success"] is True
assert result["deleted"] == 0