Add Engine.set_facets() so RefineProject.{compute_facets,get_rows}() don't lose the Engine's other attrs (i.e. mode). Split TestFacet into component classes in test_facet.py.

This commit is contained in:
Paul Makepeace 2011-04-26 16:55:07 -04:00
parent 0ba7c56ac3
commit 423a887f56
4 changed files with 57 additions and 38 deletions

View File

@ -105,7 +105,6 @@ class BlankFacet(BoolFacet):
expression='isBlank(value)', selection=selection) expression='isBlank(value)', selection=selection)
# Capitalize 'From' to get around python's reserved word. # Capitalize 'From' to get around python's reserved word.
class NumericFacet(Facet): class NumericFacet(Facet):
def __init__(self, column, From=None, to=None, select_blank=True, select_error=True, select_non_numeric=True, select_numeric=True, **options): def __init__(self, column, From=None, to=None, select_blank=True, select_error=True, select_non_numeric=True, select_numeric=True, **options):
@ -152,12 +151,15 @@ class FacetsResponse(object):
class Engine(object): class Engine(object):
def __init__(self, facets=None, mode='row-based'): def __init__(self, facets=None, mode='row-based'):
self.set_facets(facets)
self.mode = mode
def set_facets(self, facets=None):
if facets is None: if facets is None:
facets = [] facets = []
elif not isinstance(facets, list): elif not isinstance(facets, list):
facets = [facets] facets = [facets]
self.facets = facets self.facets = facets
self.mode = mode
def as_dict(self): def as_dict(self):
return { return {

View File

@ -282,13 +282,13 @@ class RefineProject:
def compute_facets(self, facets=None): def compute_facets(self, facets=None):
if facets: if facets:
self.engine = facet.Engine(facets) self.engine.set_facets(facets)
response = self.do_json('compute-facets') response = self.do_json('compute-facets')
return facet.FacetsResponse(response) return facet.FacetsResponse(response)
def get_rows(self, facets=None, sort_by=None, start=0, limit=10): def get_rows(self, facets=None, sort_by=None, start=0, limit=10):
if facets: if facets:
self.engine = facet.Engine(facets) self.engine.set_facets(facets)
if sort_by is not None: if sort_by is not None:
self.sorting = facet.Sorting(sort_by) self.sorting = facet.Sorting(sort_by)
response = self.do_json('get-rows', {'sorting': self.sorting.as_json(), response = self.do_json('get-rows', {'sorting': self.sorting.as_json(),
@ -306,7 +306,7 @@ class RefineProject:
def remove_rows(self, facets=None): def remove_rows(self, facets=None):
if facets: if facets:
self.engine = facet.Engine(facets) self.engine.set_facets(facets)
return self.do_json('remove-rows') return self.do_json('remove-rows')
def text_transform(self, column, expression, on_error='set-to-blank', def text_transform(self, column, expression, on_error='set-to-blank',

View File

@ -50,6 +50,31 @@ class FacetTest(unittest.TestCase):
self.assertEqual(facet.selection[0]['v']['v'], False) self.assertEqual(facet.selection[0]['v']['v'], False)
self.assertRaises(ValueError, FlaggedFacet, 'false') # no strings self.assertRaises(ValueError, FlaggedFacet, 'false') # no strings
def test_selections(self):
facet = TextFacet('column name')
facet.include('element')
self.assertEqual(len(facet.selection), 1)
facet.include('element 2')
self.assertEqual(len(facet.selection), 2)
facet.exclude('element')
self.assertEqual(len(facet.selection), 1)
facet.reset()
self.assertEqual(len(facet.selection), 0)
facet.include('element').include('element 2')
self.assertEqual(len(facet.selection), 2)
class EngineTest(unittest.TestCase):
def test_init(self):
engine = Engine()
self.assertEqual(engine.mode, 'row-based')
engine.mode = 'record-based'
self.assertEqual(engine.mode, 'record-based')
engine.set_facets(BlankFacet)
self.assertEqual(engine.mode, 'record-based')
engine.set_facets([BlankFacet] * 2)
self.assertEqual(len(engine), 2)
def test_serialize(self): def test_serialize(self):
engine = Engine() engine = Engine()
engine_json = engine.as_json() engine_json = engine.as_json()
@ -59,6 +84,29 @@ class FacetTest(unittest.TestCase):
facet = NumericFacet(column='column', From=1, to=5) facet = NumericFacet(column='column', From=1, to=5)
self.assertEqual(facet.as_dict(), {'from': 1, 'to': 5, 'selectBlank': True, 'name': 'column', 'selectError': True, 'expression': 'value', 'selectNumeric': True, 'columnName': 'column', 'selectNonNumeric': True, 'type': 'range'}) self.assertEqual(facet.as_dict(), {'from': 1, 'to': 5, 'selectBlank': True, 'name': 'column', 'selectError': True, 'expression': 'value', 'selectNumeric': True, 'columnName': 'column', 'selectNonNumeric': True, 'type': 'range'})
def test_add_facet(self):
facet = TextFacet(column='Party Code')
engine = Engine(facet)
engine.add_facet(TextFacet(column='Ethnicity'))
self.assertEqual(len(engine.facets), 2)
self.assertEqual(len(engine), 2)
def test_reset_remove(self):
text_facet1 = TextFacet('column name')
text_facet1.include('element')
text_facet2 = TextFacet('column name 2')
text_facet2.include('element 2')
engine = Engine([text_facet1, text_facet2])
self.assertEqual(len(engine), 2)
self.assertEqual(len(text_facet1.selection), 1)
engine.reset_all()
self.assertEqual(len(text_facet1.selection), 0)
self.assertEqual(len(text_facet2.selection), 0)
engine.remove_all()
self.assertEqual(len(engine), 0)
class SortingTest(unittest.TestCase):
def test_sorting(self): def test_sorting(self):
sorting = Sorting() sorting = Sorting()
self.assertEqual(sorting.as_json(), '{"criteria": []}') self.assertEqual(sorting.as_json(), '{"criteria": []}')
@ -78,40 +126,8 @@ class FacetTest(unittest.TestCase):
self.assertEqual(c['column'], 'date') self.assertEqual(c['column'], 'date')
self.assertEqual(c['valueType'], 'date') self.assertEqual(c['valueType'], 'date')
def test_add_facet(self):
facet = TextFacet(column='Party Code')
engine = Engine(facet)
engine.add_facet(TextFacet(column='Ethnicity'))
self.assertEqual(len(engine.facets), 2)
self.assertEqual(len(engine), 2)
def test_selections(self):
facet = TextFacet('column name')
facet.include('element')
self.assertEqual(len(facet.selection), 1)
facet.include('element 2')
self.assertEqual(len(facet.selection), 2)
facet.exclude('element')
self.assertEqual(len(facet.selection), 1)
facet.reset()
self.assertEqual(len(facet.selection), 0)
facet.include('element').include('element 2')
self.assertEqual(len(facet.selection), 2)
def test_reset_remove(self):
text_facet1 = TextFacet('column name')
text_facet1.include('element')
text_facet2 = TextFacet('column name 2')
text_facet2.include('element 2')
engine = Engine([text_facet1, text_facet2])
self.assertEqual(len(engine), 2)
self.assertEqual(len(text_facet1.selection), 1)
engine.reset_all()
self.assertEqual(len(text_facet1.selection), 0)
self.assertEqual(len(text_facet2.selection), 0)
engine.remove_all()
self.assertEqual(len(engine), 0)
class FacetsResponseTest(unittest.TestCase):
def test_facets_response(self): def test_facets_response(self):
response = """{"facets":[{"name":"Party Code","expression":"value","columnName":"Party Code","invert":false,"choices":[{"v":{"v":"D","l":"D"},"c":3700,"s":false},{"v":{"v":"R","l":"R"},"c":1613,"s":false},{"v":{"v":"N","l":"N"},"c":15,"s":false},{"v":{"v":"O","l":"O"},"c":184,"s":false}],"blankChoice":{"s":false,"c":1446}}],"mode":"row-based"}""" response = """{"facets":[{"name":"Party Code","expression":"value","columnName":"Party Code","invert":false,"choices":[{"v":{"v":"D","l":"D"},"c":3700,"s":false},{"v":{"v":"R","l":"R"},"c":1613,"s":false},{"v":{"v":"N","l":"N"},"c":15,"s":false},{"v":{"v":"O","l":"O"},"c":184,"s":false}],"blankChoice":{"s":false,"c":1446}}],"mode":"row-based"}"""
response = FacetsResponse(json.loads(response)) response = FacetsResponse(json.loads(response))

View File

@ -20,6 +20,7 @@ class RefineServerTest(refinetest.RefineTestCase):
def test_init(self): def test_init(self):
self.assertEqual(self.server.server, self.assertEqual(self.server.server,
'http://%s:%s' % (refine.REFINE_HOST, refine.REFINE_PORT)) 'http://%s:%s' % (refine.REFINE_HOST, refine.REFINE_PORT))
# strip trailing /
server = refine.RefineServer('http://refine.example/') server = refine.RefineServer('http://refine.example/')
self.assertEqual(server.server, 'http://refine.example') self.assertEqual(server.server, 'http://refine.example')