Skip to content

Commit 41c7df7

Browse files
authored
ref(replays): Update test coverage to determine which event from the mock data (#96528)
Just making the test coverage a little more robust. The event type must be derived from the mock which ensures that both functions compose properly.
1 parent 836b997 commit 41c7df7

File tree

1 file changed

+109
-49
lines changed

1 file changed

+109
-49
lines changed

tests/sentry/replays/unit/test_event_parser.py

Lines changed: 109 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,13 @@ def test_parse_highlighted_events_fault_tolerance(event):
784784

785785
def test_as_trace_item_context_click_event():
786786
event = {
787+
"type": 5,
787788
"data": {
789+
"tag": "breadcrumb",
788790
"payload": {
789791
"timestamp": 1674298825.403,
792+
"type": "default",
793+
"category": "ui.click",
790794
"message": "div#hello.hello.world",
791795
"data": {
792796
"node": {
@@ -806,11 +810,11 @@ def test_as_trace_item_context_click_event():
806810
}
807811
},
808812
"url": "https://example.com/form",
809-
}
810-
}
813+
},
814+
},
811815
}
812816

813-
result = as_trace_item_context(EventType.CLICK, event)
817+
result = as_trace_item_context(which(event), event)
814818
assert result is not None
815819
assert result["timestamp"] == 1674298825.403
816820
assert result["attributes"]["category"] == "ui.click"
@@ -834,39 +838,50 @@ def test_as_trace_item_context_click_event():
834838

835839
def test_as_trace_item_context_click_event_missing_node():
836840
event = {
841+
"type": 5,
837842
"data": {
843+
"tag": "breadcrumb",
838844
"payload": {
839845
"timestamp": 1674298825.403,
846+
"type": "default",
847+
"category": "ui.click",
840848
"message": "div#hello.hello.world",
841849
"data": {},
842850
"url": "https://example.com/form",
843-
}
844-
}
851+
},
852+
},
845853
}
846854

847-
result = as_trace_item_context(EventType.CLICK, event)
855+
result = as_trace_item_context(which(event), event)
848856
assert result is None
849857

850858

851859
def test_as_trace_item_context_dead_click_event():
852860
event = {
861+
"type": 5,
853862
"data": {
863+
"tag": "breadcrumb",
854864
"payload": {
865+
"type": "default",
866+
"category": "ui.slowClickDetected",
855867
"timestamp": 1674298825.403,
856868
"message": "button.slow",
857869
"data": {
870+
"endReason": "timeout",
871+
"timeAfterClickMs": 7000,
872+
"clickCount": 3,
858873
"node": {
859874
"id": 456,
860-
"tagName": "button",
875+
"tagName": "a",
861876
"textContent": "Slow button",
862877
"attributes": {},
863-
}
878+
},
864879
},
865-
}
866-
}
880+
},
881+
},
867882
}
868883

869-
result = as_trace_item_context(EventType.DEAD_CLICK, event)
884+
result = as_trace_item_context(which(event), event)
870885
assert result is not None
871886
assert result["attributes"]["is_dead"] is True
872887
assert result["attributes"]["is_rage"] is False
@@ -875,23 +890,30 @@ def test_as_trace_item_context_dead_click_event():
875890

876891
def test_as_trace_item_context_rage_click_event():
877892
event = {
893+
"type": 5,
878894
"data": {
895+
"tag": "breadcrumb",
879896
"payload": {
897+
"type": "default",
898+
"category": "ui.slowClickDetected",
880899
"timestamp": 1674298825.403,
881-
"message": "button.rage",
900+
"message": "button.slow",
882901
"data": {
902+
"endReason": "timeout",
903+
"timeAfterClickMs": 7000,
904+
"clickCount": 5,
883905
"node": {
884-
"id": 789,
885-
"tagName": "button",
886-
"textContent": "Rage button",
906+
"id": 456,
907+
"tagName": "a",
908+
"textContent": "Slow button",
887909
"attributes": {},
888-
}
910+
},
889911
},
890-
}
891-
}
912+
},
913+
},
892914
}
893915

894-
result = as_trace_item_context(EventType.RAGE_CLICK, event)
916+
result = as_trace_item_context(which(event), event)
895917
assert result is not None
896918
assert result["attributes"]["is_dead"] is True
897919
assert result["attributes"]["is_rage"] is True
@@ -900,15 +922,20 @@ def test_as_trace_item_context_rage_click_event():
900922

901923
def test_as_trace_item_context_navigation_event():
902924
event = {
925+
"type": 5,
926+
"timestamp": 1753710793872,
903927
"data": {
928+
"tag": "breadcrumb",
904929
"payload": {
905930
"timestamp": 1674298825.0,
931+
"type": "default",
932+
"category": "navigation",
906933
"data": {"from": "/old-page", "to": "/new-page"},
907-
}
908-
}
934+
},
935+
},
909936
}
910937

911-
result = as_trace_item_context(EventType.NAVIGATION, event)
938+
result = as_trace_item_context(which(event), event)
912939
assert result is not None
913940
assert result["timestamp"] == 1674298825.0
914941
assert result["attributes"]["category"] == "navigation"
@@ -919,15 +946,20 @@ def test_as_trace_item_context_navigation_event():
919946

920947
def test_as_trace_item_context_navigation_event_missing_optional_fields():
921948
event = {
949+
"type": 5,
950+
"timestamp": 1753710793872,
922951
"data": {
952+
"tag": "breadcrumb",
923953
"payload": {
924954
"timestamp": 1674298825.0,
955+
"type": "default",
956+
"category": "navigation",
925957
"data": {},
926-
}
927-
}
958+
},
959+
},
928960
}
929961

930-
result = as_trace_item_context(EventType.NAVIGATION, event)
962+
result = as_trace_item_context(which(event), event)
931963
assert result is not None
932964
assert result["attributes"]["category"] == "navigation"
933965
assert "from" not in result["attributes"]
@@ -937,8 +969,12 @@ def test_as_trace_item_context_navigation_event_missing_optional_fields():
937969

938970
def test_as_trace_item_context_resource_fetch_event():
939971
event = {
972+
"type": 5,
973+
"timestamp": 1753710794.0346,
940974
"data": {
975+
"tag": "performanceSpan",
941976
"payload": {
977+
"op": "resource.fetch",
942978
"startTimestamp": 1674298825.0,
943979
"endTimestamp": 1674298825.0,
944980
"description": "https://sentry.io/",
@@ -948,11 +984,11 @@ def test_as_trace_item_context_resource_fetch_event():
948984
"method": "GET",
949985
"statusCode": 200,
950986
},
951-
}
952-
}
987+
},
988+
},
953989
}
954990

955-
result = as_trace_item_context(EventType.RESOURCE_FETCH, event)
991+
result = as_trace_item_context(which(event), event)
956992
assert result is not None
957993
assert result["timestamp"] == 1674298825.0
958994
assert result["attributes"]["category"] == "resource.fetch"
@@ -964,8 +1000,12 @@ def test_as_trace_item_context_resource_fetch_event():
9641000

9651001
def test_as_trace_item_context_resource_xhr_event():
9661002
event = {
1003+
"type": 5,
1004+
"timestamp": 1753710794.0346,
9671005
"data": {
1006+
"tag": "performanceSpan",
9681007
"payload": {
1008+
"op": "resource.xhr",
9691009
"startTimestamp": 1674298825.0,
9701010
"endTimestamp": 1674298825.0,
9711011
"description": "https://sentry.io/",
@@ -975,11 +1015,11 @@ def test_as_trace_item_context_resource_xhr_event():
9751015
"request": {"size": 512},
9761016
"response": {"size": 1024},
9771017
},
978-
}
979-
}
1018+
},
1019+
},
9801020
}
9811021

982-
result = as_trace_item_context(EventType.RESOURCE_XHR, event)
1022+
result = as_trace_item_context(which(event), event)
9831023
assert result is not None
9841024
assert result["attributes"]["category"] == "resource.xhr"
9851025
assert result["attributes"]["request_size"] == 512
@@ -1011,8 +1051,12 @@ def test_as_trace_item_context_resource_no_sizes():
10111051

10121052
def test_as_trace_item_context_resource_script_event():
10131053
event = {
1054+
"type": 5,
1055+
"timestamp": 1753710794.0346,
10141056
"data": {
1057+
"tag": "performanceSpan",
10151058
"payload": {
1059+
"op": "resource.script",
10161060
"startTimestamp": 1674298825.0,
10171061
"endTimestamp": 1674298825.0,
10181062
"description": "https://sentry.io/",
@@ -1022,11 +1066,11 @@ def test_as_trace_item_context_resource_script_event():
10221066
"decodedBodySize": 45,
10231067
"encodedBodySize": 55,
10241068
},
1025-
}
1026-
}
1069+
},
1070+
},
10271071
}
10281072

1029-
result = as_trace_item_context(EventType.RESOURCE_SCRIPT, event)
1073+
result = as_trace_item_context(which(event), event)
10301074
assert result is not None
10311075
assert result["attributes"]["category"] == "resource.script"
10321076
assert result["attributes"]["size"] == 10
@@ -1039,8 +1083,12 @@ def test_as_trace_item_context_resource_script_event():
10391083

10401084
def test_as_trace_item_context_resource_image_event():
10411085
event = {
1086+
"type": 5,
1087+
"timestamp": 1753710794.0346,
10421088
"data": {
1089+
"tag": "performanceSpan",
10431090
"payload": {
1091+
"op": "resource.img",
10441092
"startTimestamp": 1674298825.0,
10451093
"endTimestamp": 1674298825.0,
10461094
"description": "https://sentry.io/",
@@ -1050,11 +1098,11 @@ def test_as_trace_item_context_resource_image_event():
10501098
"decodedBodySize": 45,
10511099
"encodedBodySize": 55,
10521100
},
1053-
}
1054-
}
1101+
},
1102+
},
10551103
}
10561104

1057-
result = as_trace_item_context(EventType.RESOURCE_IMAGE, event)
1105+
result = as_trace_item_context(which(event), event)
10581106
assert result is not None
10591107
assert result["attributes"]["category"] == "resource.img"
10601108
assert result["attributes"]["size"] == 10
@@ -1067,16 +1115,21 @@ def test_as_trace_item_context_resource_image_event():
10671115

10681116
def test_as_trace_item_context_lcp_event():
10691117
event = {
1118+
"type": 5,
1119+
"timestamp": 1753712471.43,
10701120
"data": {
1121+
"tag": "performanceSpan",
10711122
"payload": {
1123+
"op": "web-vital",
1124+
"description": "largest-contentful-paint",
10721125
"startTimestamp": 1674298825.0,
10731126
"endTimestamp": 1674298825.0,
10741127
"data": {"rating": "good", "size": 1024, "value": 1500},
1075-
}
1076-
}
1128+
},
1129+
},
10771130
}
10781131

1079-
result = as_trace_item_context(EventType.LCP, event)
1132+
result = as_trace_item_context(which(event), event)
10801133
assert result is not None
10811134
assert result["timestamp"] == 1674298825.0
10821135
assert result["attributes"]["category"] == "web-vital.lcp"
@@ -1089,16 +1142,21 @@ def test_as_trace_item_context_lcp_event():
10891142

10901143
def test_as_trace_item_context_fcp_event():
10911144
event = {
1145+
"type": 5,
1146+
"timestamp": 1753712471.43,
10921147
"data": {
1148+
"tag": "performanceSpan",
10931149
"payload": {
1150+
"op": "web-vital",
1151+
"description": "first-contentful-paint",
10941152
"startTimestamp": 1674298825.0,
10951153
"endTimestamp": 1674298825.0,
10961154
"data": {"rating": "needs-improvement", "size": 512, "value": 2000},
1097-
}
1098-
}
1155+
},
1156+
},
10991157
}
11001158

1101-
result = as_trace_item_context(EventType.FCP, event)
1159+
result = as_trace_item_context(which(event), event)
11021160
assert result is not None
11031161
assert result["attributes"]["category"] == "web-vital.fcp"
11041162
assert result["attributes"]["duration"] == 0
@@ -1131,7 +1189,7 @@ def test_as_trace_item_context_cls_event():
11311189
},
11321190
},
11331191
}
1134-
result = as_trace_item_context(EventType.CLS, event)
1192+
result = as_trace_item_context(which(event), event)
11351193
assert result is not None
11361194
assert result["attributes"]["category"] == "web-vital.cls"
11371195
assert result["attributes"]["duration"] == 0
@@ -1169,8 +1227,10 @@ def test_as_trace_item_context_mutations():
11691227

11701228
def test_as_trace_item_context_options():
11711229
event = {
1172-
"timestamp": 1674298825507,
1230+
"type": 5,
1231+
"timestamp": 1753710752516,
11731232
"data": {
1233+
"tag": "options",
11741234
"payload": {
11751235
"shouldRecordCanvas": True,
11761236
"sessionSampleRate": 0.1,
@@ -1184,13 +1244,13 @@ def test_as_trace_item_context_options():
11841244
"networkCaptureBodies": False,
11851245
"networkRequestHasHeaders": True,
11861246
"networkResponseHasHeaders": False,
1187-
}
1247+
},
11881248
},
11891249
}
11901250

1191-
result = as_trace_item_context(EventType.OPTIONS, event)
1251+
result = as_trace_item_context(which(event), event)
11921252
assert result is not None
1193-
assert result["timestamp"] == 1674298825.507 # timestamp is divided by 1000
1253+
assert result["timestamp"] == 1753710752.516 # timestamp is divided by 1000
11941254
assert result["attributes"]["category"] == "sdk.options"
11951255
assert result["attributes"]["shouldRecordCanvas"] is True
11961256
assert result["attributes"]["sessionSampleRate"] == 0.1
@@ -1229,7 +1289,7 @@ def test_as_trace_item_context_memory():
12291289
},
12301290
}
12311291

1232-
result = as_trace_item_context(EventType.MEMORY, event)
1292+
result = as_trace_item_context(which(event), event)
12331293
assert result is not None
12341294
assert result["timestamp"] == 1753467523.594
12351295
assert result["attributes"]["category"] == "memory"

0 commit comments

Comments
 (0)