-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathtest_stg_ga4__session_conversions_daily.py
93 lines (78 loc) · 3.07 KB
/
test_stg_ga4__session_conversions_daily.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
import pytest
from dbt.tests.util import check_relations_equal, read_file, run_dbt
# Define mocks via CSV (seeds) or SQL (models)
mock_stg_ga4__events_csv = """session_key,session_partition_key,event_name,event_date_dt
A,A2022-01-01,page_view,2022-01-01
A,A2022-01-01,my_conversion,2022-01-01
A,A2022-01-01,my_conversion,2022-01-01
B,B2022-01-01,my_conversion,2022-01-01
C,C2022-01-01,some_other_event,2022-01-01
A,A2022-01-02,my_conversion,2022-01-02
""".lstrip()
mock_stg_ga4__nonstandard_events_csv = """session_key,session_partition_key,event_name,event_date_dt
A,A2022-01-01,page_view,2022-01-01
A,A2022-01-01,my-conversion,2022-01-01
A,A2022-01-01,my-conversion,2022-01-01
B,B2022-01-01,my-conversion,2022-01-01
C,C2022-01-01,some_other_event,2022-01-01
A,A2022-01-02,my-conversion,2022-01-02
""".lstrip()
expected_csv = """session_key,session_partition_key,session_partition_date,my_conversion_count
A,A2022-01-01,2022-01-01,2
B,B2022-01-01,2022-01-01,1
C,C2022-01-01,2022-01-01,0
A,A2022-01-02,2022-01-02,1
""".lstrip()
actual = read_file("../models/staging/stg_ga4__session_conversions_daily.sql")
class TestUsersFirstLastEvents:
# Update project name to ga4 so we can call macros with ga4.macro_name
@pytest.fixture(scope="class")
def project_config_update(self):
return {"name": "ga4", "vars": {"static_incremental_days": 3}}
# everything that goes in the "seeds" directory (= CSV format)
@pytest.fixture(scope="class")
def seeds(self):
return {
"stg_ga4__events.csv": mock_stg_ga4__events_csv,
"expected.csv": expected_csv,
}
# everything that goes in the "macros"
@pytest.fixture(scope="class")
def macros(self):
return {
"valid_column_name.sql": read_file("../macros/valid_column_name.sql"),
}
# everything that goes in the "models" directory (= SQL)
@pytest.fixture(scope="class")
def models(self):
return {
"actual.sql": actual,
}
def test_mock_run_and_check(self, project):
run_dbt(["build", "--vars", "conversion_events: ['my_conversion']"])
# breakpoint()
check_relations_equal(project.adapter, ["actual", "expected"])
class TestUsersNonStandardEventName:
# everything that goes in the "seeds" directory (= CSV format)
@pytest.fixture(scope="class")
def seeds(self):
return {
"stg_ga4__events.csv": mock_stg_ga4__nonstandard_events_csv,
"expected.csv": expected_csv,
}
# everything that goes in the "macros"
@pytest.fixture(scope="class")
def macros(self):
return {
"valid_column_name.sql": read_file("../macros/valid_column_name.sql"),
}
# everything that goes in the "models" directory (= SQL)
@pytest.fixture(scope="class")
def models(self):
return {
"actual.sql": actual,
}
def test_mock_run_and_check(self, project):
run_dbt(["build", "--vars", "conversion_events: ['my-conversion']"])
# breakpoint()
check_relations_equal(project.adapter, ["actual", "expected"])