Skip to content

Commit 87e8d98

Browse files
authored
Enhance key value function test (#11)
1 parent 8e37a87 commit 87e8d98

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

tests/test_transformer.py

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ def test_key_value_replacement(self):
5252

5353
def test_key_value_replacement_custom_function(self):
5454
input = {
55-
"hello": "world",
55+
"hello": "12characters",
5656
"hello2": "again",
57-
"path": {"to": {"anotherkey": "hi", "inside": {"hello": "inside"}}},
57+
"path": {
58+
"to": {
59+
"anotherkey": "hi",
60+
"twelvesymbol": {"hello": "twelvesymbol"},
61+
"fifteen_symbols": {"hello": "fifteen_symbols"},
62+
}
63+
},
5864
}
5965

6066
key_value = TransformerUtility.key_value_replacement_function(
@@ -64,34 +70,56 @@ def test_key_value_replacement_custom_function(self):
6470
)
6571

6672
expected_key_value = {
67-
"hello": "placeholder(5)",
73+
"hello": "placeholder(12)",
6874
"hello2": "again",
69-
"path": {"to": {"anotherkey": "hi", "inside": {"hello": "placeholder(6)"}}},
75+
"path": {
76+
"to": {
77+
"anotherkey": "hi",
78+
"twelvesymbol": {"hello": "placeholder(12)"},
79+
"fifteen_symbols": {"hello": "placeholder(15)"},
80+
}
81+
},
7082
}
7183

72-
copied = copy.deepcopy(input)
7384
ctx = TransformContext()
74-
assert key_value.transform(copied, ctx=ctx) == expected_key_value
85+
assert key_value.transform(input, ctx=ctx) == expected_key_value
7586
assert ctx.serialized_replacements == []
7687

77-
copied = copy.deepcopy(input)
88+
def test_key_value_replacement_custom_function_reference_replacement(self):
89+
input = {
90+
"hello": "12characters",
91+
"hello2": "again",
92+
"path": {
93+
"to": {
94+
"anotherkey": "hi",
95+
"twelvesymbol": {"hello": "twelvesymbol"},
96+
"fifteen_symbols": {"hello": "fifteen_symbols"},
97+
}
98+
},
99+
}
100+
78101
key_value = TransformerUtility.key_value_replacement_function(
79102
"hello",
80103
replacement_function=lambda k, v: f"placeholder({len(v)})",
81104
reference_replacement=True,
82105
)
83106
# replacement counters are per replacement key, so it will start from 1 again.
84107
expected_key_value_reference = {
85-
"hello": "<placeholder(5):1>",
108+
"hello": "<placeholder(12):1>",
86109
"hello2": "again",
87110
"path": {
88-
"to": {"anotherkey": "hi", "<placeholder(6):1>": {"hello": "<placeholder(6):1>"}}
111+
"to": {
112+
"anotherkey": "hi",
113+
"<placeholder(12):2>": {"hello": "<placeholder(12):2>"},
114+
"<placeholder(15):1>": {"hello": "<placeholder(15):1>"},
115+
}
89116
},
90117
}
91-
assert key_value.transform(copied, ctx=ctx) == copied
92-
assert len(ctx.serialized_replacements) == 2
118+
ctx = TransformContext()
119+
assert key_value.transform(input, ctx=ctx) == input
120+
assert len(ctx.serialized_replacements) == 3
93121

94-
tmp = json.dumps(copied, default=str)
122+
tmp = json.dumps(input, default=str)
95123
for sr in ctx.serialized_replacements:
96124
tmp = sr(tmp)
97125

0 commit comments

Comments
 (0)