bugfix: generic search would raise an exception on certain input values

This commit is contained in:
Tal Einat 2014-03-19 23:50:33 +02:00
parent 39000bda80
commit 3b84e6e5fb
2 changed files with 29 additions and 4 deletions

View File

@ -3,7 +3,7 @@ from fuzzysearch.common import Match
GenericSearchCandidate = namedtuple(
'SuperKevinSearchCandidate',
'GenericSearchCandidate',
['start', 'subseq_index', 'l_dist', 'n_subs', 'n_ins', 'n_dels'],
)
@ -29,9 +29,12 @@ def find_near_matches_generic_linear_programming(subsequence, sequence,
# optimization: prepare some often used things in advance
_subseq_len = len(subsequence)
if max_l_dist is None or \
max_l_dist >= max_substitutions + max_insertions + max_deletions:
max_l_dist = max_substitutions + max_insertions + max_deletions
maxes_sum = sum(
(x if x is not None else 0)
for x in [max_substitutions, max_insertions, max_deletions]
)
if max_l_dist is None or max_l_dist >= maxes_sum:
max_l_dist = maxes_sum
candidates = []
for index, char in enumerate(sequence):

View File

@ -125,3 +125,25 @@ class TestGenericSearch(unittest.TestCase):
self.search('bde', 'abcdefg', 1, 1, 1, 3),
))
)
def test_argument_handling(self):
# check that no exception is raised when some values are None
self.assertEqual(
self.search('a', 'b', 0, None, None, None),
[],
)
self.assertEqual(
self.search('a', 'b', None, 0, None, None),
[],
)
self.assertEqual(
self.search('a', 'b', None, None, 0, None),
[],
)
self.assertEqual(
self.search('a', 'b', None, None, None, 0),
[],
)