Skip to content

Commit

Permalink
Fix pull request issue
Browse files Browse the repository at this point in the history
  • Loading branch information
promans718 committed Oct 8, 2024
1 parent 858b217 commit ada58fb
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ v3.2.1
- Add support for JSON strings to the `DICT` replacement. Example: [DICT:{"key": true}]
- Add `REPLACE` replacement, to replace a substring with another. Example: [REPLACE:[CONTEXT:some_url]::https::http]
- Add `TITLE` replacement, to apply Python's title() function. Example: [TITLE:the title]

- Add `ROUND` replacement, float number to a string with the expected decimals. Example: [ROUND:3.3333::2]

v3.2.0
------
Expand Down
27 changes: 27 additions & 0 deletions toolium/test/utils/test_dataset_map_param_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,33 @@ class Context(object):
dataset.behave_context = context
assert map_param("[CONTEXT:list.cmsScrollableActions.-1.id]") == 'ask-for-negative'

def test_a_context_param_list_incorrect_negative_index():
"""
Verification of a list with a correct negative index (In bounds) as CONTEXT
"""
class Context(object):
pass
context = Context()

context.list = {
'cmsScrollableActions': [
{
'id': 'ask-for-duplicate',
'text': 'QA duplica'
},
{
'id': 'ask-for-qa',
'text': 'QA no duplica'
},
{
'id': 'ask-for-negative',
'text': 'QA negative index'
}
]
}
dataset.behave_context = context
assert map_param("[CONTEXT:list.cmsScrollableActions.-5.id]")


def test_a_context_param_list_oob_index():
"""
Expand Down
14 changes: 14 additions & 0 deletions toolium/test/utils/test_dataset_replace_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,13 @@ def test_replace_param_dict():
assert param == {'a': 'test1', 'b': 'test2', 'c': 'test3'}


def test_replace_param_dict_json_format():
param = replace_param('[DICT:{"key": "value", "key_2": true}]')
assert param == '{"key": "value", "key_2": true}'
param = replace_param('[DICT:{"key": "value", "key_2": null}]')
assert param == '{"key": "value", "key_2": null}'


def test_replace_param_upper():
param = replace_param('[UPPER:test]')
assert param == 'TEST'
Expand Down Expand Up @@ -454,3 +461,10 @@ def test_replace_param_title():
assert param == "Holahola"
param = replace_param('[TITLE:hOlA]')
assert param == "HOlA"


def test_replace_param_round():
param = replace_param('[ROUND:7.5::2]')
assert param == "7.50"
param = replace_param('[ROUND:3.33333333::3]')
assert param == "3.333"
18 changes: 9 additions & 9 deletions toolium/utils/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def replace_param(param, language='es', infer_param_type=True):
[STR:xxxx] Cast xxxx to a string
[INT:xxxx] Cast xxxx to an int
[FLOAT:xxxx] Cast xxxx to a float
[ROUND:xxxx::2] String float with the expected decimals
[LIST:xxxx] Cast xxxx to a list
[DICT:xxxx] Cast xxxx to a dict
[UPPER:xxxx] Converts xxxx to upper case
Expand Down Expand Up @@ -228,24 +229,22 @@ def _get_random_phone_number():
def _replace_param_transform_string(param):
"""
Transform param value according to the specified prefix.
Available transformations: DICT, LIST, INT, FLOAT, STR, UPPER, LOWER, REPLACE, TITLE
Available transformations: DICT, LIST, INT, FLOAT, ROUND, STR, UPPER, LOWER, REPLACE, TITLE
:param param: parameter value
:return: tuple with replaced value and boolean to know if replacement has been done
"""
type_mapping_regex = r'\[(DICT|LIST|INT|FLOAT|STR|UPPER|LOWER|REPLACE|TITLE):([\w\W]*)\]'
type_mapping_regex = r'\[(DICT|LIST|INT|FLOAT|STR|UPPER|LOWER|REPLACE|TITLE|ROUND):([\w\W]*)\]'
type_mapping_match_group = re.match(type_mapping_regex, param)
new_param = param
param_transformed = False

if type_mapping_match_group:
param_transformed = True
if type_mapping_match_group.group(1) in ['DICT', 'LIST', 'INT', 'FLOAT']:
if '::' in type_mapping_match_group.group() and 'FLOAT' in type_mapping_match_group.group():
params_to_replace = type_mapping_match_group.group(
2).split('::')
float_formatted = "{:.2f}".format(round(float(params_to_replace[0]), int(params_to_replace[1])))
new_param = float_formatted
if type_mapping_match_group.group(1) in ['DICT', 'LIST', 'INT', 'FLOAT', 'ROUND']:
if '::' in type_mapping_match_group.group() and 'ROUND' in type_mapping_match_group.group():
params_to_replace = type_mapping_match_group.group(2).split('::')
new_param = f"{round(float(params_to_replace[0]), int(params_to_replace[1])):.{int(params_to_replace[1])}f}"
elif type_mapping_match_group.group(1) == 'DICT':
try:
new_param = json.loads(type_mapping_match_group.group(2).strip())
Expand Down Expand Up @@ -646,7 +645,8 @@ def get_value_from_context(param, context):
if isinstance(value, dict) and part in value:
value = value[part]
# evaluate if in an array, access is requested by index
elif isinstance(value, list) and part.lstrip('-+').isdigit() and int(part) < len(value):
elif isinstance(value, list) and part.lstrip('-+').isdigit() \
and int(part) < (len(value) + 1 if part.startswith("-") else len(value)):
value = value[int(part)]
# or by a key=value expression
elif isinstance(value, list) and (element := _select_element_in_list(value, part)):
Expand Down

0 comments on commit ada58fb

Please sign in to comment.