Skip to content

Commit

Permalink
Update ActionOutput references.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyandrewmeyer committed Aug 2, 2024
1 parent 56545a4 commit eefe9cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -974,18 +974,31 @@ def test_backup_action():

# If you didn't declare do_backup in the charm's metadata,
# the `ConsistencyChecker` will slap you on the wrist and refuse to proceed.
out: scenario.ActionOutput = ctx.run(ctx.on.action("do_backup"), scenario.State())
state = ctx.run(ctx.on.action("do_backup"), scenario.State())

# You can assert action results, logs, failure using the ActionOutput interface:
assert out.logs == ['baz', 'qux']

if out.success:
# If the action did not fail, we can read the results:
assert out.results == {'foo': 'bar'}
# You can assert action results and logs using the action history:
assert ctx.action_history[0].logs == ['baz', 'qux']
assert ctx.action_history[0].results == {'foo': 'bar'}
```

## Failing Actions

If the charm code calls `event.fail()` to indicate that the action has failed,
a `ActionFailed` exception will be raised. This avoids having to include
`assert ctx.action_history[0].status == "completed"` code in every test where
the action is successful.

```python
def test_backup_action_failed():
ctx = scenario.Context(MyCharm)

with pytest.raises(ActionFailed) as exc_info:
ctx.run(ctx.on.action("do_backup"), scenario.State())
assert exc_info.value.message == "sorry, couldn't do the backup"

else:
# If the action fails, we can read a failure message:
assert out.failure == 'boo-hoo'
# You can still assert action results and logs that occured before the failure:
assert ctx.action_history[0].logs == ['baz', 'qux']
assert ctx.action_history[0].results == {'foo': 'bar'}
```

## Parametrized Actions
Expand Down
3 changes: 2 additions & 1 deletion scenario/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -1837,10 +1837,11 @@ class _Action(_max_posargs(1)):
def test_backup_action():
ctx = scenario.Context(MyCharm)
out: scenario.ActionOutput = ctx.run(
state = ctx.run(
ctx.on.action('do_backup', params={'filename': 'foo'}),
scenario.State()
)
assert ctx.action_history[0].results == ...
"""

name: str
Expand Down

0 comments on commit eefe9cb

Please sign in to comment.