Skip to content

Instantly share code, notes, and snippets.

@jerith
Created February 23, 2016 15:43
Show Gist options
  • Select an option

  • Save jerith/fa7bd138a04335775ef0 to your computer and use it in GitHub Desktop.

Select an option

Save jerith/fa7bd138a04335775ef0 to your computer and use it in GitHub Desktop.
A possible fix for Hypothesis Django model generation
DEFAULT_VALUE = object()
def models(model, **extra):
result = {}
mappings = field_mappings()
mandatory = set()
for f in model._meta.concrete_fields:
if isinstance(f, dm.AutoField):
continue
try:
mapped = mappings[type(f)]
except KeyError:
if not f.null:
mandatory.add(f.name)
continue
if f.null:
mapped = st.one_of(st.none(), mapped)
result[f.name] = mapped
missed = {x for x in mandatory if x not in extra}
if missed:
raise InvalidArgument((
u'Missing arguments for mandatory field%s %s for model %s' % (
u's' if len(missed) > 1 else u'',
u', '.join(missed),
model.__name__,
)))
for k, v in extra.items():
if v is not DEFAULT_VALUE:
result[k] = v
return ModelStrategy(model, result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment