Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Hypothesis DRF

Project description

[Hypothesis]( strategy for
[Django REST framework]( serializers.

Generate data that is valid according to a DRF serializer

### Known issues

* Only tested with python3.6
* `URLField` can be slow when generating the first example
* `*RelatedField` gets first object in queryset, not random
* Not all fields are supported, known non-working:
- FileField
- FilePathField
- ImageField
- ModelField

### Example

pip install hypothesis-drf

from hypothesis_drf import from_serializer, from_field
from rest_framework import serializers

class ExampleSerializer(serializers.Serializer):
name = serializers.CharField(min_length=3, max_length=8)
amount = serializers.IntegerField(min_value=200, max_value=500)

# {'amount': 391, 'name': '\U00053a6b&\U00030fee$.'}

from_field(serializers.FloatField(min_value=-10, max_value=22)).example()
# -8.499125311228873


For HyperLinkedRelatedField (and variants) you must pass an instantiated serializer as
they require access to the request via context:

from hypothesis_drf import from_serializer

class RelatedExample(serializers.ModelSerializer):
class Meta:
model = SourceModel
fields = ('target', )

target = HyperLinkedRelatedField(queryset=TargetModel.objects.all())

from_serializer(RelatedExample(context={'request': request})).example()
# {'target': ''}

### Custom fields

Provide `hypothesis_strategy` on the field:

from rest_framework import fields
from hypothesis import strategies as st

class MyField(fields.Field):
hypothesis_strategy = st.booleans()

# ...

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for hypothesis-drf, version 1.0.0
Filename, size File type Python version Upload date Hashes
Filename, size hypothesis_drf-1.0.0-py3-none-any.whl (6.6 kB) File type Wheel Python version 3.6 Upload date Hashes View
Filename, size hypothesis-drf-1.0.0.tar.gz (8.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page