-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_main.py
More file actions
76 lines (62 loc) · 2.23 KB
/
test_main.py
File metadata and controls
76 lines (62 loc) · 2.23 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
import logging
from copy import deepcopy
from main import merge_user_events
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
logger = logging.getLogger(__name__)
def test_session_split_and_sort():
logger.info("test_session_split_and_sort: start")
events = [
{"user_id": "u1", "ts": 2201, "type": "click", "meta": {"page": "/late"}},
{"user_id": "u1", "ts": 1000, "type": "click", "meta": {"page": "/"}},
{"user_id": "u1", "ts": 1600, "type": "view", "meta": {"page": "/home"}},
{"user_id": "u2", "ts": 1100, "type": "signup", "meta": {"plan": "pro"}},
]
sessions = merge_user_events(events)
assert [s["start_ts"] for s in sessions] == [1000, 1100, 2201]
assert sessions[0]["user_id"] == "u1"
assert sessions[1]["user_id"] == "u2"
assert sessions[2]["user_id"] == "u1"
assert sessions[0]["types"] == [
{"type": "click", "count": 1},
{"type": "view", "count": 1},
]
assert sessions[2]["types"] == [{"type": "click", "count": 1}]
logger.info("test_session_split_and_sort: done")
def test_meta_merge_and_immutability():
logger.info("test_meta_merge_and_immutability: start")
events = [
{
"user_id": "u1",
"ts": 100,
"type": "click",
"meta": {"page": "/", "nested": {"a": 1}},
},
{
"user_id": "u1",
"ts": 200,
"type": "click",
"meta": {"page": "/home", "nested": {"b": 2}},
},
{
"user_id": "u1",
"ts": 300,
"type": "view",
"meta": {"nested": {"c": 3}},
},
]
original = deepcopy(events)
sessions = merge_user_events(events)
assert events == original, "input events were mutated"
meta = sessions[0]["meta"]
assert meta == {"page": "/", "nested": {"a": 1, "b": 2, "c": 3}}
types = sessions[0]["types"]
assert types == [
{"type": "click", "count": 2},
{"type": "view", "count": 1},
]
logger.info("test_meta_merge_and_immutability: done")
if __name__ == "__main__":
logger.info("running tests")
test_session_split_and_sort()
test_meta_merge_and_immutability()
logger.info("tests passed")