Skip to content

Commit

Permalink
Fix/obfuscation application (#62)
Browse files Browse the repository at this point in the history
* Add deepcopy

* Add a test

* Fix tests
  • Loading branch information
AndyRae authored Jan 27, 2025
1 parent 99bf8ae commit ca4efba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/hutch_bunny/core/obfuscation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from copy import deepcopy


def low_number_suppression(value: int | float, threshold: int = 10) -> int | float:
"""Suppress values that fall below a given threshold.
Expand Down Expand Up @@ -39,6 +42,8 @@ def rounding(value: int | float, nearest: int = 10) -> int:
def apply_filters(value: int | float, filters: list) -> int | float:
"""Iterate over a list of filters and apply them to the supplied value.
Makes a deep copy of the filters list to avoid mutating the original list.
Args:
value (int | float): The value to be filtered.
filters (list): The filters applied to the value.
Expand All @@ -48,7 +53,8 @@ def apply_filters(value: int | float, filters: list) -> int | float:
"""
actions = {"Low Number Suppression": low_number_suppression, "Rounding": rounding}
result = value
for f in filters:
filters_copy = deepcopy(filters)
for f in filters_copy:
if action := actions.get(f.pop("id", None)):
result = action(result, **f)
if result == 0:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_obfuscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
low_number_suppression,
rounding,
)
from copy import deepcopy


def test_low_number_suppression():
Expand Down Expand Up @@ -64,3 +65,25 @@ def test_apply_filters_combined_leak():
def test_apply_filters_combined_empty_filter():
# Test that an empty filter list returns the original value
assert apply_filters(9, []) == 9


def test_apply_filters_preserves_original_filters():
# Test that the original filters list is not modified
original_filters = [
{"id": "Low Number Suppression", "threshold": 100},
{"id": "Rounding", "nearest": 100},
]

# Make a deep copy to compare
filters_before = deepcopy(original_filters)

# Apply filters in a loop to simulate daemon behavior
for _ in range(3):
result = apply_filters(123, filters=original_filters)
# Verify each iteration still produces the expected result
assert result == 100
# Verify filters still contain their 'id' keys after each iteration
assert all('id' in f for f in original_filters)

# Verify the original filters list remains completely unchanged
assert original_filters == filters_before

0 comments on commit ca4efba

Please sign in to comment.