Hypothesis strategy for MongoEngine models
Project description
This package contains a Hypothesis strategy for generating example documents from a MongoEngine model.
Here’s a minimal example:
from hypothesis import given, note from hypothesis_mongoengine.strategies import documents from mongoengine import Document, StringField class Foo(Document): foo = StringField() @given(documents(Foo)) def test_something(foo): # Handy since the default __repr__ is unhelpful: note(foo.to_json()) assert hasattr(foo, 'id')
You can customize the generation of examples by passing alternate strategies for each field as keyword arguments:
@given(documents(Foo, foo=strategies.strings(max_size=7))) def test_another thing(foo): pass
By default, all examples that would validate against the built-in MongoEngine restrictions are generated. If the field is not required, None will also be generated. If choices is specified, only those values will be generated.
If validation is specified, the default strategy will be filtered by the validation function. If the custom validation function accepts too few values, Hypothesis may fail the health check. In that case, supply a custom validator that generates acceptable examples more efficiently.
What’s Not Supported
ReferenceField is not generically supported and probably will never be. You can, and should, provide an application-specific strategy for these fields. This permits you to ensure that the referential-integrity constraints needed by your application are satisfied. Don’t forget that MongoEngine expects the documents to have been saved to the database before you try to reference them.
DynamicDocument (and DynamicEmbeddedDocument) currently generate only the explicitly-specified fields.
DynamicField is normally used internally by DynamicDocument, but if you have a model which references it explicitly, it won’t by handled generically.
Handling Custom Fields
If you have a custom field in use in your application, you can register a strategy to generate examples for it using the field_strat decorator.
For example, a strategy for the EnumField from extras-mongoengine could look like this:
from extras_mongoengine.fields import EnumField from hypothesis import strategies from hypothesis_mongoengine import field_strat @field_strat(EnumField) def my_custom_strat(field): return strategies.sampled_from(field.enum)
The fields are looked up in the registry by their names, so if you have a hierarchy of custom fields, you must register the leaf types. You can, however, stack the decorator several times if you need to:
from extras_mongoengine.fields import EnumField, IntEnumField, StringEnumField from hypothesis import strategies from hypothesis_mongoengine import field_strat @field_strat(EnumField) @field_strat(IntEnumField) @field_strat(StringEnumField) def my_custom_strat(field): return strategies.sampled_from(field.enum)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for hypothesis-mongoengine-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f94b82e0bcbe0047ab29a5519c21a0eb53c8fa9cd704c1269f72abc81b1b6eb |
|
MD5 | 644449eaad89c9db8a8d6f235bf7b465 |
|
BLAKE2b-256 | ae140a0c0ece2c8f8f7644f669ef0b4212066fbdcc314e6e47a1fe07df997090 |
Hashes for hypothesis_mongoengine-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad18d98c9a7f218a6b508dfde86268bad02ccf77f883dd6ba5e646717149a1ce |
|
MD5 | 7da37c1ef949b8cbfdb771849474d585 |
|
BLAKE2b-256 | ea9bbcfb40f04b02ecf8606a77282f8c17af1bb10801766fb63b77c7eaa440ea |