Skip to content

Commit

Permalink
Add test for FilteringCSVReader with any_match and inverse arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Jan 22, 2016
1 parent 0b75a63 commit dde3e85
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 10 additions & 6 deletions csvkit/grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ def __next__(self):
def test_row(self, row):
for idx, test in self.patterns.items():
result = test(row[idx])
if result and self.any_match:
return not self.inverse # True

if not result and not self.any_match:
return self.inverse # False
if self.any_match:
if result:
return not self.inverse # True
else:
if not result:
return self.inverse # False

return not self.any_match and not self.inverse # True
if self.any_match:
return self.inverse # False
else:
return not self.inverse # True

def standardize_patterns(column_names, patterns):
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/test_grep.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,14 @@ def test_any_match(self):
self.fail("Should be no more rows left.")
except StopIteration:
pass

def test_any_match_and_inverse(self):
fcr = FilteringCSVReader(iter(self.tab2), patterns={'age': 'only', 0: '2'}, any_match=True, inverse=True)
self.assertEqual(self.tab2[0], next(fcr))
self.assertEqual(self.tab2[1], next(fcr))
self.assertEqual(self.tab2[3], next(fcr))
try:
next(fcr)
self.fail("Should be no more rows left.")
except StopIteration:
pass

0 comments on commit dde3e85

Please sign in to comment.