From fbb577643232c4eb14dfa4526284f382f4c575da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonel=20C=C3=A2mara?= Date: Sat, 19 Mar 2016 12:52:48 +0000 Subject: [PATCH] test_IS_IMAGE Removed unused and undocumented IS_IN_SUBSET Fixed _options IS_EMPTY_OR not passing arguments to the other options method Some tests for formatters --- gluon/tests/test_validators.py | 71 +++++++++++++++++++++++++++++++++- gluon/validators.py | 22 ++--------- 2 files changed, 72 insertions(+), 21 deletions(-) diff --git a/gluon/tests/test_validators.py b/gluon/tests/test_validators.py index a84467ab..ba21c1db 100644 --- a/gluon/tests/test_validators.py +++ b/gluon/tests/test_validators.py @@ -179,6 +179,8 @@ class TestValidators(unittest.TestCase): self.assertEqual(rtn, ('1', None)) rtn = IS_IN_SET([('id1','first label'), ('id2','second label')])('id1') # Redundant way self.assertEqual(rtn, ('id1', None)) + rtn = IS_IN_SET([('id1','first label'), ('id2','second label')]).options(zero=False) + self.assertEqual(rtn, [('id1', 'first label'), ('id2', 'second label')]) def test_IS_IN_DB(self): from gluon.dal import DAL, Field @@ -268,6 +270,8 @@ class TestValidators(unittest.TestCase): self.assertEqual(rtn, ('6,5', 'Enter a number')) rtn = IS_FLOAT_IN_RANGE(dot=',')('6.5') self.assertEqual(rtn, (6.5, None)) + rtn = IS_FLOAT_IN_RANGE(dot=',').formatter(0.25) + self.assertEqual(rtn, '0,25') def test_IS_DECIMAL_IN_RANGE(self): rtn = IS_DECIMAL_IN_RANGE(1,5)('4') @@ -314,6 +318,8 @@ class TestValidators(unittest.TestCase): self.assertEqual(rtn, (decimal.Decimal('6.5'), None)) rtn = IS_DECIMAL_IN_RANGE(1,5)(decimal.Decimal('4')) self.assertEqual(rtn, (decimal.Decimal('4'), None)) + rtn = IS_DECIMAL_IN_RANGE(dot=',').formatter(0.25) + self.assertEqual(rtn, '0,25') def test_IS_NOT_EMPTY(self): rtn = IS_NOT_EMPTY()(1) @@ -428,6 +434,8 @@ class TestValidators(unittest.TestCase): emails.append('a') rtn = IS_LIST_OF_EMAILS()(';'.join(emails)) self.assertEqual(rtn, ('localguy@localhost;_Yosemite.Sam@example.com;a', 'Invalid emails: a')) + rtn = IS_LIST_OF_EMAILS().formatter(['test@example.com', 'dude@example.com']) + self.assertEqual(rtn, 'test@example.com, dude@example.com') def test_IS_URL(self): rtn = IS_URL()('http://example.com') @@ -477,6 +485,8 @@ class TestValidators(unittest.TestCase): self.assertEqual(rtn, (datetime.date(2008, 3, 3), None)) rtn = v('31/03/2008') self.assertEqual(rtn, ('31/03/2008', 'oops')) + rtn = IS_DATE(format="%m/%d/%Y",error_message="oops").formatter(datetime.date(1834, 12, 14)) + self.assertEqual(rtn, '12/14/1834') def test_IS_DATETIME(self): v = IS_DATETIME(format="%m/%d/%Y %H:%M",error_message="oops") @@ -657,6 +667,10 @@ class TestValidators(unittest.TestCase): self.assertEqual(rtn, ('abc', 'Enter a valid email address')) rtn = IS_EMPTY_OR(IS_EMAIL())(' abc ') self.assertEqual(rtn, ('abc', 'Enter a valid email address')) + rtn = IS_EMPTY_OR(IS_IN_SET([('id1','first label'), ('id2','second label')], zero='zero')).options(zero=False) + self.assertEqual(rtn, [('', ''),('id1', 'first label'), ('id2', 'second label')]) + rtn = IS_EMPTY_OR(IS_IN_SET([('id1','first label'), ('id2','second label')], zero='zero')).options() + self.assertEqual(rtn, [('', 'zero'),('id1', 'first label'), ('id2', 'second label')]) def test_CLEANUP(self): rtn = CLEANUP()('helloò') @@ -733,9 +747,62 @@ class TestValidators(unittest.TestCase): 'May not include any numbers'])) ) - # TODO: def test_IS_IN_SUBSET(self): + def test_IS_IMAGE(self): + class DummyImageFile(object): + def __init__(self, filename, ext, width, height): + from StringIO import StringIO + import struct + self.filename = filename + '.' + ext + self.file = StringIO() + if ext == 'bmp': + self.file.write(b'BM') + self.file.write(b' '*16) + self.file.write(struct.pack('