From cc4afb3b0fcf27a1d40837b8c1167f1a182b9d17 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Mon, 16 Dec 2024 15:17:38 +0330 Subject: [PATCH 1/2] feat: Added test cases for null string discourse user handling! --- .../discourse/transform_raw_members.py | 2 +- .../test_discourse_transform_raw_members.py | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dags/analyzer_helper/discourse/transform_raw_members.py b/dags/analyzer_helper/discourse/transform_raw_members.py index 0abce570..288b1560 100644 --- a/dags/analyzer_helper/discourse/transform_raw_members.py +++ b/dags/analyzer_helper/discourse/transform_raw_members.py @@ -53,7 +53,7 @@ def transform_member(self, member: dict) -> dict: "left_at": None, "joined_at": self.converter.from_date_string(member.get("joined_at")), "options": { - "name": member["name"] if member["name"] is not "null" else None, + "name": member["name"] if member["name"] != "null" else None, "username": member["username"], "avatar": avatar, }, diff --git a/dags/analyzer_helper/tests/unit/test_discourse_transform_raw_members.py b/dags/analyzer_helper/tests/unit/test_discourse_transform_raw_members.py index 2f3afb38..8861e9e7 100644 --- a/dags/analyzer_helper/tests/unit/test_discourse_transform_raw_members.py +++ b/dags/analyzer_helper/tests/unit/test_discourse_transform_raw_members.py @@ -46,6 +46,36 @@ def test_transform_single_member(self): result = self.transformer.transform([raw_member]) self.assertEqual(result, expected_result) + def test_transform_single_member_null_name(self): + """ + Tests that transform correctly transforms a single member + """ + raw_member = { + "id": 85149, + "joined_at": "2023-07-02", + "isBot": False, + "name": "null", + "username": "memberUser1", + "avatar": "/path1/{size}/endpoint", + } + + expected_result = [ + { + "id": "85149", + "is_bot": False, + "left_at": None, + "joined_at": datetime(2023, 7, 2, 0, 0, 0, 0, tzinfo=timezone.utc), + "options": { + "name": None, + "username": "memberUser1", + "avatar": "https://" + self.endpoint + "/path1/128/endpoint", + }, + } + ] + + result = self.transformer.transform([raw_member]) + self.assertEqual(result, expected_result) + def test_transform_multiple_members(self): """ Tests that transform correctly transforms multiple members From 3c271a99c46a91ed1ca2f3f01fe60e0656175533 Mon Sep 17 00:00:00 2001 From: Mohammad Amin Date: Mon, 16 Dec 2024 15:36:24 +0330 Subject: [PATCH 2/2] feat: handling the null values for members joined_at! --- dags/analyzer_helper/discourse/transform_raw_members.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/dags/analyzer_helper/discourse/transform_raw_members.py b/dags/analyzer_helper/discourse/transform_raw_members.py index 288b1560..8943166c 100644 --- a/dags/analyzer_helper/discourse/transform_raw_members.py +++ b/dags/analyzer_helper/discourse/transform_raw_members.py @@ -38,7 +38,9 @@ def transform_member(self, member: dict) -> dict: avatar_template: str = member["avatar"] avatar: str - if avatar_template.startswith(("http://", "https://")): + if avatar_template is not None and avatar_template.startswith( + ("http://", "https://") + ): avatar = avatar_template elif avatar_template is not None: avatar = "https://" + self.endpoint + avatar_template @@ -46,12 +48,15 @@ def transform_member(self, member: dict) -> dict: avatar = None avatar = avatar.replace("{size}", "128") if avatar else None + joined_at = member.get("joined_at") member = { "id": str(int(discourse_id)) if discourse_id is not None else None, "is_bot": member.get("isBot", False), "left_at": None, - "joined_at": self.converter.from_date_string(member.get("joined_at")), + "joined_at": ( + self.converter.from_date_string(joined_at) if joined_at else None + ), "options": { "name": member["name"] if member["name"] != "null" else None, "username": member["username"],