def test_updateIndexFromCallableWithNone(self): uidutil = IntIdsStub() provideUtility(uidutil, IIntIds) catalog = Catalog() index = FieldIndex('getAuthor', None, field_callable=True) catalog['author'] = index ob1 = stoopidCallable(author = "joe") ob1id = uidutil.register(ob1) catalog.index_doc(ob1id, ob1) res = catalog.searchResults(author=('joe','joe')) names = [x.author for x in res] names.sort() self.assertEqual(len(names), 1) self.assertEqual(names, ['joe']) ob1.author = None catalog.index_doc(ob1id, ob1) #the index must be empty now because None values are never indexed res = catalog.searchResults(author=(None, None)) self.assertEqual(len(res), 0)
def test_basicsearch(self): """Test the simple search results interface.""" self._frob_intidutil(ints=0) catalog = Catalog() catalog['simiantype'] = StubIndex('simiantype', None) catalog['name'] = StubIndex('name', None) catalog.updateIndexes() res = catalog.searchResults(simiantype='monkey') names = [x.name for x in res] names.sort() self.assertEqual(len(names), 3) self.assertEqual(names, ['bobo', 'bubbles', 'ginger']) res = catalog.searchResults(name='bobo') names = [x.simiantype for x in res] names.sort() self.assertEqual(len(names), 2) self.assertEqual(names, ['bonobo', 'monkey']) res = catalog.searchResults(simiantype='punyhuman', name='anthony') self.assertEqual(len(res), 1) ob = iter(res).next() self.assertEqual((ob.name, ob.simiantype), ('anthony', 'punyhuman')) res = catalog.searchResults(simiantype='ape', name='bobo') self.assertEqual(len(res), 0) res = catalog.searchResults(simiantype='ape', name='mwumi') self.assertEqual(len(res), 0) self.assertRaises(KeyError, catalog.searchResults, simiantype='monkey', hat='beret')
def test_IndexRaisingValueGetter(self): """We can have indexes whose values are determined by callable methods. Raising an exception in the method should not be silently ignored That would cause index corruption -- the index would be out of sync""" uidutil = IntIdsStub() provideUtility(uidutil, IIntIds) catalog = Catalog() index = FieldIndex('getAuthor', None, field_callable=True) catalog['author'] = index ob1 = stoopidCallable(author = "joe") ob1id = uidutil.register(ob1) catalog.index_doc(ob1id, ob1) res = catalog.searchResults(author=('joe','joe')) names = [x.author for x in res] names.sort() self.assertEqual(len(names), 1) self.assertEqual(names, ['joe']) ob2 = stoopidCallable() # no author here, will raise AttributeError ob2id = uidutil.register(ob2) try: catalog.index_doc(ob2id, ob2) self.fail("AttributeError exception should be raised") except AttributeError: #this is OK, we WANT to have the exception pass