forked from Velir/dbt-ga4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_stg_ga4__derived_session_properties.py
74 lines (65 loc) · 3.54 KB
/
test_stg_ga4__derived_session_properties.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
import pytest
from dbt.tests.util import read_file,check_relations_equal,run_dbt
mock_stg_ga4__events_json = """
{ "session_key": "AAA", "event_timestamp": "1617691790431476", "event_name": "first_visit", "event_params": [{ "key": "my_param", "value": { "string_value": null, "int_value": 1, "float_value": null, "double_value": null }}], "user_properties": [{ "key": "my_property", "value": { "string_value": "value1", "int_value": null, "float_value": null, "double_value": null }}]}
{ "session_key": "AAA", "event_timestamp": "1617691790431477", "event_name": "first_visit", "event_params": [{ "key": "my_param", "value": { "string_value": null, "int_value": 2, "float_value": null, "double_value": null }}]}
{ "session_key": "BBB", "event_timestamp": "1617691790431477", "event_name": "first_visit", "event_params": [{ "key": "my_param", "value": { "string_value": null, "int_value": 1, "float_value": null, "double_value": null }}], "user_properties": [{ "key": "my_property", "value": { "string_value": "value2", "int_value": null, "float_value": null, "double_value": null }}]}
""".lstrip()
expected_csv = """session_key,my_derived_property,my_derived_property2
AAA,2,value1
BBB,1,value2
""".lstrip()
models__config_yml = """
version: 2
sources:
- name: fixture
schema: "{{ target.schema }}"
tables:
- name: mock_stg_ga4__events_json
"""
class TestDerivedSessionProperties():
# 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"
}
# everything that goes in the "seeds" directory (= CSV format)
@pytest.fixture(scope="class")
def seeds(self):
return {
"expected.csv": expected_csv,
}
# everything that goes in the "models" directory (= SQL)
@pytest.fixture(scope="class")
def models(self):
return {
"config.yml": models__config_yml,
"stg_ga4__events.sql": "select * from {{source('fixture','mock_stg_ga4__events_json')}}",
"actual.sql": read_file('../models/staging/stg_ga4__derived_session_properties.sql')
}
# everything that goes in the "macros"
@pytest.fixture(scope="class")
def macros(self):
return {
"unnest_key.sql": read_file('../macros/unnest_key.sql'),
}
def upload_json_fixture(self, project, file_name, json, table_name):
local_file_path = file_name
with open(local_file_path, "w") as outfile:
outfile.write(json)
project.adapter.upload_file(
local_file_path = local_file_path,
database = project.database,
table_schema = project.test_schema,
table_name = table_name,
kwargs = {
"source_format": "NEWLINE_DELIMITED_JSON",
"autodetect":"true"
}
)
def test_mock_run_and_check(self, project):
self.upload_json_fixture(project, "source.json", mock_stg_ga4__events_json, "mock_stg_ga4__events_json" )
run_dbt(["build", "--vars", "derived_session_properties: [{'event_parameter':'my_param','session_property_name':'my_derived_property','value_type':'int_value'},{'user_property':'my_property','session_property_name':'my_derived_property2','value_type':'string_value'}]"])
#breakpoint()
check_relations_equal(project.adapter, ["actual", "expected"])