-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bot
committed
Jan 7, 2024
1 parent
e58a38a
commit 509868f
Showing
5 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--- | ||
title: Story that rewrites given preconditions | ||
--- | ||
|
||
|
||
|
||
These examples show how to build stories that rewrite themselves | ||
from program output (in-test snapshot testing) but that rewrite | ||
the given preconditions. | ||
|
||
This is useful for changing | ||
|
||
``` | ||
self.current_step.rewrite("argument").to("new output") | ||
``` | ||
|
||
|
||
# Code Example | ||
|
||
|
||
|
||
example.story: | ||
|
||
```yaml | ||
Call API: | ||
given: | ||
mock api: | ||
request: | | ||
{"greeting": "hello"} | ||
response: | | ||
{"greeting": "hi"} | ||
steps: | ||
- Call API | ||
``` | ||
engine.py: | ||
```python | ||
from hitchstory import BaseEngine, GivenDefinition, GivenProperty | ||
from strictyaml import Map, Str | ||
|
||
class Engine(BaseEngine): | ||
given_definition = GivenDefinition( | ||
mock_api=GivenProperty( | ||
schema=Map({"request": Str(), "response": Str()}), | ||
inherit_via=GivenProperty.OVERRIDE, | ||
), | ||
) | ||
|
||
def __init__(self, rewrite=True): | ||
self._rewrite = rewrite | ||
|
||
def call_api(self): | ||
if self._rewrite: | ||
self.given.rewrite("Mock API", "response").to("""{"greeting": "bye"}""") | ||
``` | ||
With code: | ||
```python | ||
from hitchstory import StoryCollection | ||
from pathlib import Path | ||
from engine import Engine | ||
|
||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
```python | ||
StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).ordered_by_name().play() | ||
|
||
``` | ||
|
||
Will output: | ||
``` | ||
RUNNING Call API in /path/to/working/example.story ... SUCCESS in 0.1 seconds. | ||
``` | ||
|
||
|
||
|
||
|
||
File example.story should now contain: | ||
|
||
``` | ||
Call API: | ||
given: | ||
mock api: | ||
request: | | ||
{"greeting": "hello"} | ||
response: | | ||
{"greeting": "bye"} | ||
steps: | ||
- Call API | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
!!! note "Executable specification" | ||
|
||
Documentation automatically generated from | ||
<a href="https://github.com/hitchdev/hitchstory/blob/master/hitch/story/rewrite-given.story">rewrite-given.story | ||
storytests.</a> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters