From e18c5634a0f42b4ef3225351a4cf49080bbc2640 Mon Sep 17 00:00:00 2001 From: Ian Foote Date: Wed, 24 Jun 2015 17:11:19 +0100 Subject: [PATCH] Add hypothesis test for APNSDevice serializer --- .gitignore | 1 + requirements.txt | 1 + .../tests/strategies.py | 17 +++++++++++++++++ .../tests/test_serializers.py | 7 +++++++ setup.cfg | 2 +- 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 rest_framework_push_notifications/tests/strategies.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4b87615 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.hypothesis diff --git a/requirements.txt b/requirements.txt index c14eb5c..9112a59 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,5 +6,6 @@ django==1.8.2 factory-boy==2.5.2 flake8==2.4.1 flake8-import-order==0.5.1 +hypothesis==1.6.2 incuna-test-utils==6.2.7 psycopg2==2.6.1 diff --git a/rest_framework_push_notifications/tests/strategies.py b/rest_framework_push_notifications/tests/strategies.py new file mode 100644 index 0000000..52b7182 --- /dev/null +++ b/rest_framework_push_notifications/tests/strategies.py @@ -0,0 +1,17 @@ +from uuid import uuid4 + +from hypothesis.searchstrategy import BasicStrategy +from hypothesis.strategies import basic, booleans, fixed_dictionaries, text + + +class UUIDStrategy(BasicStrategy): + def generate(self, random, parameter_value): + return uuid4() + + +APNSDeviceData = fixed_dictionaries({ + 'registration_id': text(min_size=1, max_size=64), + 'name': text(max_size=255), + 'device_id': basic(UUIDStrategy), + 'active': booleans(), +}) diff --git a/rest_framework_push_notifications/tests/test_serializers.py b/rest_framework_push_notifications/tests/test_serializers.py index cced8a8..2a93180 100644 --- a/rest_framework_push_notifications/tests/test_serializers.py +++ b/rest_framework_push_notifications/tests/test_serializers.py @@ -1,5 +1,7 @@ from django.test import TestCase +from hypothesis import given +from .strategies import APNSDeviceData from .. import serializers @@ -17,3 +19,8 @@ def test_registration_id_required(self): serializer = serializers.APNSDevice(data={}) self.assertFalse(serializer.is_valid()) self.assertIn('registration_id', serializer.errors) + + @given(APNSDeviceData) + def test_validation(self, data): + serializer = serializers.APNSDevice(data=data) + self.assertTrue(serializer.is_valid(), serializer.errors) diff --git a/setup.cfg b/setup.cfg index 5f70906..8e10266 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [flake8] max-line-length = 90 -exclude = *migrations* +exclude = *migrations*,.hypothesis/* statistics = true application-import-names = rest_framework_push_notifications, tests import-order-style = google