Skip to content

Commit

Permalink
Merge branch 'main' of github.com:canonical/ops-scenario into error-type
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Oct 20, 2023
2 parents e65f8cb + 3a5284d commit bcc1743
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "ops-scenario"

version = "5.4"
version = "5.4.1"

authors = [
{ name = "Pietro Pasotti", email = "[email protected]" }
Expand Down
1 change: 1 addition & 0 deletions scenario/consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ def _check_action_param_types(
to_python_type = {
"string": str,
"boolean": bool,
"integer": int,
"number": Number,
"array": Sequence,
"object": dict,
Expand Down
5 changes: 3 additions & 2 deletions scenario/mocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
ModelError,
RelationNotFoundError,
SecretInfo,
SecretNotFoundError,
SecretRotate,
_format_action_result_dict,
_ModelBackend,
Expand Down Expand Up @@ -142,12 +143,12 @@ def _get_secret(self, id=None, label=None):
try:
return next(filter(lambda s: s.id == id, self._state.secrets))
except StopIteration:
raise RuntimeError(f"not found: secret with id={id}.")
raise SecretNotFoundError()
elif label:
try:
return next(filter(lambda s: s.label == label, self._state.secrets))
except StopIteration:
raise RuntimeError(f"not found: secret with label={label}.")
raise SecretNotFoundError()
else:
raise RuntimeError("need id or label.")

Expand Down
33 changes: 23 additions & 10 deletions tests/test_consistency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,22 +304,35 @@ def test_action_name():
)


def test_action_params_type():
action = Action("foo", params={"bar": "baz"})
_ACTION_TYPE_CHECKS = [
("string", "baz", None),
("boolean", True, "baz"),
("integer", 42, 1.5),
("number", 28.8, "baz"),
("array", ["a", "b", "c"], 1.5), # A string is an acceptable array.
("object", {"k": "v"}, "baz"),
]


@pytest.mark.parametrize("ptype,good,bad", _ACTION_TYPE_CHECKS)
def test_action_params_type(ptype, good, bad):
action = Action("foo", params={"bar": good})
assert_consistent(
State(),
action.event,
_CharmSpec(
MyCharm, meta={}, actions={"foo": {"params": {"bar": {"type": "string"}}}}
),
)
assert_inconsistent(
State(),
action.event,
_CharmSpec(
MyCharm, meta={}, actions={"foo": {"params": {"bar": {"type": "boolean"}}}}
MyCharm, meta={}, actions={"foo": {"params": {"bar": {"type": ptype}}}}
),
)
if bad is not None:
action = Action("foo", params={"bar": bad})
assert_inconsistent(
State(),
action.event,
_CharmSpec(
MyCharm, meta={}, actions={"foo": {"params": {"bar": {"type": ptype}}}}
),
)


def test_duplicate_relation_ids():
Expand Down
6 changes: 3 additions & 3 deletions tests/test_e2e/test_secrets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from ops.charm import CharmBase
from ops.framework import Framework
from ops.model import SecretRotate
from ops.model import SecretNotFoundError, SecretRotate

from scenario.state import Relation, Secret, State
from tests.helpers import trigger
Expand All @@ -25,9 +25,9 @@ def _on_event(self, event):

def test_get_secret_no_secret(mycharm):
def post_event(charm: CharmBase):
with pytest.raises(RuntimeError):
with pytest.raises(SecretNotFoundError):
assert charm.model.get_secret(id="foo")
with pytest.raises(RuntimeError):
with pytest.raises(SecretNotFoundError):
assert charm.model.get_secret(label="foo")

trigger(
Expand Down

0 comments on commit bcc1743

Please sign in to comment.