From c0852b283c9d0227c56b06cbd09ddd08fd57cd57 Mon Sep 17 00:00:00 2001 From: Gguidini Date: Wed, 19 Jul 2023 22:53:50 +0200 Subject: [PATCH] Handle string indexes with no meta These changes let `SessionTotalsArray.build_from_encoded_data` handle the creation of `SessionTotalsArray` from encoded data that has no "meta" info and the indexes of the sessions are strings (as it happens when you pull them from storage). These changes address https://codecov.sentry.io/issues/4326038619/?project=5215654&query=is%3Aunresolved&referrer=issue-stream&stream_index=0 --- shared/reports/types.py | 2 +- tests/unit/reports/test_types.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/shared/reports/types.py b/shared/reports/types.py index 418c7b0b..d1e889f2 100644 --- a/shared/reports/types.py +++ b/shared/reports/types.py @@ -223,7 +223,7 @@ def build_from_encoded_data(cls, sessions_array: Union[dict, list]): extra=dict(sessions_array=sessions_array), ) sessions_array["meta"] = { - "session_count": max(sessions_array.keys()) + 1 + "session_count": int(max(sessions_array.keys())) + 1 } meta_info = sessions_array.pop("meta") session_count = meta_info["session_count"] diff --git a/tests/unit/reports/test_types.py b/tests/unit/reports/test_types.py index edb3bbac..c73ccf8e 100644 --- a/tests/unit/reports/test_types.py +++ b/tests/unit/reports/test_types.py @@ -194,6 +194,9 @@ class TestSessionTotalsArray(object): "meta": {"session_count": 5}, "4": [0, 35, 35, 0, 0, "100", 5, 0, 0, 0, 0, 0, 0], } + encoded_obj_string_no_meta = { + "4": [0, 35, 35, 0, 0, "100", 5, 0, 0, 0, 0, 0, 0], + } def test_decode_session_totals_array_from_legacy(self): expected = SessionTotalsArray( @@ -230,6 +233,16 @@ def test_decode_session_totals_array_string_indexes(self): assert expected.session_count == result.session_count assert expected.non_null_items == result.non_null_items + def test_decode_session_totals_array_string_indexes_no_meta(self): + encoded_obj_copy = deepcopy(self.encoded_obj_string_no_meta) + expected = SessionTotalsArray( + session_count=5, + non_null_items={4: [0, 35, 35, 0, 0, "100", 5, 0, 0, 0, 0, 0, 0]}, + ) + result = SessionTotalsArray.build_from_encoded_data(encoded_obj_copy) + assert expected.session_count == result.session_count + assert expected.non_null_items == result.non_null_items + def test_decode_session_totals_array_no_meta(self): encoded_obj_copy = deepcopy(self.encoded_obj) encoded_obj_copy.pop("meta")