Skip to content

Commit ee3992f

Browse files
committed
Merge pull request #2 from apibyexample/txels/inheritable-properties
Prevent the need for repetition in sample definitions
2 parents e3e6f63 + 9cd4051 commit ee3992f

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

abe/mocks.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,24 @@ def __init__(self, filename):
2020

2121
# Make all example requests and reponses accessible via dot syntax
2222
# (e.g. mock["OK"].request.status)
23-
for k, v in self.examples.items():
24-
self.examples[k] = dotify(v)
23+
for key, value in self.examples.items():
24+
# Add the request URL automatically if missing.
25+
self._feed_inherited_fields(value, 'request', ['url', 'method'])
26+
self.examples[key] = dotify(value)
27+
28+
def _feed_inherited_fields(self, value, where, inheritable):
29+
"""
30+
If missing in request or response, some fields are inherited.
31+
32+
URL and method are defined at the top level. They don't need to be
33+
redefined in example requests or responses.
34+
35+
:param where: 'request' or 'response'
36+
:param inheritable: list of inheritable fields
37+
38+
"""
39+
if where not in value:
40+
value[where] = {}
41+
for key in inheritable:
42+
if key not in value[where]:
43+
value[where][key] = getattr(self, key)

0 commit comments

Comments
 (0)