From 13c8458d0d7cac04386c79af7ea4b721a09016c9 Mon Sep 17 00:00:00 2001 From: Guillaume Charest <1690085+gcharest@users.noreply.github.com> Date: Wed, 15 May 2024 20:20:55 +0000 Subject: [PATCH] feat: add new function to help pre format a list of objects --- app/tests/utils/test_filters.py | 137 ++++++++++++++++++++++++++++++++ app/utils/filters.py | 11 +++ 2 files changed, 148 insertions(+) diff --git a/app/tests/utils/test_filters.py b/app/tests/utils/test_filters.py index 65f384c2..2f65bfb3 100644 --- a/app/tests/utils/test_filters.py +++ b/app/tests/utils/test_filters.py @@ -1,3 +1,4 @@ +import pytest from utils import filters @@ -232,3 +233,139 @@ def test_get_unique_nested_dicts_with_duplicate_key(): assert sorted(users_from_groups, key=lambda user: user["id"]) == sorted( expected_users, key=lambda user: user["id"] ) + + +def test_preformat_items(): + items_to_format = [ + { + "id": "PREFIX-google_group_id1", + "name": "PREFIX-group-name1", + "members": [ + { + "id": "PREFIX-user_id1", + "primaryEmail": "PREFIX-user-email1@test.com", + } + ], + } + ] + + lookup_key = "name" + new_key = "DisplayName" + pattern = r"^PREFIX-" + replace = "new-" + response = filters.preformat_items( + items_to_format, lookup_key, new_key, pattern, replace + ) + + assert response == [ + { + "id": "PREFIX-google_group_id1", + "name": "PREFIX-group-name1", + "members": [ + { + "id": "PREFIX-user_id1", + "primaryEmail": "PREFIX-user-email1@test.com", + } + ], + "DisplayName": "new-group-name1", + } + ] + + +def test_preformat_items_returns_value_if_no_matching_pattern(): + items_to_format = [ + { + "id": "PREFIX-google_group_id1", + "name": "not-PREFIX-group-name1", + "members": [ + { + "id": "PREFIX-user_id1", + "primaryEmail": "PREFIX-user-email1@test.com", + } + ], + } + ] + lookup_key = "name" + new_key = "DisplayName" + pattern = r"^PREFIX-" + replace = "new-" + response = filters.preformat_items( + items_to_format, lookup_key, new_key, pattern, replace + ) + + assert response == [ + { + "id": "PREFIX-google_group_id1", + "name": "not-PREFIX-group-name1", + "members": [ + { + "id": "PREFIX-user_id1", + "primaryEmail": "PREFIX-user-email1@test.com", + } + ], + "DisplayName": "not-PREFIX-group-name1", + } + ] + + +def test_preformat_items_lookup_key_not_found_raise_error(google_groups_w_users): + items_to_format = google_groups_w_users( + n_groups=1, n_users=1, group_prefix="PREFIX-" + ) + lookup_key = "invalid_key" + new_key = "DisplayName" + pattern = "PREFIX-" + replace = "new-" + + with pytest.raises(KeyError) as exc: + filters.preformat_items(items_to_format, lookup_key, new_key, pattern, replace) + + expected_error_message = ( + f'"Item {items_to_format[0]} does not have {lookup_key} key"' + ) + assert str(exc.value) == expected_error_message + + +def test_preformat_items_with_nested_lookup_key(): + items_to_format = [ + { + "id": "PREFIX-google_group_id1", + "name": "PREFIX-group-name1", + "members": [ + { + "id": "PREFIX-user_id1", + "primaryEmail": "PREFIX-user-email1@test.com", + } + ], + "group_membership_id": { + "id": "PREFIX-group_membership_id1", + "name": "PREFIX-group_membership_name1", + }, + } + ] + + lookup_key = "group_membership_id.name" + new_key = "group_name" + pattern = r"^PREFIX-" + replace = "new-" + response = filters.preformat_items( + items_to_format, lookup_key, new_key, pattern, replace + ) + + assert response == [ + { + "id": "PREFIX-google_group_id1", + "name": "PREFIX-group-name1", + "members": [ + { + "id": "PREFIX-user_id1", + "primaryEmail": "PREFIX-user-email1@test.com", + } + ], + "group_membership_id": { + "id": "PREFIX-group_membership_id1", + "name": "PREFIX-group_membership_name1", + }, + "group_name": "new-group_membership_name1", + } + ] diff --git a/app/utils/filters.py b/app/utils/filters.py index 82e9e368..1088b568 100644 --- a/app/utils/filters.py +++ b/app/utils/filters.py @@ -1,6 +1,7 @@ """This module contains utility functions for filtering lists and dictionaries.""" from functools import reduce import logging +import re logger = logging.getLogger(__name__) @@ -133,3 +134,13 @@ def get_unique_nested_dicts(source_items, nested_key): unique_dicts[str(nested_dict)] = nested_dict logger.info(f"Found {len(unique_dicts)} unique dictionaries.") return list(unique_dicts.values()) + + +def preformat_items(items, lookup_key, new_key, pattern="", replace=""): + for item in items: + value = get_nested_value(item, lookup_key) + if not value: + raise KeyError(f"Item {item} does not have {lookup_key} key") + item[new_key] = re.sub(pattern, replace, value) + + return items