-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathtest_macro_default_channel_grouping.py
302 lines (291 loc) · 8.23 KB
/
test_macro_default_channel_grouping.py
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import pytest
from dbt.tests.util import read_file,check_relations_equal,run_dbt
traffic_data_with_expected_channels = [
# Direct: Source exactly matches "(direct)" AND Medium is one of ("(not set)", "(none)")
{
"source": "(direct)",
"medium": "(none)",
"campaign": "",
"expected_channel": "Direct"
},
{
"source": "(direct)",
"medium": "(not set)",
"campaign": "",
"expected_channel": "Direct"
},
# Cross-network: Campaign Name contains "cross-network"
{
"source": "some-source",
"medium": "some-medium",
"campaign": "some-cross-network-campaign",
"expected_channel": "Cross-network"
},
{
"source": "some-source",
"medium": "some-medium",
"campaign": "cross-network",
"expected_channel": "Cross-network"
},
# Paid Shopping:
# (Source matches a list of shopping sites
# OR
# Campaign Name matches regex ^(.*(([^a-df-z]|^)shop|shopping).*)$)
# AND
# Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
{
"source": "alibaba",
"medium": "",
"campaign": "",
"expected_channel": "Paid Shopping"
},
{
"source": "some-source",
"medium": "retargeting",
"campaign": "shopping",
"expected_channel": "Paid Shopping"
},
# Paid Search:
# Source matches a list of search sites
# AND
# Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
{
"source": "google",
"medium": "ppc",
"campaign": "",
"expected_channel": "Paid Search"
},
# Paid Social:
# Source matches a regex list of social sites
# AND
# Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
{
"source": "facebook",
"medium": "retargeting",
"campaign": "",
"expected_channel": "Paid Social"
},
# Paid Video:
# Source matches a list of video sites
# AND
# Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
{
"source": "youtube.com",
"medium": "paid-something",
"campaign": "",
"expected_channel": "Paid Video"
},
# Display:
# Medium is one of (“display”, “banner”, “expandable”, “interstitial”, “cpm”)
{
"source": "youtube.com",
"medium": "display",
"campaign": "",
"expected_channel": "Display"
},
# Paid Other:
# Medium matches regex ^(.*cp.*|ppc|retargeting|paid.*)$
{
"source": "some-source",
"medium": "cpc",
"campaign": "",
"expected_channel": "Paid Other"
},
# Organic Shopping:
# Source matches a list of shopping sites
# OR
# Campaign name matches regex ^(.*(([^a-df-z]|^)shop|shopping).*)$
{
"source": "Google Shopping",
"medium": "",
"campaign": "",
"expected_channel": "Organic Shopping"
},
{
"source": "some-source",
"medium": "",
"campaign": "some-shopping-campaign",
"expected_channel": "Organic Shopping"
},
# Organic Social:
# Source matches a regex list of social sites
# OR
# Medium is one of (“social”, “social-network”, “social-media”, “sm”, “social network”, “social media”)
{
"source": "facebook",
"medium": "",
"campaign": "",
"expected_channel": "Organic Social"
},
{
"source": "some-source",
"medium": "social",
"campaign": "",
"expected_channel": "Organic Social"
},
# Organic Video:
# Source matches a list of video sites
# OR
# Medium matches regex ^(.*video.*)$
{
"source": "youtube.com",
"medium": "",
"campaign": "",
"expected_channel": "Organic Video"
},
{
"source": "some-source",
"medium": "video",
"campaign": "",
"expected_channel": "Organic Video"
},
# Organic Search:
# Source matches a list of search sites
# OR
# Medium exactly matches organic
{
"source": "bing",
"medium": "",
"campaign": "",
"expected_channel": "Organic Search"
},
{
"source": "some-source",
"medium": "organic",
"campaign": "",
"expected_channel": "Organic Search"
},
# Referral:
# Medium is one of ("referral", "app", or "link")
{
"source": "some-source",
"medium": "referral",
"campaign": "",
"expected_channel": "Referral"
},
# Email:
# Source = email|e-mail|e_mail|e mail
# OR
# Medium = email|e-mail|e_mail|e mail
{
"source": "email",
"medium": "",
"campaign": "",
"expected_channel": "Email"
},
{
"source": "",
"medium": "e mail",
"campaign": "",
"expected_channel": "Email"
},
# Affiliates:
# Medium = affiliate
{
"source": "some-source",
"medium": "affiliate",
"campaign": "",
"expected_channel": "Affiliates"
},
# Audio:
# Medium exactly matches audio
{
"source": "some-source",
"medium": "audio",
"campaign": "",
"expected_channel": "Audio"
},
# SMS:
# Source exactly matches sms
# OR
# Medium exactly matches sms
{
"source": "sms",
"medium": "",
"campaign": "",
"expected_channel": "SMS"
},
{
"source": "",
"medium": "sms",
"campaign": "",
"expected_channel": "SMS"
},
# Mobile Push Notifications:
# Medium ends with "push"
# OR
# Medium contains "mobile" or "notification"
# OR
# Source exactly matches "firebase"
{
"source": "some-source",
"medium": "something-push",
"campaign": "",
"expected_channel": "Mobile Push Notifications"
},
{
"source": "some-source",
"medium": "mobile-notification",
"campaign": "",
"expected_channel": "Mobile Push Notifications"
},
{
"source": "firebase",
"medium": "",
"campaign": "",
"expected_channel": "Mobile Push Notifications"
},
# Unassigned is the value Analytics uses when there are no other channel rules that match the event data.
{
"source": "some-source",
"medium": "some-medium",
"campaign": "some-campaign",
"expected_channel": "Unassigned"
},
]
# Generate the input CSV content and the expected CSV content
csv_header = "source,medium,campaign"
expected_header = "default_channel_grouping"
traffic_input_lines = [csv_header] + [
f"{row['source']},{row['medium']},{row['campaign']}" for row in traffic_data_with_expected_channels
]
expected_csv_lines = [expected_header] + [
row['expected_channel'] for row in traffic_data_with_expected_channels
]
# Join the lines into a single string for input and expected CSV
traffic_input = "\n".join(traffic_input_lines)
expected_csv = "\n".join(expected_csv_lines)
actual = """
with input as (
select * from {{ref('traffic_input')}}
left join {{ref('source_category_mapping')}} using (source)
)
select
{{default_channel_grouping('source', 'medium', 'source_category','campaign')}} as default_channel_grouping
from input
"""
class TestDefaultChannelGrouping():
# everything that goes in the "seeds" directory (= CSV format)
@pytest.fixture(scope="class")
def seeds(self):
return {
"source_category_mapping.csv": read_file('../seeds/ga4_source_categories.csv'),
"traffic_input.csv": traffic_input,
"expected.csv": expected_csv,
}
# everything that goes in the "models" directory (= SQL)
@pytest.fixture(scope="class")
def models(self):
return {
"actual.sql": actual,
}
# everything that goes in the "macros"
@pytest.fixture(scope="class")
def macros(self):
return {
"macro_to_test.sql": read_file('../macros/default_channel_grouping.sql'),
}
def test_mock_run_and_check(self, project):
#breakpoint()
run_dbt(["build"])
check_relations_equal(project.adapter, ["actual", "expected"])