Skip to content

Commit 1fa9310

Browse files
committed
Allow list search in CONTEXT - PR fixes
1 parent 3247fe9 commit 1fa9310

File tree

2 files changed

+20
-84
lines changed

2 files changed

+20
-84
lines changed

toolium/test/utils/test_dataset_map_param_context.py

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import pytest
2020

2121
from toolium.utils import dataset
22-
from toolium.utils.dataset import get_attribute, map_param
22+
from toolium.utils.dataset import map_param
2323

2424

2525
def test_a_context_param():
@@ -305,9 +305,9 @@ class Context(object):
305305
{'id': 'ask-for-qa', 'text': 'QA no duplica'}]
306306

307307

308-
def test_a_context_param_list_default():
308+
def test_a_context_param_list_default_no_index():
309309
"""
310-
Verification of a list without index as CONTEXT (get the attribute of the first element in list)
310+
Verification of a list without index as CONTEXT
311311
"""
312312
class Context(object):
313313
pass
@@ -326,7 +326,10 @@ class Context(object):
326326
]
327327
}
328328
dataset.behave_context = context
329-
assert map_param("[CONTEXT:list.cmsScrollableActions.text]") == 'QA duplica'
329+
330+
with pytest.raises(Exception) as excinfo:
331+
map_param("[CONTEXT:list.cmsScrollableActions.text]")
332+
assert "the index 'text' must be a numeric index" == str(excinfo.value)
330333

331334

332335
def test_a_context_param_list_correct_index():
@@ -408,11 +411,11 @@ class Context(object):
408411

409412
with pytest.raises(Exception) as excinfo:
410413
map_param("[CONTEXT:list.cmsScrollableActions.prueba.id]")
411-
assert "the index 'prueba' must be a numeric index or a valid key" == str(excinfo.value)
414+
assert "the index 'prueba' must be a numeric index" == str(excinfo.value)
412415

413416
with pytest.raises(Exception) as excinfo:
414417
map_param("[CONTEXT:list.cmsScrollableActions.'36'.id]")
415-
assert "the index ''36'' must be a numeric index or a valid key" == str(excinfo.value)
418+
assert "the index ''36'' must be a numeric index" == str(excinfo.value)
416419

417420

418421
def test_a_context_param_class_no_numeric_index():
@@ -438,42 +441,8 @@ def __init__(self):
438441
print(context)
439442
with pytest.raises(Exception) as excinfo:
440443
map_param("[CONTEXT:list.cmsScrollableActions.prueba.id]")
441-
assert "the index 'prueba' must be a numeric index or a valid key" == str(excinfo.value)
444+
assert "the index 'prueba' must be a numeric index" == str(excinfo.value)
442445

443446
with pytest.raises(Exception) as excinfo:
444447
map_param("[CONTEXT:list.cmsScrollableActions.'36'.id]")
445-
assert "the index ''36'' must be a numeric index or a valid key" == str(excinfo.value)
446-
447-
448-
def test_get_attribute_class():
449-
"""
450-
Verification of a get attribute given a class
451-
"""
452-
453-
class ExampleClass:
454-
"""
455-
ExampleClass class
456-
"""
457-
458-
def __init__(self, url, text):
459-
self.url = url
460-
self.text = text
461-
462-
example = ExampleClass(url={'id': 'ask-for-duplicate'}, text="QA duplica")
463-
464-
assert get_attribute(example, "text") == 'QA duplica'
465-
466-
467-
def test_get_attribute_dict():
468-
"""
469-
Verification of a get attribute given a dictionary
470-
"""
471-
472-
example = {
473-
'url': {
474-
'id': 'ask-for-duplicate'
475-
},
476-
'text': 'QA duplica'
477-
}
478-
479-
assert get_attribute(example, "text") == 'QA duplica'
448+
assert "the index ''36'' must be a numeric index" == str(excinfo.value)

toolium/utils/dataset.py

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,10 @@ def get_value_from_context(param, context):
601601
for part in parts[1:]:
602602
if isinstance(value, dict) and part in value:
603603
value = value[part]
604-
elif isinstance(value, list):
605-
msg, value = get_value_in_list(value, part)
604+
elif isinstance(value, list) and part.isdigit() and int(part) < len(value):
605+
value = value[int(part)]
606606
elif hasattr(value, part):
607-
value = get_attribute(value, part)
607+
value = getattr(value, part)
608608
else:
609609
msg = get_value_context_errors(value, part)
610610
if msg:
@@ -613,44 +613,6 @@ def get_value_from_context(param, context):
613613
return value
614614

615615

616-
def get_value_in_list(value, part):
617-
"""
618-
get the value updated with elements in list
619-
update the value with the given list_index and the part
620-
621-
:param value: updated value of the CONTEXT dict
622-
:param part: part to take in the value (index of the list)
623-
:return: error msg and updated value
624-
"""
625-
msg = None
626-
if part.isdigit():
627-
part = int(part)
628-
if part >= len(value):
629-
msg = f"Invalid index '{part}', list size is '{len(value)}'. {part} >= {len(value)}."
630-
else:
631-
value = value[part]
632-
else:
633-
try:
634-
value = get_attribute(value[0], part)
635-
except (KeyError, TypeError):
636-
msg = f"the index '{part}' must be a numeric index or a valid key"
637-
return msg, value
638-
639-
640-
def get_attribute(value, key):
641-
"""
642-
get the key attribute, accepts dictionary and classes
643-
644-
:param value: updated value of the dict
645-
:param key: part to take in the value
646-
:return: error msg and updated value
647-
"""
648-
try:
649-
return getattr(value, key)
650-
except AttributeError:
651-
return value[key]
652-
653-
654616
def get_value_context_errors(value, part):
655617
"""
656618
Returns the errors
@@ -661,6 +623,11 @@ def get_value_context_errors(value, part):
661623
"""
662624
if isinstance(value, dict):
663625
return f"'{part}' key not found in {value} value in context"
626+
elif isinstance(value, list):
627+
if part.isdigit():
628+
return f"Invalid index '{part}', list size is '{len(value)}'. {part} >= {len(value)}."
629+
else:
630+
return f"the index '{part}' must be a numeric index"
664631
else:
665632
return f"'{part}' attribute not found in {type(value).__name__} class in context"
666633

@@ -681,7 +648,7 @@ def _get_initial_value_from_context(initial_key, context):
681648
if initial_key in context_storage:
682649
value = context_storage[initial_key]
683650
elif hasattr(context, initial_key):
684-
value = get_attribute(context, initial_key)
651+
value = getattr(context, initial_key)
685652
else:
686653
msg = f"'{initial_key}' key not found in context"
687654
logger.error(msg)

0 commit comments

Comments
 (0)