Skip to content

Commit

Permalink
Feature: onstory callback to provide querystring and form metadata on…
Browse files Browse the repository at this point in the history
… documenting each story
  • Loading branch information
pylover committed Aug 26, 2024
1 parent ac4ffd8 commit 633ea89
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
1 change: 1 addition & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- write parameters table once at the base call.
- do not print out table of json forms, just print the object instead.
2 changes: 1 addition & 1 deletion bddrest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from .exceptions import InvalidUrlParametersError, CallVerifyError


__version__ = '5.1.0'
__version__ = '6.0.0dev'
20 changes: 13 additions & 7 deletions bddrest/documentary/documenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@


class Documenter:
def __init__(self, onfile, format='markdown', onfield=None):
def __init__(self, onfile, format='markdown', onstory=None):
self.format = format
self.onfile = onfile
self.onfield = onfield
self.onstory = onstory

def write_response(self, formatter, response):
formatter.write_header(f'Response: {response.status}', 3)
Expand Down Expand Up @@ -37,6 +37,12 @@ def write_curl(self, formatter, content):
def write_call(self, basecall, call, formatter):
formatter.write_header(f'{call.verb} {call.url}', 3)

if self.onstory:
queryinfo, fieldsinfo = self.onstory(basecall)
else:
queryinfo = {}
fieldsinfo = {}

if call.description:
formatter.write_paragraph(call.description)

Expand Down Expand Up @@ -66,7 +72,7 @@ def write_call(self, basecall, call, formatter):
formatter.write_header('Form', 3)
rows = []
for k, value in call.form.items():
info = self.onfield(call, k) if self.onfield else {}
info = fieldsinfo.get(k, {})
if info is None:
info = {}

Expand All @@ -90,7 +96,7 @@ def write_call(self, basecall, call, formatter):
formatter.write_header('Multipart', 3)
rows = []
for k, v in call.multipart.items():
info = self.onfield(call, k) if self.onfield else {}
info = fieldsinfo.get(k, {})
if info is None:
info = {}

Expand All @@ -113,14 +119,14 @@ def write_call(self, basecall, call, formatter):
formatter.write_header('Form', 3)
rows = []
for k, v in call.json.items():
info = self.onfield(call, k) if self.onfield else {}
info = fieldsinfo.get(k, {})
required = info.get('required')
not_none = info.get('not_none')
notnone = info.get('notnone')
type_ = info.get('type')
rows.append((
k,
'?' if required is None else required and 'Yes' or 'No',
'?' if not_none is None else not_none and 'No' or 'Yes',
'?' if notnone is None else notnone and 'No' or 'Yes',
'?' if type_ is None else type_,
v
))
Expand Down
8 changes: 4 additions & 4 deletions tests/test_documentary_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

def test_html():

def get_field_info(call, name):
return dict(
def get_field_info(call):
return dict(), dict(
f1=dict(required=True, not_none=True, type='str'),
).get(name)
)

story = Story.loads(provided_story)
outfile = io.StringIO()
story.document(
onfile=lambda _: outfile,
onfield=get_field_info,
onstory=get_field_info,
format='html'
)
outputstring = outfile.getvalue()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_documentary_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

def test_markdown():

def get_field_info(call, name):
return dict(
def get_field_info(call):
return dict(), dict(
f1=dict(required=True, not_none=True, type='str'),
).get(name)
)

story = Story.loads(provided_story)
outfile = io.StringIO()
story.document(onfile=lambda _: outfile, onfield=get_field_info)
story.document(onfile=lambda _: outfile, onstory=get_field_info)
outputstring = outfile.getvalue()
assert expected_markdown == outputstring

Expand Down

0 comments on commit 633ea89

Please sign in to comment.