Package: hypothesis-mongoengine
This package contains aHypothesisstrategy for generating example documents from aMongoEnginemodel.Here’s a minimal example:fromhypothesisimportgiven,notefromhypothesis_mongoengine.strategiesimportdocumentsfrommongoengineimportDocument,StringFieldclassFoo(Document):foo=StringField()@given(documents(Foo))deftest_something(foo):# Handy since the default __repr__ is unhelpful:note(foo.to_json())asserthasattr(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)))deftest_anotherthing(foo):passBy default, all examples that would validate against the built-in MongoEngine restrictions are generated.
If the field is not required,Nonewill also be generated.
Ifchoicesis specified, only those values will be generated.Ifvalidationis 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 SupportedReferenceFieldis 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.
You can use thehypothesis_mongoengine.helpers.mark_savedfunction to make a document appear as if saved.DictFieldis not generically supported and probably will never be.MapFieldis supported generically and should be preferred toDictFieldwhen the values are homogenous.
When writing custom strategies for aDictField,
you can use thehypothesis_mongoengine.strategies.mongodb_keysstrategy to generate the keys in the absence of more specific application knowledge about the keys.DynamicDocument(andDynamicEmbeddedDocument) currently generate only the explicitly-specified fields.DynamicFieldis normally used internally byDynamicDocument,
but if you have a model which references it explicitly, it won’t be handled generically.Handling Custom FieldsIf you have a custom field in use in your application,
you can register a strategy to generate examples for it using thefield_stratdecorator.For example, a strategy for theEnumFieldfromextras-mongoenginecould look like this:fromextras_mongoengine.fieldsimportEnumFieldfromhypothesisimportstrategiesfromhypothesis_mongoengine.strategiesimportfield_strat@field_strat(EnumField)defmy_custom_strat(field):returnstrategies.sampled_from(field.enum)The fields are looked up in the registry by equality of the classes,
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:fromextras_mongoengine.fieldsimportEnumField,IntEnumField,StringEnumFieldfromhypothesisimportstrategiesfromhypothesis_mongoengine.strategiesimportfield_strat@field_strat(EnumField)@field_strat(IntEnumField)@field_strat(StringEnumField)defmy_custom_strat(field):returnstrategies.sampled_from(field.enum)