filter test improvements

This commit is contained in:
James Turk 2010-02-21 12:01:04 -05:00
parent 30cb36e17e
commit de3155cb06
2 changed files with 34 additions and 11 deletions

View File

@ -0,0 +1,7 @@
import unittest
from saucebrush.tests.filters import FilterTestCase
filter_suite = unittest.TestLoader().loadTestsFromTestCase(FilterTestCase)
if __name__ == '__main__':
unittest.main()

View File

@ -4,7 +4,7 @@ import types
from saucebrush.filters import (Filter, YieldFilter, FieldFilter, from saucebrush.filters import (Filter, YieldFilter, FieldFilter,
ConditionalFilter, FieldModifier, ConditionalFilter, FieldModifier,
FieldRemover, FieldMerger, FieldAdder, FieldRemover, FieldMerger, FieldAdder,
FieldCopier, FieldRenamer) FieldCopier, FieldRenamer, Unique)
class DummyRecipe(object): class DummyRecipe(object):
rejected_record = None rejected_record = None
@ -17,6 +17,13 @@ class Doubler(Filter):
def process_record(self, record): def process_record(self, record):
return record*2 return record*2
class OddRemover(Filter):
def process_record(self, record):
if record % 2 == 0:
return record
else:
return None # explicitly return None
class ListFlattener(YieldFilter): class ListFlattener(YieldFilter):
def process_record(self, record): def process_record(self, record):
for item in record: for item in record:
@ -26,7 +33,7 @@ class FieldDoubler(FieldFilter):
def process_field(self, item): def process_field(self, item):
return item*2 return item*2
class OddRemover(ConditionalFilter): class ConditionalOddRemover(ConditionalFilter):
def test_record(self, record): def test_record(self, record):
# return True for even values # return True for even values
return record % 2 == 0 return record % 2 == 0
@ -61,6 +68,13 @@ class FilterTestCase(unittest.TestCase):
self.assertEquals(type(result), types.GeneratorType) self.assertEquals(type(result), types.GeneratorType)
self.assertEquals(list(result), [2,4,6]) self.assertEquals(list(result), [2,4,6])
def test_simple_filter_return_none(self):
cf = OddRemover()
result = cf.attach(range(10))
# ensure only even numbers remain
self.assertEquals(list(result), [0,2,4,6,8])
def test_simple_yield_filter(self): def test_simple_yield_filter(self):
lf = ListFlattener() lf = ListFlattener()
result = lf.attach([[1],[2,3],[4,5,6]]) result = lf.attach([[1],[2,3],[4,5,6]])
@ -79,7 +93,7 @@ class FilterTestCase(unittest.TestCase):
self.assert_filter_result(ff, expected_data) self.assert_filter_result(ff, expected_data)
def test_conditional_filter(self): def test_conditional_filter(self):
cf = OddRemover() cf = ConditionalOddRemover()
result = cf.attach(range(10)) result = cf.attach(range(10))
# ensure only even numbers remain # ensure only even numbers remain
@ -175,15 +189,17 @@ class FilterTestCase(unittest.TestCase):
{'x':1, 'y':10, 'c':100}] {'x':1, 'y':10, 'c':100}]
self.assert_filter_result(fr, expected_data) self.assert_filter_result(fr, expected_data)
# splitter # TODO: splitter & flattner tests?
# flatteners?
# unique
# unicode filter
# string filter
# phone # cleaner def test_unique_filter(self):
# date cleaner u = Unique()
# name cleaner in_data = [{'a': 77}, {'a':33}, {'a': 77}]
expected_data = [{'a': 77}, {'a':33}]
result = u.attach(in_data)
self.assertEquals(list(result), expected_data)
# TODO: unicode & string filter tests
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()