Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: partial replacement for string with length #383

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions toolium/test/utils/test_dataset_replace_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,12 @@ def test_replace_param_type_inference_disabled():
assert param == '{"a":"test1", "b":true, "c":null}'
param = replace_param('["1", true, null]', infer_param_type=False)
assert param == '["1", true, null]'


def test_replace_param_partial_string_with_length():
param = replace_param('parameter=[STRING_WITH_LENGTH_5]')
assert param == 'parameter=aaaaa'
param = replace_param('[STRING_WITH_LENGTH_5] is string')
assert param == 'aaaaa is string'
param = replace_param('parameter [STRING_WITH_LENGTH_5] is string')
assert param == 'parameter aaaaa is string'
21 changes: 9 additions & 12 deletions toolium/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,30 +308,27 @@ def _replace_param_fixed_length(param):
:param param: parameter value
:return: tuple with replaced value and boolean to know if replacement has been done
"""
string_seed = 'a'
integer_seed = '1'
new_param = param
param_replaced = False
# we allow partial replacements for STRING
string_expression = re.compile(r'\[STRING_WITH_LENGTH_([0-9]+)\]')
new_param = string_expression.sub(lambda x: int(x.group(1)) * 'a', param)
if param.startswith('[') and param.endswith(']'):
if any(x in param for x in ['STRING_ARRAY_WITH_LENGTH_', 'INTEGER_ARRAY_WITH_LENGTH_']):
seeds = {'STRING': 'a', 'INTEGER': 1}
seeds = {'STRING': string_seed, 'INTEGER': int(integer_seed)}
seed, length = param[1:-1].split('_ARRAY_WITH_LENGTH_')
new_param = list(seeds[seed] for x in range(int(length)))
param_replaced = True
elif 'JSON_WITH_LENGTH_' in param:
length = int(param[1:-1].split('JSON_WITH_LENGTH_')[1])
new_param = dict((str(x), str(x)) for x in range(length))
param_replaced = True
elif any(x in param for x in ['STRING_WITH_LENGTH_', 'INTEGER_WITH_LENGTH_']):
seeds = {'STRING': 'a', 'INTEGER': '1'}
# The chain to be generated can be just a part of param
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could not be because we are inside an if that checks that param starts and ends with []

start = param.find('[')
end = param.find(']')
seed, length = param[start + 1:end].split('_WITH_LENGTH_')
generated_part = seeds[seed] * int(length)
placeholder = '[' + seed + '_WITH_LENGTH_' + length + ']'
new_param = param.replace(placeholder, generated_part)
elif any(x in param for x in ['INTEGER_WITH_LENGTH_']):
length = param[len('[INTEGER_WITH_LENGTH_'):-1]
new_param = int(integer_seed * int(length))
param_replaced = True
if seed == 'INTEGER':
new_param = int(new_param)
return new_param, param_replaced


Expand Down
Loading