generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new function to help pre format a list of objects
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": "[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", | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters