Skip to content

Commit

Permalink
Merge d1074e2 into 4e9b38e
Browse files Browse the repository at this point in the history
  • Loading branch information
rgonalo committed May 4, 2023
2 parents 4e9b38e + d1074e2 commit d90e75f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -6,6 +6,8 @@ v3.0.1

*Release date: In development*

- Allow to search in `context.storage` using `[CONTEXT:a.b.c]` replacement when `before_feature` method is not used

v3.0.0
------

Expand Down
50 changes: 45 additions & 5 deletions toolium/test/utils/test_dataset_map_param_context.py
Expand Up @@ -16,7 +16,6 @@
limitations under the License.
"""

import mock
import pytest

from toolium.utils import dataset
Expand All @@ -27,7 +26,9 @@ def test_a_context_param():
"""
Verification of a mapped parameter as CONTEXT
"""
context = mock.MagicMock()
class Context(object):
pass
context = Context()
context.attribute = "attribute value"
context.storage = {"storage_key": "storage entry value"}
context.feature_storage = {"feature_storage_key": "feature storage entry value"}
Expand All @@ -42,7 +43,9 @@ def test_a_context_param_storage():
"""
Verification of a mapped parameter as CONTEXT saved in storage
"""
context = mock.MagicMock()
class Context(object):
pass
context = Context()
context.attribute = "attribute value"
context.storage = {"storage_key": "storage entry value"}
context.feature_storage = {"feature_storage_key": "feature storage entry value"}
Expand All @@ -57,7 +60,9 @@ def test_a_context_param_feature_storage():
"""
Verification of a mapped parameter as CONTEXT saved in feature storage
"""
context = mock.MagicMock()
class Context(object):
pass
context = Context()
context.attribute = "attribute value"
context.storage = {"storage_key": "storage entry value"}
context.feature_storage = {"feature_storage_key": "feature storage entry value"}
Expand All @@ -72,7 +77,9 @@ def test_a_context_param_storage_and_feature_storage():
"""
Verification of a mapped parameter as CONTEXT saved in storage and feature storage
"""
context = mock.MagicMock()
class Context(object):
pass
context = Context()
context.attribute = "attribute value"
context.storage = {"storage_key": "storage entry value"}
context.feature_storage = {"storage_key": "feature storage entry value"}
Expand All @@ -83,6 +90,39 @@ def test_a_context_param_storage_and_feature_storage():
assert expected_st == result_st


def test_a_context_param_without_storage_and_feature_storage():
"""
Verification of a mapped parameter as CONTEXT when before_feature and before_scenario have not been executed, so
storage and feature_storage are not initialized
"""
class Context(object):
pass
context = Context()
context.attribute = "attribute value"
dataset.behave_context = context

result_att = map_param("[CONTEXT:attribute]")
expected_att = "attribute value"
assert expected_att == result_att


def test_a_context_param_storage_without_feature_storage():
"""
Verification of a mapped parameter as CONTEXT saved in storage when before_feature has been executed, so
feature_storage is not initialized
"""
class Context(object):
pass
context = Context()
context.attribute = "attribute value"
context.storage = {"storage_key": "storage entry value"}
dataset.behave_context = context

result_st = map_param("[CONTEXT:storage_key]")
expected_st = "storage entry value"
assert expected_st == result_st


def test_a_context_param_dict():
"""
Verification of a mapped parameter in a dict as CONTEXT
Expand Down
7 changes: 3 additions & 4 deletions toolium/utils/dataset.py
Expand Up @@ -619,11 +619,10 @@ def _get_initial_value_from_context(initial_key, context):
:param context: behave context
:return: mapped value
"""
try:
context_storage = context.storage if hasattr(context, 'storage') else {}
if hasattr(context, 'feature_storage'):
# context.feature_storage is initialized only when before_feature method is called
context_storage = collections.ChainMap(context.storage, context.feature_storage)
except AttributeError:
# When before_feature is not called, context.storage and context.feature_storage are not available
context_storage = {}
if initial_key in context_storage:
value = context_storage[initial_key]
elif hasattr(context, initial_key):
Expand Down

0 comments on commit d90e75f

Please sign in to comment.