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
from hypothesis_mongoengine.strategies import documents
from mongoengine import Document, StringField
class Foo(Document):
foo = StringField()
@given(documents(Foo))
def test_something(foo):
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.
Depending on the level of control you have over the models, it might be useful to infer everything except whether or not a field is required. In that cause, you can use the provided field_values strategy but provide required=True as a keyword argument.
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. You can use the hypothesis_mongoengine.helpers.mark_saved function to make a document appear as if saved.
DictField is not generically supported and probably will never be. MapField is supported generically and should be preferred to DictField when the values are homogenous. When writing custom strategies for a DictField, you can use the hypothesis_mongoengine.strategies.mongodb_keys strategy to generate the keys in the absence of more specific application knowledge about the keys.
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 be 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_strategy 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.strategies import field_strategy
@field_strategy(EnumField)
def my_custom_strat(field):
return strategies.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:
from extras_mongoengine.fields import EnumField, IntEnumField, StringEnumField
from hypothesis import strategies
from hypothesis_mongoengine.strategies import field_strategy
@field_strategy(EnumField)
@field_strategy(IntEnumField)
@field_strategy(StringEnumField)
def my_custom_strat(field):
return strategies.sampled_from(field.enum)
Project details
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
File details
Details for the file hypothesis_mongoengine-2.0.0.tar.gz
.
File metadata
- Download URL: hypothesis_mongoengine-2.0.0.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd2132350f5614185a3fcc146f0a5dcc767ea99b5eddb9729b30d169853b1ade |
|
MD5 | 49023c04abed3db43c8ad8a822f05bb8 |
|
BLAKE2b-256 | 87efc7ece36dddc7c106e5c3bdb481e8f0f17239d83f802817407420a137b429 |
Provenance
The following attestation bundles were made for hypothesis_mongoengine-2.0.0.tar.gz
:
Publisher:
pypi-package.yml
on gmacon/hypothesis-mongoengine
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
hypothesis_mongoengine-2.0.0.tar.gz
- Subject digest:
fd2132350f5614185a3fcc146f0a5dcc767ea99b5eddb9729b30d169853b1ade
- Sigstore transparency entry: 150036076
- Sigstore integration time:
- Predicate type:
File details
Details for the file hypothesis_mongoengine-2.0.0-py3-none-any.whl
.
File metadata
- Download URL: hypothesis_mongoengine-2.0.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 488c15a9674178a2b274b25ee9c5df161358eb1160ca9a9152eb39d672e71ece |
|
MD5 | 0633fdba8ff9a006d74c82ed4f2679d2 |
|
BLAKE2b-256 | 18f0ecd5d9f0e1425cb9d7cb91c748bbefad09a7d3a2705aa0c1b21394f76c29 |
Provenance
The following attestation bundles were made for hypothesis_mongoengine-2.0.0-py3-none-any.whl
:
Publisher:
pypi-package.yml
on gmacon/hypothesis-mongoengine
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
hypothesis_mongoengine-2.0.0-py3-none-any.whl
- Subject digest:
488c15a9674178a2b274b25ee9c5df161358eb1160ca9a9152eb39d672e71ece
- Sigstore transparency entry: 150036077
- Sigstore integration time:
- Predicate type: