Skip to content

Commit

Permalink
NOV-244340: expression for selecting in an array for a context storage (
Browse files Browse the repository at this point in the history
#388)

* chore: base code for selecting in an array using expresions like a.[key='value'].b

* fix: previous unitary tests with the new code

* test: add positive unitary tests for the new feature

* test: add negative unitary tests

* doc: updated methods docstring

* doc: updated changelog

* fix: flake8 format for unitary testing

* fix: flake8 format for dataset module

* chore: adapt code to codeclimate bot stupidity

* test: add unitary test for invalid list structure
  • Loading branch information
robertomier committed May 29, 2024
1 parent 1cee00e commit 9c3eefa
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ v3.1.5
*Release date: In development*

- Fix `export_poeditor_project` method allowing empty export response
- Add key=value expressions for selecting elements in the context storage

v3.1.4
------
Expand Down
313 changes: 308 additions & 5 deletions toolium/test/utils/test_dataset_map_param_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class Context(object):

with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.text]")
assert "the index 'text' must be a numeric index" == str(excinfo.value)
assert "the expression 'text' was not able to select an element in the list" == str(excinfo.value)


def test_a_context_param_list_correct_index():
Expand Down Expand Up @@ -411,11 +411,11 @@ class Context(object):

with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.prueba.id]")
assert "the index 'prueba' must be a numeric index" == str(excinfo.value)
assert "the expression 'prueba' was not able to select an element in the list" == str(excinfo.value)

with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.'36'.id]")
assert "the index ''36'' must be a numeric index" == str(excinfo.value)
assert "the expression ''36'' was not able to select an element in the list" == str(excinfo.value)


def test_a_context_param_class_no_numeric_index():
Expand All @@ -441,8 +441,311 @@ def __init__(self):
print(context)
with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.prueba.id]")
assert "the index 'prueba' must be a numeric index" == str(excinfo.value)
assert "the expression 'prueba' was not able to select an element in the list" == str(excinfo.value)

with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.'36'.id]")
assert "the index ''36'' must be a numeric index" == str(excinfo.value)
assert "the expression ''36'' was not able to select an element in the list" == str(excinfo.value)


def test_a_context_param_list_correct_select_expression():
"""
Verification of a list with a correct select expression 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"},
]
}
dataset.behave_context = context
assert (
map_param("[CONTEXT:list.cmsScrollableActions.id=ask-for-qa.text]")
== "QA no duplica"
)


def test_a_context_param_list_correct_select_expression_with_value_single_quotes():
"""
Verification of a list with a correct select expression having single quotes for value 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"},
]
}
dataset.behave_context = context
assert (
map_param("[CONTEXT:list.cmsScrollableActions.id='ask-for-qa'.text]")
== "QA no duplica"
)


def test_a_context_param_list_correct_select_expression_with_value_double_quotes():
"""
Verification of a list with a correct select expression having double quotes for value 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"},
]
}
dataset.behave_context = context
assert (
map_param('[CONTEXT:list.cmsScrollableActions.id="ask-for-qa".text]')
== "QA no duplica"
)


def test_a_context_param_list_correct_select_expression_with_key_single_quotes():
"""
Verification of a list with a correct select expression having single quotes for the value 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"},
]
}
dataset.behave_context = context
assert (
map_param("[CONTEXT:list.cmsScrollableActions.'id'=ask-for-qa.text]")
== "QA no duplica"
)


def test_a_context_param_list_correct_select_expression_with_key_double_quotes():
"""
Verification of a list with a correct select expression having double quotes for the key 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"},
]
}
dataset.behave_context = context
assert (
map_param('[CONTEXT:list.cmsScrollableActions."id"=ask-for-qa.text]')
== "QA no duplica"
)


def test_a_context_param_list_correct_select_expression_with_key_and_value_with_quotes():
"""
Verification of a list with a correct select expression having quotes for both key and value 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"},
]
}
dataset.behave_context = context
assert (
map_param('[CONTEXT:list.cmsScrollableActions."id"="ask-for-qa".text]')
== "QA no duplica"
)


def test_a_context_param_list_correct_select_expression_with_blanks():
"""
Verification of a list with a correct select expression with blanks in the text to search for 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"},
]
}
dataset.behave_context = context
assert (
map_param('[CONTEXT:list.cmsScrollableActions.text="QA no duplica".id]')
== "ask-for-qa"
)


def test_a_context_param_list_correct_select_expression_finds_nothing():
"""
Verification of a list with a correct select expression which obtains no result 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"},
]
}
dataset.behave_context = context
with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.id='not-existing-id'.text]")
assert (
"the expression 'id='not-existing-id'' was not able to select an element in the list"
== str(excinfo.value)
)


def test_a_context_param_list_correct_select_expression_with_empty_value_finds_nothing():
"""
Verification of a list with a correct select expression with empty value which obtains no result 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"},
]
}
dataset.behave_context = context
with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.id=''.text]")
assert (
"the expression 'id=''' was not able to select an element in the list"
== str(excinfo.value)
)


def test_a_context_param_list_correct_select_expression_with_empty_value_hits_value():
"""
Verification of a list with a correct select expression with empty value which obtains no result as CONTEXT
"""

class Context(object):
pass

context = Context()

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


def test_a_context_param_list_invalid_select_expression():
"""
Verification of a list with an invalid select expression 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"},
]
}
dataset.behave_context = context
with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.invalidexpression.text]")
assert (
"the expression 'invalidexpression' was not able to select an element in the list"
== str(excinfo.value)
)


def test_a_context_param_list_invalid_select_expression_having_empty_key():
"""
Verification of a list with a invalid select expression having empty key 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"},
]
}
dataset.behave_context = context
with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.='not-existing-id'.text]")
assert (
"the expression '='not-existing-id'' was not able to select an element in the list"
== str(excinfo.value)
)


def test_a_context_param_list_invalid_structure_for_valid_select():
"""
Verification of a list with a invalid structure for a valid select as CONTEXT
"""

class Context(object):
pass

context = Context()

context.list = {
"cmsScrollableActions": {"id": "ask-for-duplicate", "text": "QA duplica"},
}
dataset.behave_context = context
with pytest.raises(Exception) as excinfo:
map_param("[CONTEXT:list.cmsScrollableActions.id=ask-for-duplicate.text]")
assert (
"'id=ask-for-duplicate' key not found in {'id': 'ask-for-duplicate', 'text': 'QA duplica'} value in context"
== str(excinfo.value)
)
Loading

0 comments on commit 9c3eefa

Please sign in to comment.