Skip to content

Commit

Permalink
full branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
tmessi committed May 13, 2016
1 parent a3835b0 commit 4fea87b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
14 changes: 14 additions & 0 deletions tests/test_yalp_grok.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ def test_conversion_failure(self):
self.assertIsNotNone(match)
self.assertEqual(match['test_str'], 'not_a_int')

def test_conversion_failure_float(self):
text = 'not_a_float'
pat = '%{WORD:test_str:float}'
match = grok_match(text, pat)
self.assertIsNotNone(match)
self.assertEqual(match['test_str'], 'not_a_float')

def test_unsupported_type(self):
text = 'unknown_type'
pat = '%{WORD:test_str:decimal}'
match = grok_match(text, pat)
self.assertIsNotNone(match)
self.assertEqual(match['test_str'], 'unknown_type')

def test_compound_matches(self):
pats_dir = os.path.join(os.path.dirname(__file__), 'test_patterns')
text = (
Expand Down
16 changes: 7 additions & 9 deletions yalp_grok/yalp_grok.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
INT = ('INT', 'POSINT', 'NONNEGINT')
FLOAT = ('BASE10NUM')
TYPES = {'int': INT, 'float': FLOAT}
CONVERSIONS = {
'int': int,
'float': float,
}

# GROK pattern/format data abstracted from logic
NAMED_PATTERN = {
Expand Down Expand Up @@ -242,22 +246,16 @@ def _apply_map(match_dict, type_map):
for name, detected_type in type_map.items():
try:
match_dict[name] = _convert(match_dict[name], detected_type)
except ValueError:
if sys.version_info[0] < 3:
sys.exc_clear()
except (ValueError, TypeError):
pass
return match_dict


def _convert(value, detected_type):
'''
Attempts conversion if value is not None.
'''
if value:
if detected_type == 'int':
return int(value)
if detected_type == 'float':
return float(value)
return value
return CONVERSIONS[detected_type](value)


PREDEFINED_PATTERNS = _reload_patterns(DEFAULT_PATTERNS_DIRS)

0 comments on commit 4fea87b

Please sign in to comment.