Add: selections to facets; row index to RefineRow; reference to sections of tutorial '{n}' into tests; {7} adding a selection to a facet

This commit is contained in:
Paul Makepeace 2011-04-24 15:07:49 -04:00
parent 26bc2030a2
commit c7b445e851
3 changed files with 38 additions and 4 deletions

View File

@ -23,6 +23,7 @@ class Facet(object):
def __init__(self, column, expression='value', omit_blank=False, omit_error=False, select_blank=False, select_error=False, invert=False):
self.column = column
self.name = column # XXX not sure what the difference is yet
self.selections = []
self.expression = expression
self.invert = invert
self.omit_blank = omit_blank
@ -36,7 +37,7 @@ class Facet(object):
'name': self.column,
'columnName': self.column,
'expression': self.expression,
'selection': [], # XXX what is this?
'selection': self.selections,
'omitBlank': self.omit_blank,
'omitError': self.omit_error,
'selectBlank': self.select_blank,
@ -44,6 +45,16 @@ class Facet(object):
'invert': self.invert,
}
def include(self, selection):
for s in self.selections:
if s['v']['v'] == selection:
return
self.selections.append({'v': {'v': selection, 'l': selection}})
def exclude(self, selection):
self.selections = [s for s in self.selections if s['v']['v'] != selection]
class FacetResponse(object):
def __init__(self, facet):
@ -229,6 +240,7 @@ class RowsResponse(object):
def __init__(self, row_response):
self.flagged = row_response['flagged']
self.starred = row_response['starred']
self.index = row_response['i']
self.row = [c['v'] if c else None for c in row_response['cells']]
def __init__(self, rows_response):
@ -336,6 +348,8 @@ class RefineProject:
return FacetsResponse(response)
def get_rows(self, engine=None, start=0, limit=10):
response = self.do_json('get-rows', {'start': start, 'limit': limit})
response = self.do_json('get-rows', {
'sorting': "{'criteria': []}", 'engine': self.engine.as_json(),
'start': start, 'limit': limit})
return RowsResponse(response)

View File

@ -36,6 +36,15 @@ class FacetTest(unittest.TestCase):
engine.add_facet(Facet(column='Ethnicity'))
self.assertEqual(len(engine.facets), 2)
def test_selections(self):
facet = Facet('column name')
facet.include('element')
self.assertEqual(len(facet.selections), 1)
facet.include('element 2')
self.assertEqual(len(facet.selections), 2)
facet.exclude('element')
self.assertEqual(len(facet.selections), 1)
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 = json.loads(response)

View File

@ -19,6 +19,7 @@ PATH_TO_TEST_DATA = os.path.join('google', 'test', 'data')
class RefineTestCase(unittest.TestCase):
project_file = None
project = None
# {1}, {2}
def setUp(self):
self.server = RefineServer()
self.refine = Refine(self.server)
@ -67,6 +68,7 @@ class TutorialTestFacets(RefineTestCase):
project_file = 'louisiana-elected-officials.csv'
def test_get_rows(self):
# {3}
response = self.project.get_rows(limit=10)
self.assertEqual(len(response.rows), 10)
self.assertEqual(response.limit, 10)
@ -76,6 +78,7 @@ class TutorialTestFacets(RefineTestCase):
self.assertFalse(row.starred)
def test_basic_facet(self):
# {4}
facet = Facet(column='Party Code')
facets = self.project.text_facet(facet)
pc = facets.facets[0]
@ -83,14 +86,22 @@ class TutorialTestFacets(RefineTestCase):
self.assertEqual(pc.choices['D'].count, 3700)
self.assertEqual(pc.choices['N'].count, 15)
self.assertEqual(pc.blank_choice.count, 1446)
# {5}, {6}
engine = Engine(facet)
engine.add_facet(Facet(column='Ethnicity'))
ethnicity_facet = Facet(column='Ethnicity')
engine.add_facet(ethnicity_facet)
self.project.engine = engine
facets = self.project.text_facet()
e = facets.facets[1]
self.assertEqual(e.choices['B'].count, 1255)
self.assertEqual(e.choices['W'].count, 4469)
# {7}
ethnicity_facet.include('B')
facets = self.project.text_facet()
response = self.project.get_rows()
self.assertEqual(response.filtered, 1255)
indexes = [r.index for r in response.rows]
self.assertEqual(indexes, [1, 2, 3, 4, 6, 12, 18, 26, 28, 32])
if __name__ == '__main__':
unittest.main()