-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_anomaly_exclude_metrics.py
More file actions
190 lines (164 loc) · 5.92 KB
/
test_anomaly_exclude_metrics.py
File metadata and controls
190 lines (164 loc) · 5.92 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
from datetime import datetime, timedelta
from typing import Any, Dict, List
from data_generator import DATE_FORMAT, generate_dates
from dbt_project import DbtProject
from parametrization import Parametrization
TIMESTAMP_COLUMN = "updated_at"
DBT_TEST_NAME = "elementary.column_anomalies"
DBT_TEST_ARGS = {
"timestamp_column": TIMESTAMP_COLUMN,
"column_anomalies": ["sum"],
}
@Parametrization.autodetect_parameters()
@Parametrization.case(
name="daily_buckets",
time_bucket={"period": "day", "count": 1},
dates_step=timedelta(days=1),
)
@Parametrization.case(
name="six_hour_buckets",
time_bucket={"period": "hour", "count": 6},
dates_step=timedelta(hours=6),
)
def test_exclude_specific_dates(
test_id: str, dbt_project: DbtProject, time_bucket: dict, dates_step: timedelta
):
utc_now = datetime.utcnow()
test_bucket, *training_buckets = generate_dates(
base_date=utc_now - timedelta(1), step=dates_step
)
exclude_dates = [
(utc_now - timedelta(5)).date(),
(utc_now - timedelta(3)).date(),
]
data: List[Dict[str, Any]] = [
{TIMESTAMP_COLUMN: test_bucket.strftime(DATE_FORMAT), "metric": 10}
]
data += [
{
TIMESTAMP_COLUMN: cur_bucket.strftime(DATE_FORMAT),
"metric": 1 if cur_bucket.date() not in exclude_dates else 10,
}
for cur_bucket in training_buckets
]
test_args = {**DBT_TEST_ARGS, "time_bucket": time_bucket}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, data=data, test_column="metric"
)
assert test_result["status"] == "pass"
excluded_dates_str = ", ".join(
[f"cast('{cur_date}' as date)" for cur_date in exclude_dates]
)
test_args = {
**DBT_TEST_ARGS,
"anomaly_exclude_metrics": f"metric_date in ({excluded_dates_str})",
"time_bucket": time_bucket,
}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, test_column="metric"
)
assert test_result["status"] == "fail"
def test_exclude_specific_timestamps(test_id: str, dbt_project: DbtProject):
# To avoid races, set the "custom_started_at" to the beginning of the hour
test_started_at = datetime.utcnow().replace(minute=0, second=0)
test_bucket, *training_buckets = generate_dates(
base_date=test_started_at - timedelta(hours=1),
step=timedelta(hours=1),
days_back=1,
)
excluded_buckets = [
test_started_at - timedelta(hours=22),
test_started_at - timedelta(hours=20),
]
data: List[Dict[str, Any]] = [
{TIMESTAMP_COLUMN: test_bucket.strftime(DATE_FORMAT), "metric": 10}
]
data += [
{
TIMESTAMP_COLUMN: cur_bucket.strftime(DATE_FORMAT),
"metric": 1 if cur_bucket not in excluded_buckets else 10,
}
for cur_bucket in training_buckets
]
time_bucket = {"period": "hour", "count": 1}
test_args = {**DBT_TEST_ARGS, "time_bucket": time_bucket, "days_back": 1}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, data=data, test_column="metric"
)
assert test_result["status"] == "pass"
excluded_buckets_str = ", ".join(
[
"cast('%s' as timestamp)" % cur_ts.strftime(DATE_FORMAT)
for cur_ts in excluded_buckets
]
)
test_args = {
**DBT_TEST_ARGS,
"time_bucket": time_bucket,
"days_back": 1,
"anomaly_exclude_metrics": f"metric_time_bucket in ({excluded_buckets_str})",
}
test_result = dbt_project.test(
test_id,
DBT_TEST_NAME,
test_args,
test_column="metric",
test_vars={"custom_run_started_at": test_started_at.isoformat()},
)
assert test_result["status"] == "fail"
def test_exclude_date_range(test_id: str, dbt_project: DbtProject):
utc_today = datetime.utcnow().date()
test_date, *training_dates = generate_dates(base_date=utc_today - timedelta(1))
start_date = utc_today - timedelta(6)
end_date = utc_today - timedelta(3)
data: List[Dict[str, Any]] = [
{TIMESTAMP_COLUMN: test_date.strftime(DATE_FORMAT), "metric": 10}
]
data += [
{
TIMESTAMP_COLUMN: cur_date.strftime(DATE_FORMAT),
"metric": 1 if cur_date < start_date or cur_date > end_date else 10,
}
for cur_date in training_dates
]
test_args = {**DBT_TEST_ARGS, "days_back": 30}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, data=data, test_column="metric"
)
assert test_result["status"] == "pass"
test_args = {
**DBT_TEST_ARGS,
"anomaly_exclude_metrics": f"metric_date >= cast('{start_date}' as date) and metric_date <= cast('{end_date}' as date)",
"days_back": 30,
}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, test_column="metric"
)
assert test_result["status"] == "fail"
def test_exclude_by_metric_value(test_id: str, dbt_project: DbtProject):
utc_today = datetime.utcnow().date()
test_date, *training_dates = generate_dates(base_date=utc_today - timedelta(1))
data: List[Dict[str, Any]] = [
{TIMESTAMP_COLUMN: test_date.strftime(DATE_FORMAT), "metric": 10}
]
data += [
{
TIMESTAMP_COLUMN: cur_date.strftime(DATE_FORMAT),
"metric": 1 if cur_date.day % 3 > 0 else 10,
}
for cur_date in training_dates
]
test_args = {**DBT_TEST_ARGS, "days_back": 30}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, data=data, test_column="metric"
)
assert test_result["status"] == "pass"
test_args = {
**DBT_TEST_ARGS,
"anomaly_exclude_metrics": f"metric_date < cast('{test_date}' as date) and metric_value >= 5",
"days_back": 30,
}
test_result = dbt_project.test(
test_id, DBT_TEST_NAME, test_args, test_column="metric"
)
assert test_result["status"] == "fail"