Skip to content

Commit

Permalink
Feat/set vars (#82)
Browse files Browse the repository at this point in the history
* feat: add a SetVars step

* docs: document the SteVars step

* docs: changelog

* Bump version: 3.0.0-dev5 → 3.0.0-dev6

---------

Co-authored-by: github-actions <[email protected]>
  • Loading branch information
EtienneWallet and github-actions[bot] authored Nov 27, 2024
1 parent 7a7ec4f commit 9b04292
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
3 changes: 2 additions & 1 deletion docs/source/dev_documentation/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

- the data save path of `MxOps` is now configurable through the config var `DATA_PATH`
- chain-simulator network and adapted transaction schema (forward chain until transaction completion)
- FileFuzzerStep
- `FileFuzzerStep`
- Account's addresses are now saved within the `Scenario` data at load time. They can be referenced to with "%account_id.address"
- `SetVarsStep`


### Changed
Expand Down
18 changes: 17 additions & 1 deletion docs/source/user_documentation/steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,20 @@ steps:
```
Then, all of the `Steps` is the `Scene` `assign_role.yaml` should be written while using `%USER_FOR_ROLE` instead of the address of the wallet you want to assign the role to.
This will apply all the `Steps` to françois, jacques and jean without having to copy/paste the `Steps` for each one of them.
This will apply all the `Steps` to françois, jacques and jean without having to copy/paste the `Steps` for each one of them.

(set_vars_step_target)=
### Set Vars Step

This `Step` allows you to directly set some variables within the Scenario data. This will be useful if you need to set some values for generic `Scenes` or if you need to backup some variables
so that they are not overwritten.

```yaml
type: SetVars
variables:
MyVar: 12312424
result-backup: "%previously-registered-value"
nested_values: ["$CONF_VAR", 12421, "%var", {"%account.address": "%saved-value"}]
```

The above example will saves three variables within the `Scenario` data: `MyVar`, `result-backup` and `nested_values`. Their values (or nested values) will be accessible with the `%` symbol (refer to the {doc}`values` section for more details of the value system of MxOps).
22 changes: 22 additions & 0 deletions mxops/execution/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,3 +1704,25 @@ def instanciate_steps(raw_steps: List[Dict]) -> List[Step]:
raise errors.InvalidStepDefinition(step_type, raw_step) from err
steps_list.append(step)
return steps_list


@dataclass
class SetVarsStep(Step):
"""
Represents a steop to set variables within the Scenario
"""

variables: Dict[str, Any]

def execute(self):
"""
Parse the values to be assigned to the given variables
"""
scenario_data = ScenarioData.get()

for key, raw_value in self.variables.items():
value = utils.retrieve_value_from_any(raw_value)
LOGGER.info(
f"Setting variable `{key}` with the value `{value}` ({raw_value})"
)
scenario_data.set_value(key, value)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "mxops"
version = "3.0.0-dev5"
version = "3.0.0-dev6"
authors = [
{name="Etienne Wallet"},
]
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 3.0.0-dev5
current_version = 3.0.0-dev6
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(-(?P<release>\w+)(?P<build>\d+))?
serialize =
{major}.{minor}.{patch}-{release}{build}
Expand Down
48 changes: 45 additions & 3 deletions tests/test_steps.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
from mxops.data.execution_data import ScenarioData
from mxops.execution.steps import PythonStep
from mxops.execution.steps import PythonStep, SetVarsStep


def test_python_step():
Expand Down Expand Up @@ -37,6 +37,48 @@ def test_python_step():
assert os_value_2 == "4582"


def test_query_step():
def test_direct_set_vars_step():
# Given
pass
scenario_data = ScenarioData.get()
variables = {
"int-var": 1,
"str-var": "hello",
"list-var": [1, 2, "a", [1, 3]],
"dict-var": {"a": 23, 23: ["a", 124]},
}
step = SetVarsStep(variables)

# When
step.execute()

# Then
saved_values = {key: scenario_data.get_value(key) for key in variables}
assert variables == saved_values


def test_reference_set_vars_step():
# Given
scenario_data = ScenarioData.get()
scenario_data.set_value("reg-list", [1, 2, 3])
scenario_data.set_value("reg-name", "bob")
scenario_data.set_value("reg-value", 1)
variables = {
"int-var": "%reg-value",
"str-var": "%reg-name",
"list-var": [1, 2, "a", "%reg-list"],
"dict-var": {"%reg-name": 23, 23: ["%reg-name", "%reg-list"]},
}
step = SetVarsStep(variables)

# When
step.execute()

# Then
expected_result = {
"int-var": 1,
"str-var": "bob",
"list-var": [1, 2, "a", [1, 2, 3]],
"dict-var": {"bob": 23, 23: ["bob", [1, 2, 3]]},
}
saved_values = {key: scenario_data.get_value(key) for key in variables}
assert expected_result == saved_values

0 comments on commit 9b04292

Please sign in to comment.