Created
February 23, 2016 15:43
-
-
Save jerith/fa7bd138a04335775ef0 to your computer and use it in GitHub Desktop.
A possible fix for Hypothesis Django model generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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