Skip to content

Commit

Permalink
Added ReplaceValue.BLANK action to replace the old ReplaceValue.DROP
Browse files Browse the repository at this point in the history
action.
  • Loading branch information
austinhartzheim committed Jan 3, 2016
1 parent 3772dc8 commit dfb883d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 9 additions & 2 deletions rigidity/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,16 @@ class ReplaceValue(Rule):
or use a default value.
'''
#: When no replacement is found, drop the row.
ACTION_DROP = 1
ACTION_DROPROW = 1
#: When no replacement is found, return a set default value.
ACTION_DEFAULT_VALUE = 2
#: When no replacement is found, allow the original to pass through.
ACTION_PASSTHROUGH = 3
#: When no replacement is found, raise an exception.
ACTION_ERROR = 4
#: When no replacement is found, return an empty string.
ACTION_BLANK = 5
ACTION_DROP = ACTION_BLANK # Legacy support for v1.2.0; depreciated

def __init__(self, replacements={}, missing_action=ACTION_ERROR,
default_value=''):
Expand All @@ -293,10 +296,14 @@ def __init__(self, replacements={}, missing_action=ACTION_ERROR,
self.missing_action = missing_action
self.default_value = default_value

if missing_action == self.ACTION_BLANK:
self.missing_action = self.ACTION_DEFAULT_VALUE
self.default_value = ''

def apply(self, value):
if value in self.replacements:
return self.replacements[value]
elif self.missing_action == self.ACTION_DROP:
elif self.missing_action == self.ACTION_DROPROW:
raise rigidity.errors.DropRow()
elif self.missing_action == self.ACTION_PASSTHROUGH:
return value
Expand Down
11 changes: 10 additions & 1 deletion tests/test_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def test_apply_drop(self):
an empty string is returned when the value does not have an
available replacement.
'''
rule = rigidity.rules.ReplaceValue(missing_action=rigidity.rules.ReplaceValue.ACTION_DROP)
rule = rigidity.rules.ReplaceValue(missing_action=rigidity.rules.ReplaceValue.ACTION_DROPROW)
self.assertRaises(errors.DropRow, rule.apply, 'anystring')

def test_apply_default_value(self):
Expand Down Expand Up @@ -255,6 +255,15 @@ def test_apply_error(self):
self.assertRaises(IndexError, rule.apply, 'anystring')
self.assertRaises(IndexError, rule.apply, 10)

def test_apply_blank(self):
'''
Test that when the missing_action is the blank behavior, an
empty string is returned when the value does not have a
replacement.
'''
rule = rigidity.rules.ReplaceValue(missing_action=rules.ReplaceValue.ACTION_BLANK)
self.assertEqual(rule.apply('anystring'), '')

def test_apply_invalid_missing_action(self):
'''
Test that when the missing_action is set to an invalid
Expand Down

0 comments on commit dfb883d

Please sign in to comment.