@@ -52,9 +52,15 @@ def test_key_value_replacement(self):
52
52
53
53
def test_key_value_replacement_custom_function (self ):
54
54
input = {
55
- "hello" : "world " ,
55
+ "hello" : "12characters " ,
56
56
"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
+ },
58
64
}
59
65
60
66
key_value = TransformerUtility .key_value_replacement_function (
@@ -64,34 +70,56 @@ def test_key_value_replacement_custom_function(self):
64
70
)
65
71
66
72
expected_key_value = {
67
- "hello" : "placeholder(5 )" ,
73
+ "hello" : "placeholder(12 )" ,
68
74
"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
+ },
70
82
}
71
83
72
- copied = copy .deepcopy (input )
73
84
ctx = TransformContext ()
74
- assert key_value .transform (copied , ctx = ctx ) == expected_key_value
85
+ assert key_value .transform (input , ctx = ctx ) == expected_key_value
75
86
assert ctx .serialized_replacements == []
76
87
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
+
78
101
key_value = TransformerUtility .key_value_replacement_function (
79
102
"hello" ,
80
103
replacement_function = lambda k , v : f"placeholder({ len (v )} )" ,
81
104
reference_replacement = True ,
82
105
)
83
106
# replacement counters are per replacement key, so it will start from 1 again.
84
107
expected_key_value_reference = {
85
- "hello" : "<placeholder(5 ):1>" ,
108
+ "hello" : "<placeholder(12 ):1>" ,
86
109
"hello2" : "again" ,
87
110
"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
+ }
89
116
},
90
117
}
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
93
121
94
- tmp = json .dumps (copied , default = str )
122
+ tmp = json .dumps (input , default = str )
95
123
for sr in ctx .serialized_replacements :
96
124
tmp = sr (tmp )
97
125
0 commit comments