Skip to content

Commit

Permalink
upd8 (mock api): allow None.
Browse files Browse the repository at this point in the history
A field which can be `None` schema would be missing in the mock data.
  • Loading branch information
shenfe authored Oct 16, 2018
1 parent 4c7dbc4 commit 4b94ea4
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion azzert.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,34 @@ def ensure(value, schema, options={}, **kwargs):
def _mock(schema, options={}):
st = type_of(schema)

none_flag = 'this field can be missing'

if st is tuple:
for s in schema:
if isinstance(s, E):
return s.value
for s in schema:
if isinstance(s, D):
return s.value
for s in schema:
if s is types.NoneType:
return None
for s in schema:
if s is None:
return none_flag

if st is set:
for s in schema:
return s

if st is dict:
return {k: _mock(s, options) for k, s in schema.items()}
re = {}
for k, s in schema.items():
v = _mock(s, options)
if v == none_flag:
continue
re[k] = v
return re

if st is list:
return [_mock(schema[0], options)]
Expand Down

0 comments on commit 4b94ea4

Please sign in to comment.