Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let hypothesis decide when to add optional keys #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions hyposchema/hypo_schema.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from random import randint

from hypothesis import strategies as hs

from hyposchema.regex import regex

EXCLUDE = object()


def gen_int(prop):
min_value = prop.get("minimum", None)
Expand All @@ -27,13 +27,6 @@ def gen_string(prop):
max_size=max_value)


def should_include(key, required_list):
if key in required_list:
return True
else:
return bool(randint(0, 1))


def gen_array(prop):
min_items = prop.get("minItems", None)
max_items = prop.get("maxItems", None)
Expand All @@ -59,6 +52,11 @@ def gen_any_obj():
lambda children: hs.dictionaries(hs.text(), children),
max_leaves=10)


def gen_maybe(value):
return hs.one_of(value, hs.just(EXCLUDE))


def gen_object(prop):

required = prop.get("required", [])
Expand All @@ -72,11 +70,11 @@ def gen_object(prop):

for k in prop[prop_key].keys():
json_prop = prop[prop_key][k]
gen = get_generator(json_prop)
output[k] = gen if k in required else gen_maybe(gen)

if should_include(k, required):
output[k] = get_generator(json_prop)

return hs.fixed_dictionaries(output)
return hs.fixed_dictionaries(output).map(
lambda m: {k: v for k, v in m.items() if v is not EXCLUDE})


def gen_enum(prop):
Expand Down