Skip to content

Commit

Permalink
feat: add new function to help pre format a list of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
gcharest authored May 15, 2024
1 parent 1f45299 commit 13c8458
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
137 changes: 137 additions & 0 deletions app/tests/utils/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pytest
from utils import filters


Expand Down Expand Up @@ -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": "[email protected]",
}
],
}
]

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": "[email protected]",
}
],
"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": "[email protected]",
}
],
}
]
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": "[email protected]",
}
],
"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": "[email protected]",
}
],
"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": "[email protected]",
}
],
"group_membership_id": {
"id": "PREFIX-group_membership_id1",
"name": "PREFIX-group_membership_name1",
},
"group_name": "new-group_membership_name1",
}
]
11 changes: 11 additions & 0 deletions app/utils/filters.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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

0 comments on commit 13c8458

Please sign in to comment.