Skip to content

Commit

Permalink
Merge branch 'rules'
Browse files Browse the repository at this point in the history
  • Loading branch information
austinhartzheim committed Aug 11, 2015
2 parents 0b6d147 + 4855006 commit d370cd2
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
44 changes: 44 additions & 0 deletions rigidity/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,50 @@ def apply(self, value):
return buffer.value


class Boolean(Rule):
'''
Cast a string as a boolean value.
'''
ACTION_ERROR = 1
ACTION_DEFAULT = 2
ACTION_DROPROW = 3

def __init__(self, allow_null=False, action=ACTION_ERROR, default=None):
self.allow_null = allow_null
self.default = default
self.action = action

def apply(self, value):
lvalue = str(value).lower()
if lvalue in ('true', 'yes', 't', '1'):
return True
elif lvalue in ('false', 'no', 'f', '0'):
return False
elif self.allow_null and lvalue in ('null', 'none', ''):
return None
else:
if self.action == self.ACTION_ERROR:
raise ValueError('Value was not a boolean value')
elif self.action == self.ACTION_DEFAULT:
return self.default
elif self.action == self.ACTION_DROPROW:
raise rigidity.errors.DropRow()
else:
raise ValueError('Value was not a boolean value')


class Bytes(Rule):
'''
Encode a string as a bytes object.
'''

def __init__(self, encoding='utf8'):
self.encoding = encoding

def apply(self, value):
return bytes(value, self.encoding)


class Contains(Rule):
'''
Check that a string field value contains the string (or all strings
Expand Down
58 changes: 58 additions & 0 deletions tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,64 @@ def test_apply(self):
self.assertEqual(self.rule.apply('hello'), 'hello')


class TestBoolean(unittest.TestCase):

def test_apply_string_boolean(self):
rule = rules.Boolean()
self.assertTrue(rule.apply('true'))
self.assertTrue(rule.apply('yes'))
self.assertTrue(rule.apply('t'))
self.assertTrue(rule.apply('1'))
self.assertFalse(rule.apply('false'))
self.assertFalse(rule.apply('no'))
self.assertFalse(rule.apply('f'))
self.assertFalse(rule.apply('0'))
self.assertRaises(ValueError, rule.apply, 'null')
self.assertRaises(ValueError, rule.apply, 'none')
self.assertRaises(ValueError, rule.apply, '')

def test_apply_invalid_boolean(self):
rule = rules.Boolean()
self.assertRaises(ValueError, rule.apply, 'a')

def test_apply_allow_null(self):
rule = rules.Boolean(allow_null=True)
self.assertIs(None, rule.apply('null'))
self.assertIs(None, rule.apply('none'))
self.assertIs(None, rule.apply(''))

def test_apply_action_default(self):
rule = rules.Boolean(action=rules.Boolean.ACTION_DEFAULT, default='t')
self.assertEqual('t', rule.apply('a'))

def test_apply_action_droprow(self):
rule = rules.Boolean(action=rules.Boolean.ACTION_DROPROW)
self.assertRaises(errors.DropRow, rule.apply, 'a')

def test_apply_action_invalid(self):
rule = rules.Boolean(action=None)
self.assertRaises(ValueError, rule.apply, 'a')


class TestBytes(unittest.TestCase):

def test_apply(self):
rule = rules.Bytes()
self.assertEqual(rule.apply('hello'), b'hello')

def test_apply_utf16(self):
rule = rules.Bytes('utf16')
self.assertEqual(rule.apply('hello'), 'hello'.encode('utf16'))

def test_apply_encoding_mismatch(self):
'''
Attempt to encode a unicode character with the ASCII encoding
and assert that this raises an error.
'''
rule = rules.Bytes('ascii')
self.assertRaises(ValueError, rule.apply, 'ጷ')


class TestCapitalizeWords(unittest.TestCase):

def test_apply(self):
Expand Down

0 comments on commit d370cd2

Please sign in to comment.