Fix bugs in draw_signature and add tests
This commit is contained in:
@@ -2,3 +2,4 @@ from .widgets import JSignatureWidgetTest
|
||||
from .forms import JSignatureFormFieldTest
|
||||
from .fields import JSignatureFieldTest
|
||||
from .mixins import JSignatureFieldsMixinTest
|
||||
from .utils import UtilsTest
|
||||
|
||||
32
jsignature/tests/utils.py
Normal file
32
jsignature/tests/utils.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import json
|
||||
import os
|
||||
import imghdr
|
||||
from PIL import Image
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from ..utils import draw_signature
|
||||
|
||||
DUMMY_VALUE = [{"x":[205,210],"y":[59,63]},{"x":[205,207],"y":[67,64]}]
|
||||
DUMMY_STR_VALUE = json.dumps(DUMMY_VALUE)
|
||||
|
||||
class UtilsTest(SimpleTestCase):
|
||||
|
||||
def test_inputs(self):
|
||||
# Bad str value
|
||||
self.assertRaises(ValueError, draw_signature, 'foo_bar')
|
||||
# Bad type value
|
||||
self.assertRaises(ValueError, draw_signature, object())
|
||||
# Good list value
|
||||
draw_signature(DUMMY_VALUE)
|
||||
# Good str value
|
||||
draw_signature(DUMMY_STR_VALUE)
|
||||
|
||||
def test_outputs(self):
|
||||
# As a file
|
||||
output = draw_signature(DUMMY_VALUE, as_file=True)
|
||||
self.assertTrue(os.path.isfile(output))
|
||||
self.assertIsNotNone(imghdr.what(output))
|
||||
# As an Image
|
||||
output = draw_signature(DUMMY_VALUE)
|
||||
self.assertTrue(issubclass(output.__class__, Image.Image))
|
||||
self.assertTrue(all(output.getbbox()))
|
||||
@@ -24,8 +24,8 @@ def draw_signature(data, as_file=False):
|
||||
raise ValueError
|
||||
|
||||
# Compute box
|
||||
width = max(chain(*[d['x'] for d in data])) + 10
|
||||
height = max(chain(*[d['y'] for d in data])) + 10
|
||||
width = max(chain(*[d['x'] for d in drawing])) + 10
|
||||
height = max(chain(*[d['y'] for d in drawing])) + 10
|
||||
|
||||
# Draw image
|
||||
im = Image.new("RGBA", (width*AA, height*AA))
|
||||
@@ -45,7 +45,7 @@ def draw_signature(data, as_file=False):
|
||||
if as_file:
|
||||
ret = im._dump(format='PNG')
|
||||
else:
|
||||
ret = img
|
||||
ret = im
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
Reference in New Issue
Block a user