From 124fe21d06329ea3d2b0329700bdc9f80fd68964 Mon Sep 17 00:00:00 2001 From: Gagaro Date: Thu, 4 Dec 2014 10:22:31 +0100 Subject: [PATCH 1/5] tests: upgrade quicktest.py + improve travis configuration --- .travis.yml | 31 +++++++++++++++++++++++++++---- quicktest.py | 32 +++++++++++++++++++++++++------- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 95b869d..39c5da0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,42 @@ language: python python: - - "2.7" + - 2.6 + - 2.7 + - 3.2 + - 3.3 + - 3.4 env: - - DJANGO_VERSION=1.4 - - DJANGO_VERSION=1.5 + - DJANGO_VERSION=1.4 MODULE=jsignature + - DJANGO_VERSION=1.5 MODULE=jsignature + - DJANGO_VERSION=1.6 MODULE=jsignature.tests + - DJANGO_VERSION=1.7 MODULE=jsignature.tests install: - pip install -r requirements.txt --use-mirrors - pip install -q Django==$DJANGO_VERSION --use-mirrors - pip install coverage -script: coverage run quicktest.py jsignature +script: coverage run quicktest.py $MODULE after_success: - pip install coveralls - coveralls + +# We need to exclude old versions of Django for tests with python 3. +# We need to exclude old versions of Python for tests with Django >= 1.7. +matrix: + exclude: + - python: 3.2 + env: DJANGO_VERSION=1.4 + - python: 3.3 + env: DJANGO_VERSION=1.4 + - python: 3.4 + env: DJANGO_VERSION=1.4 + - python: 3.4 + env: DJANGO_VERSION=1.5 + - python: 3.4 + env: DJANGO_VERSION=1.6 + - python: 2.6 + env: DJANGO_VERSION=1.7 diff --git a/quicktest.py b/quicktest.py index 358460d..885e736 100644 --- a/quicktest.py +++ b/quicktest.py @@ -3,6 +3,7 @@ import sys import argparse from django.conf import settings + class QuickDjangoTest(object): """ A quick way to run the Django test suite without a fully-configured project. @@ -16,8 +17,6 @@ class QuickDjangoTest(object): """ DIRNAME = os.path.dirname(__file__) INSTALLED_APPS = ( - 'django.contrib.contenttypes', - 'django.contrib.sessions', ) def __init__(self, *args, **kwargs): @@ -29,7 +28,10 @@ class QuickDjangoTest(object): Fire up the Django test suite developed for version 1.2 """ settings.configure( - DATABASES={ + TEMPLATE_DIRS = ('jsignature/templates/',), + ROOT_URLCONF = 'jsignature.tests', + DEBUG = True, + DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(self.DIRNAME, 'database.db'), @@ -39,11 +41,27 @@ class QuickDjangoTest(object): 'PORT': '', } }, - INSTALLED_APPS=self.INSTALLED_APPS + self.apps, + MIDDLEWARE_CLASSES = ( + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + ), + INSTALLED_APPS = self.INSTALLED_APPS + self.apps ) - from django.test.simple import DjangoTestSuiteRunner - failures = DjangoTestSuiteRunner().run_tests(self.apps, verbosity=1) - if failures: # pragma: no cover + # Setup is needed for Django >= 1.7 + import django + if hasattr(django, 'setup'): + django.setup() + try: + from django.test.runner import DiscoverRunner + failures = DiscoverRunner().run_tests(self.apps, verbosity=1) + except ImportError: + # DjangoTestSuiteRunner has been deprecated in Django 1.7 + from django.test.simple import DjangoTestSuiteRunner + failures = DjangoTestSuiteRunner().run_tests(self.apps, verbosity=1) + if failures: # pragma: no cover sys.exit(failures) if __name__ == '__main__': From 70b4f2e9b6b5f970df9c429d6cef73de0cc4da50 Mon Sep 17 00:00:00 2001 From: Gagaro Date: Thu, 4 Dec 2014 10:25:28 +0100 Subject: [PATCH 2/5] fix: .travis.yml matrix exclude --- .travis.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 39c5da0..6a0e139 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,14 +29,14 @@ after_success: matrix: exclude: - python: 3.2 - env: DJANGO_VERSION=1.4 + env: DJANGO_VERSION=1.4 MODULE=jsignature - python: 3.3 - env: DJANGO_VERSION=1.4 + env: DJANGO_VERSION=1.4 MODULE=jsignature - python: 3.4 - env: DJANGO_VERSION=1.4 + env: DJANGO_VERSION=1.4 MODULE=jsignature - python: 3.4 - env: DJANGO_VERSION=1.5 + env: DJANGO_VERSION=1.5 MODULE=jsignature - python: 3.4 - env: DJANGO_VERSION=1.6 + env: DJANGO_VERSION=1.6 MODULE=jsignature.tests - python: 2.6 - env: DJANGO_VERSION=1.7 + env: DJANGO_VERSION=1.7 MODULE=jsignature.tests From 8e912f97384e243a8135cbff554651c38113fad7 Mon Sep 17 00:00:00 2001 From: Gagaro Date: Thu, 4 Dec 2014 11:31:34 +0100 Subject: [PATCH 3/5] tests: improve testing by splitting tests + use six for basestring python3 compatibility --- jsignature/fields.py | 3 ++- jsignature/tests/fields.py | 35 +++++++++++++++++++++++++---------- jsignature/tests/forms.py | 11 +++++++---- jsignature/tests/mixins.py | 9 +++++++-- jsignature/tests/utils.py | 18 ++++++++++-------- jsignature/tests/widgets.py | 5 +++-- jsignature/widgets.py | 3 ++- requirements.txt | 3 ++- 8 files changed, 58 insertions(+), 29 deletions(-) diff --git a/jsignature/fields.py b/jsignature/fields.py index 0eba34d..78a7f4f 100644 --- a/jsignature/fields.py +++ b/jsignature/fields.py @@ -3,6 +3,7 @@ with jSignature jQuery plugin """ import json +import six from django.db import models from django.core.exceptions import ValidationError from .forms import ( @@ -37,7 +38,7 @@ class JSignatureField(models.Field): def get_prep_value(self, value): if value in JSIGNATURE_EMPTY_VALUES: return None - elif isinstance(value, basestring): + elif isinstance(value, six.string_types): return value elif isinstance(value, list): return json.dumps(value) diff --git a/jsignature/tests/fields.py b/jsignature/tests/fields.py index 38ce27d..870b2b6 100644 --- a/jsignature/tests/fields.py +++ b/jsignature/tests/fields.py @@ -1,4 +1,5 @@ import json +import six from django.test import SimpleTestCase from django.core.exceptions import ValidationError @@ -8,35 +9,49 @@ from ..forms import JSignatureField as JSignatureFormField class JSignatureFieldTest(SimpleTestCase): - def test_to_python(self): + def test_to_python_empty(self): f = JSignatureField() - # Empty values for val in ['', [], '[]']: self.assertIsNone(f.to_python(val)) - # Correct values + + def test_to_python_correct_value_python(self): + f = JSignatureField() val = [{"x": [1, 2], "y": [3, 4]}] self.assertEquals(val, f.to_python(val)) + + def test_to_python_correct_value_json(self): + f = JSignatureField() + val = [{"x": [1, 2], "y": [3, 4]}] val_str = '[{"x":[1,2], "y":[3,4]}]' self.assertEquals(val, f.to_python(val_str)) - # Incorrect values + + def test_to_python_incorrect_value(self): + f = JSignatureField() val = 'foo' self.assertRaises(ValidationError, f.to_python, val) - def test_get_prep_value(self): + def test_get_prep_value_empty(self): f = JSignatureField() - # Empty values for val in ['', [], '[]']: self.assertIsNone(f.get_prep_value(val)) - # Correct values + + def test_get_prep_value_correct_values_python(self): + f = JSignatureField() val = [{"x": [1, 2], "y": [3, 4]}] val_prep = f.get_prep_value(val) - self.assertIsInstance(val_prep, basestring) + self.assertIsInstance(val_prep, six.string_types) self.assertEquals(val, json.loads(val_prep)) + + def test_get_prep_value_correct_values_json(self): + f = JSignatureField() + val = [{"x": [1, 2], "y": [3, 4]}] val_str = '[{"x":[1,2], "y":[3,4]}]' val_prep = f.get_prep_value(val_str) - self.assertIsInstance(val_prep, basestring) + self.assertIsInstance(val_prep, six.string_types) self.assertEquals(val, json.loads(val_prep)) - # Incorrect values + + def test_get_prep_value_incorrect_values(self): + f = JSignatureField() val = type('Foo') self.assertRaises(ValidationError, f.get_prep_value, val) diff --git a/jsignature/tests/forms.py b/jsignature/tests/forms.py index 1e01a3a..2325590 100644 --- a/jsignature/tests/forms.py +++ b/jsignature/tests/forms.py @@ -11,14 +11,17 @@ class JSignatureFormFieldTest(SimpleTestCase): f = JSignatureField() self.assertIsInstance(f.widget, JSignatureWidget) - def test_to_python(self): + def test_to_python_empty_values(self): f = JSignatureField() - # Empty values for val in ['', [], '[]']: self.assertIsNone(f.to_python(val)) - # Correct values + + def test_to_python_correct_values(self): + f = JSignatureField() val = '[{"x":[1,2], "y":[3,4]}]' self.assertEquals([{'x': [1, 2], 'y': [3, 4]}], f.to_python(val)) - # Incorrect values + + def test_to_python_incorrect_values(self): + f = JSignatureField() val = 'foo' self.assertRaises(ValidationError, f.to_python, val) diff --git a/jsignature/tests/mixins.py b/jsignature/tests/mixins.py index 5d52a7a..838355b 100644 --- a/jsignature/tests/mixins.py +++ b/jsignature/tests/mixins.py @@ -19,7 +19,7 @@ class JSignatureFieldsMixinTest(SimpleTestCase): def tearDown(self): settings.INSTALLED_APPS = self.old_installed_apps - def test_save(self): + def test_save_create(self): # If an object is created signed, signature date must be set signature_value = [{"x": [1, 2], "y": [3, 4]}] i = JSignatureTestModel(signature=signature_value) @@ -27,16 +27,19 @@ class JSignatureFieldsMixinTest(SimpleTestCase): i = JSignatureTestModel.objects.get(pk=i.pk) self.assertEqual(date.today(), i.signature_date.date()) + def test_save_no_change(self): # If signature doesn't change, signature date must not be updated + signature_value = [{"x": [1, 2], "y": [3, 4]}] i = JSignatureTestModel(signature=signature_value) i.save() i.signature_date = date(2013, 1, 1) - i.signature = signature_value i.save() i = JSignatureTestModel.objects.get(pk=i.pk) self.assertEqual(date(2013, 1, 1), i.signature_date.date()) + def test_save_change(self): # If signature changes, signature date must be updated too + signature_value = [{"x": [1, 2], "y": [3, 4]}] new_signature_value = [{"x": [5, 6], "y": [7, 8]}] i = JSignatureTestModel(signature=signature_value, signature_date=date(2013, 1, 1)) @@ -47,7 +50,9 @@ class JSignatureFieldsMixinTest(SimpleTestCase): i = JSignatureTestModel.objects.get(pk=i.pk) self.assertEqual(date.today(), i.signature_date.date()) + def test_save_none(self): # If sinature is set to None, it must be the same for signature_date + signature_value = [{"x": [1, 2], "y": [3, 4]}] i = JSignatureTestModel(signature=signature_value) i.save() i.signature = None diff --git a/jsignature/tests/utils.py b/jsignature/tests/utils.py index a20bd29..cc38d96 100644 --- a/jsignature/tests/utils.py +++ b/jsignature/tests/utils.py @@ -13,22 +13,24 @@ DUMMY_STR_VALUE = json.dumps(DUMMY_VALUE) class UtilsTest(SimpleTestCase): - def test_inputs(self): - # Bad str value + def test_inputs_bad_str_value(self): self.assertRaises(ValueError, draw_signature, 'foo_bar') - # Bad type value + + def test_inputs_bad_type_value(self): self.assertRaises(ValueError, draw_signature, object()) - # Good list value + + def test_inputs_good_list_value(self): draw_signature(DUMMY_VALUE) - # Good str value + + def test_inputs_good_str_value(self): draw_signature(DUMMY_STR_VALUE) - def test_outputs(self): - # As a file + def test_outputs_as_file(self): output = draw_signature(DUMMY_VALUE, as_file=True) self.assertTrue(os.path.isfile(output)) self.assertIsNotNone(imghdr.what(output)) - # As an Image + + def test_outputs_as_image(self): output = draw_signature(DUMMY_VALUE) self.assertTrue(issubclass(output.__class__, Image.Image)) self.assertTrue(all(output.getbbox())) diff --git a/jsignature/tests/widgets.py b/jsignature/tests/widgets.py index d8457f7..5d4be07 100644 --- a/jsignature/tests/widgets.py +++ b/jsignature/tests/widgets.py @@ -1,5 +1,6 @@ import json from pyquery import PyQuery as pq +import six from django.test import SimpleTestCase from django.core.exceptions import ValidationError @@ -48,7 +49,7 @@ class JSignatureWidgetTest(SimpleTestCase): w = JSignatureWidget() val = [{"x": [1, 2], "y": [3, 4]}] val_prep = w.prep_value(val) - self.assertIsInstance(val_prep, basestring) + self.assertIsInstance(val_prep, six.string_types) self.assertEquals(val, json.loads(val_prep)) def test_prep_value_correct_values_json(self): @@ -56,7 +57,7 @@ class JSignatureWidgetTest(SimpleTestCase): val = [{"x": [1, 2], "y": [3, 4]}] val_str = '[{"x":[1,2], "y":[3,4]}]' val_prep = w.prep_value(val_str) - self.assertIsInstance(val_prep, basestring) + self.assertIsInstance(val_prep, six.string_types) self.assertEquals(val, json.loads(val_prep)) def test_prep_value_incorrect_values(self): diff --git a/jsignature/widgets.py b/jsignature/widgets.py index 7d12bc9..de90412 100644 --- a/jsignature/widgets.py +++ b/jsignature/widgets.py @@ -3,6 +3,7 @@ with jSignature jQuery plugin """ import json +import six from django.template.loader import render_to_string from django.forms.widgets import HiddenInput @@ -51,7 +52,7 @@ class JSignatureWidget(HiddenInput): """ Prepare value before effectively render widget """ if value in JSIGNATURE_EMPTY_VALUES: return "[]" - elif isinstance(value, basestring): + elif isinstance(value, six.string_types): return value elif isinstance(value, list): return json.dumps(value) diff --git a/requirements.txt b/requirements.txt index 1d189fb..170f7c9 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pillow -pyquery \ No newline at end of file +pyquery +six From c15074873b82cc6b43aaae3a299d6ca2d613f1d7 Mon Sep 17 00:00:00 2001 From: Gagaro Date: Thu, 4 Dec 2014 12:27:25 +0100 Subject: [PATCH 4/5] fix: python 3 compatibility --- jsignature/fields.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jsignature/fields.py b/jsignature/fields.py index 78a7f4f..b80f69f 100644 --- a/jsignature/fields.py +++ b/jsignature/fields.py @@ -6,17 +6,18 @@ import json import six from django.db import models from django.core.exceptions import ValidationError +from django.utils.six import with_metaclass + from .forms import ( JSignatureField as JSignatureFormField, JSIGNATURE_EMPTY_VALUES) -class JSignatureField(models.Field): +class JSignatureField(with_metaclass(models.SubfieldBase, models.Field)): """ A model field handling a signature captured with jSignature """ description = "A signature captured with jSignature" - __metaclass__ = models.SubfieldBase def get_internal_type(self): return 'TextField' From 880033d3a581b53617196dbf767ce721dd78316a Mon Sep 17 00:00:00 2001 From: Gagaro Date: Thu, 4 Dec 2014 12:34:49 +0100 Subject: [PATCH 5/5] fix: django.utils.six not available before django 1.6 --- jsignature/fields.py | 4 ++-- jsignature/tests/fields.py | 1 + jsignature/widgets.py | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/jsignature/fields.py b/jsignature/fields.py index b80f69f..926fe35 100644 --- a/jsignature/fields.py +++ b/jsignature/fields.py @@ -4,16 +4,16 @@ """ import json import six + from django.db import models from django.core.exceptions import ValidationError -from django.utils.six import with_metaclass from .forms import ( JSignatureField as JSignatureFormField, JSIGNATURE_EMPTY_VALUES) -class JSignatureField(with_metaclass(models.SubfieldBase, models.Field)): +class JSignatureField(six.with_metaclass(models.SubfieldBase, models.Field)): """ A model field handling a signature captured with jSignature """ diff --git a/jsignature/tests/fields.py b/jsignature/tests/fields.py index 870b2b6..5e520aa 100644 --- a/jsignature/tests/fields.py +++ b/jsignature/tests/fields.py @@ -1,5 +1,6 @@ import json import six + from django.test import SimpleTestCase from django.core.exceptions import ValidationError diff --git a/jsignature/widgets.py b/jsignature/widgets.py index de90412..0b56187 100644 --- a/jsignature/widgets.py +++ b/jsignature/widgets.py @@ -9,6 +9,7 @@ from django.template.loader import render_to_string from django.forms.widgets import HiddenInput from django.core import validators from django.core.exceptions import ValidationError + from django.utils.safestring import mark_safe from django.utils.translation import ugettext_lazy as _ from jsignature.settings import JSIGNATURE_DEFAULT_CONFIG