Skip to content

Commit 5ec2258

Browse files
committed
Allow list search in CONTEXT - get_attribute
1 parent 6dfb468 commit 5ec2258

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

toolium/test/utils/test_dataset_map_param_context.py

Lines changed: 34 additions & 1 deletion
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 map_param
22+
from toolium.utils.dataset import get_attribute, map_param
2323

2424

2525
def test_a_context_param():
@@ -433,3 +433,36 @@ class Context(object):
433433
}
434434
dataset.behave_context = context
435435
assert map_param("[CONTEXT:list.url{3}.id{1}]") == 'ask-for-duplicate'
436+
437+
438+
def test_get_attribute_class():
439+
"""
440+
Verification of a get attribute given a class
441+
"""
442+
443+
class ExampleClass:
444+
"""
445+
ExampleClass class
446+
"""
447+
448+
def __init__(self, url, text):
449+
self.url = url
450+
self.text = text
451+
452+
example = ExampleClass(url = {'id': 'ask-for-duplicate'}, text="QA duplica")
453+
454+
assert get_attribute(example, "text") == 'QA duplica'
455+
456+
def test_get_attribute_dict():
457+
"""
458+
Verification of a get attribute given a dictionary
459+
"""
460+
461+
example = {
462+
'url': {
463+
'id': 'ask-for-duplicate'
464+
},
465+
'text': 'QA duplica'
466+
}
467+
468+
assert get_attribute(example, "text") == 'QA duplica'

toolium/utils/dataset.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ def get_value_from_context(param, context):
609609
elif isinstance(value, list):
610610
msg, value = get_value_in_list(value, list_index, part)
611611
elif hasattr(value, part):
612-
value = getattr(value, part)
612+
value = get_attribute(value, part)
613613
else:
614614
msg = get_value_context_errors(value, part)
615615
if msg:
@@ -627,7 +627,10 @@ def get_list_index_value(value, is_list_with_index):
627627
:param is_list_with_index: key that matchs the regex (.*){(.*)}, key{index}
628628
:return: value and index updated
629629
"""
630-
val = value[is_list_with_index.group(1)]
630+
try:
631+
val = get_attribute(value, is_list_with_index.group(1))
632+
except AttributeError:
633+
val = value[is_list_with_index.group(1)]
631634
try:
632635
idx = int(is_list_with_index.group(2))
633636
except ValueError:
@@ -649,10 +652,24 @@ def get_value_in_list(value, list_index, part):
649652
if list_index >= len(value):
650653
msg = f"Invalid index '{list_index}', list size is '{len(value)}'. {list_index} >= {len(value)}."
651654
else:
652-
value = value[list_index][part]
655+
value = get_attribute(value[list_index], part)
653656
return msg, value
654657

655658

659+
def get_attribute(value, key):
660+
"""
661+
get the key attribute, accepts dictionary and classes
662+
663+
:param value: updated value of the dict
664+
:param key: part to take in the value
665+
:return: error msg and updated value
666+
"""
667+
try:
668+
return getattr(value, key)
669+
except AttributeError:
670+
return value[key]
671+
672+
656673
def get_value_context_errors(value, part):
657674
"""
658675
Returns the errors
@@ -683,7 +700,7 @@ def _get_initial_value_from_context(initial_key, context):
683700
if initial_key in context_storage:
684701
value = context_storage[initial_key]
685702
elif hasattr(context, initial_key):
686-
value = getattr(context, initial_key)
703+
value = get_attribute(context, initial_key)
687704
else:
688705
msg = f"'{initial_key}' key not found in context"
689706
logger.error(msg)
@@ -774,7 +791,7 @@ def get_file(file_path):
774791
Return the content of a file given its path. If a base path was previously set by using
775792
the set_file_path() function, the file path specified must be relative to that path.
776793
777-
:param file path: file path using slash as separator (e.g. "resources/files/doc.txt")
794+
:param file_path: file path using slash as separator (e.g. "resources/files/doc.txt")
778795
:return: string with the file content
779796
"""
780797
file_path_parts = (base_file_path + file_path).split("/")
@@ -791,7 +808,7 @@ def convert_file_to_base64(file_path):
791808
Return the content of a file given its path encoded in Base64. If a base path was previously set by using
792809
the set_file_path() function, the file path specified must be relative to that path.
793810
794-
:param file path: file path using slash as separator (e.g. "resources/files/doc.txt")
811+
:param file_path: file path using slash as separator (e.g. "resources/files/doc.txt")
795812
:return: string with the file content encoded in Base64
796813
"""
797814
file_path_parts = (base_base64_path + file_path).split("/")

0 commit comments

Comments
 (0)