tests: improve testing by splitting tests + use six for basestring python3 compatibility
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()))
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pillow
|
||||
pyquery
|
||||
six
|
||||
|
||||
Reference in New Issue
Block a user