1
0
mirror of https://github.com/EDCD/EDMarketConnector.git synced 2025-04-13 07:47:14 +03:00

made precedence of operators a part of the API

This commit is contained in:
A_D 2021-08-14 13:41:01 +02:00
parent 581fa05143
commit 710807f585
No known key found for this signature in database
GPG Key ID: 4BE9EB7DF45076C4
3 changed files with 33 additions and 6 deletions

View File

@ -25,9 +25,12 @@ this to the user (for internal killswitches, anyway).
| Key (* = required) | Type | Description |
| -----------------: | :--------------: | :-------------------------------------------------------------------------------------------- |
| `reason`* | `str` | The reason that this killswitch was added |
| `delete_fields` | `List[str]` | A list of fields in the matching event to be removed, if they exist. |
| `set_fields` | `Dict[str, Any]` | A map of key -> contents to update (or overwrite) existing data with |
| `redact_fields` | `List[str]` | A list of fields to redact. This is equivalent to setting the fields to the string "REDACTED" |
| `delete_fields` | `List[str]` | A list of fields in the matching event to be removed, if they exist. |
The order listed above is the precedence for actions. i.e. All set fields are set, then all redact fields are redacted
then all delete fields are deleted.
An example follows:

View File

@ -45,8 +45,9 @@ class SingleKill(NamedTuple):
:param target: data to apply a rule to
"""
# fields that contain . might be going deeper into the dict. check here FIRST to see if they exist at the top
# level, if not, then work our way down
for key, value in (self.set_fields if self .set_fields is not None else {}).items():
_deep_apply(target, key, value)
for key in (self.redact_fields if self.redact_fields is not None else []):
_deep_apply(target, key, "REDACTED")
@ -54,9 +55,6 @@ class SingleKill(NamedTuple):
for key in (self.delete_fields if self.delete_fields is not None else []):
_deep_apply(target, key, delete=True)
for key, value in (self.set_fields if self .set_fields is not None else {}).items():
_deep_apply(target, key, value)
return target

View File

@ -1,4 +1,5 @@
"""Tests of killswitch behaviour."""
import copy
from typing import Optional
import pytest
@ -47,3 +48,28 @@ def test_killswitch(
return # we didn't expect any result
assert res == result
@pytest.mark.parametrize(
('kill_dict', 'input', 'result'),
[
({'set_fields': {'test': None}}, {}, {'test': None}),
({'set_fields': {'test': None}, 'delete_fields': ['test']}, {}, {}),
({'set_fields': {'test': None}, 'redact_fields': ['test']}, {}, {'test': 'REDACTED'}),
({'set_fields': {'test': None}, 'redact_fields': ['test'], 'delete_fields': ['test']}, {}, {}),
],
)
def test_operator_precedence(
kill_dict: killswitch.SingleKillSwitchJSON, input: killswitch.UPDATABLE_DATA, result: killswitch.UPDATABLE_DATA
) -> None:
"""Ensure that operators are being applied in the correct order."""
kill = killswitch.SingleKill(
"", "", kill_dict.get('redact_fields'), kill_dict.get('delete_fields'), kill_dict.get('set_fields')
)
cpy = copy.deepcopy(input)
kill.apply_rules(cpy)
assert cpy == result