Remove redundant add facet test. Add Facet.reset(). Add tests for filtering on 2nd facet, and reset'ing both.

This commit is contained in:
Paul Makepeace 2011-04-24 15:45:32 -04:00
parent a2fa25d4e4
commit 686723445b
3 changed files with 27 additions and 15 deletions

View File

@ -44,16 +44,18 @@ class Facet(object):
'selectError': self.select_error, 'selectError': self.select_error,
'invert': self.invert, 'invert': self.invert,
} }
def include(self, selection): def include(self, selection):
for s in self.selections: for s in self.selections:
if s['v']['v'] == selection: if s['v']['v'] == selection:
return return
self.selections.append({'v': {'v': selection, 'l': selection}}) self.selections.append({'v': {'v': selection, 'l': selection}})
def exclude(self, selection): def exclude(self, selection):
self.selections = [s for s in self.selections if s['v']['v'] != selection] self.selections = [s for s in self.selections if s['v']['v'] != selection]
def reset(self):
self.selections = []
class FacetResponse(object): class FacetResponse(object):

View File

@ -20,21 +20,19 @@ class FacetTest(unittest.TestCase):
facet = Facet('column name') facet = Facet('column name')
engine = Engine(facet) engine = Engine(facet)
self.assertTrue(str(engine)) self.assertTrue(str(engine))
facet2 = Facet('Ethnicity')
engine.add_facet(facet2)
self.assertEqual(len(engine.facets), 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()
self.assertEqual(engine_json, '{"facets": [], "mode": "row-based"}') self.assertEqual(engine_json, '{"facets": [], "mode": "row-based"}')
def test_add_facet(self): def test_add_facet(self):
facet = Facet(column='Party Code') facet = Facet(column='Party Code')
engine = Engine(facet) engine = Engine(facet)
engine.add_facet(Facet(column='Ethnicity')) engine.add_facet(Facet(column='Ethnicity'))
self.assertEqual(len(engine.facets), 2) self.assertEqual(len(engine.facets), 2)
self.assertEqual(len(engine), 2)
def test_selections(self): def test_selections(self):
facet = Facet('column name') facet = Facet('column name')
@ -43,13 +41,16 @@ class FacetTest(unittest.TestCase):
facet.include('element 2') facet.include('element 2')
self.assertEqual(len(facet.selections), 2) self.assertEqual(len(facet.selections), 2)
facet.exclude('element') facet.exclude('element')
self.assertEqual(len(facet.selections), 1) self.assertEqual(len(facet.selections), 1)
facet.reset()
self.assertEqual(len(facet.selections), 0)
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 = json.loads(response) response = json.loads(response)
facets = FacetsResponse(response) facets = FacetsResponse(response)
self.assertEqual(facets.facets[0].choices['D'].count, 3700) self.assertEqual(facets.facets[0].choices['D'].count, 3700)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -79,15 +79,15 @@ class TutorialTestFacets(RefineTestCase):
def test_basic_facet(self): def test_basic_facet(self):
# {4} # {4}
facet = Facet(column='Party Code') party_code_facet = Facet(column='Party Code')
response = self.project.text_facet(facet) response = self.project.text_facet(party_code_facet)
pc = response.facets[0] pc = response.facets[0]
self.assertEqual(pc.name, 'Party Code') self.assertEqual(pc.name, 'Party Code')
self.assertEqual(pc.choices['D'].count, 3700) self.assertEqual(pc.choices['D'].count, 3700)
self.assertEqual(pc.choices['N'].count, 15) self.assertEqual(pc.choices['N'].count, 15)
self.assertEqual(pc.blank_choice.count, 1446) self.assertEqual(pc.blank_choice.count, 1446)
# {5}, {6} # {5}, {6}
engine = Engine(facet) engine = Engine(party_code_facet)
ethnicity_facet = Facet(column='Ethnicity') ethnicity_facet = Facet(column='Ethnicity')
engine.add_facet(ethnicity_facet) engine.add_facet(ethnicity_facet)
self.project.engine = engine self.project.engine = engine
@ -108,7 +108,16 @@ class TutorialTestFacets(RefineTestCase):
self.assertEqual(pc.choices['D'].count, 1179) self.assertEqual(pc.choices['D'].count, 1179)
self.assertEqual(pc.choices['R'].count, 11) self.assertEqual(pc.choices['R'].count, 11)
self.assertEqual(pc.blank_choice.count, 46) self.assertEqual(pc.blank_choice.count, 46)
# {9}
party_code_facet.include('R')
response = self.project.text_facet()
e = response.facets[1]
self.assertEqual(e.choices['B'].count, 11)
# {10}
party_code_facet.reset()
ethnicity_facet.reset()
response = self.project.get_rows()
self.assertEqual(response.filtered, 6958)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()