def test(self): u = IntIds() obj = P() obj._p_jar = ConnectionStub() self.assertRaises(KeyError, u.getId, obj) self.assertRaises(KeyError, u.getId, P()) self.assert_(u.queryId(obj) is None) self.assert_(u.queryId(obj, 42) is 42) self.assert_(u.queryId(P(), 42) is 42) self.assert_(u.queryObject(42) is None) self.assert_(u.queryObject(42, obj) is obj) uid = u.register(obj) self.assert_(u.getObject(uid) is obj) self.assert_(u.queryObject(uid) is obj) self.assertEquals(u.getId(obj), uid) self.assertEquals(u.queryId(obj), uid) uid2 = u.register(obj) self.assertEquals(uid, uid2) u.unregister(obj) self.assertRaises(KeyError, u.getObject, uid) self.assertRaises(KeyError, u.getId, obj)
class SearchContainer(ReadonlyContainer): __name__ = 'search' def __init__(self): self.clear() def clear(self): self.tag_container = SearchByTagContainer(self) self.catalog = Catalog() self.catalog['tags'] = KeywordIndex('tags', ITagged) self.catalog['name'] = TextIndex('display_name', IDisplayName, True) self.catalog['__all'] = TextIndex('tokens', ITokenized, True) self.ids = IntIds() def index_object(self, obj): real_obj = follow_symlinks(obj) try: self.catalog.index_doc(self.ids.register(real_obj), real_obj) except NotYet: log.msg("cannot index object %s because it's not yet committed" % obj, system='search') def _index_object(self, obj): real_obj = follow_symlinks(obj) self.catalog.index_doc(self.ids.register(real_obj), real_obj) def unindex_object(self, obj): try: self.catalog.unindex_doc(self.ids.register(obj)) except NotYet: log.msg("cannot unindex object %s because it's not yet committed" % obj, system='search') def search(self, **kwargs): # HACK, we should be able to setup a persistent utility provideUtility(self.ids, IIntIds) return list(self.catalog.searchResults(**kwargs)) def search_goog(self, query): # hack, zope catalog treats ':' specially return self.search(__all=query.replace(':', '_')) @property def _items(self): return {'by-tag': self.tag_container}
def setupCatalog(test): intids = IntIds() component.provideUtility(intids, IIntIds) component.provideAdapter(SimpleKeyReference) cat = Catalog() cat['org_name'] = FieldIndex('name', IOrganization) cat['proj_name'] = FieldIndex('name', IProject) cat['proj_descr'] = FieldIndex('description', IProject) cat['student_name'] = FieldIndex('name', IStudent) cat['student_country'] = FieldIndex('country', IStudent) cat['mentor_name'] = FieldIndex('name', IMentor) cat['all_students'] = AllIndex(IStudent) cat['all_mentors'] = AllIndex(IMentor) cat['all_projects'] = AllIndex(IProject) cat['all_orgs'] = AllIndex(IOrganization) m1 = Mentor(u"John Doe") id = intids.register(m1) cat.index_doc(id, m1) p1 = Project(u"Save the world", u'test') id = intids.register(p1) cat.index_doc(id, p1) s1 = Student(u"Charith", u"Sri Lanka") id = intids.register(s1) cat.index_doc(id, s1) s2 = Student(u"Jane", u"USA") id = intids.register(s2) cat.index_doc(id, s2) s3 = Student(u"Ann", u"Hungary") id = intids.register(s3) cat.index_doc(id, s3) s4 = Student(u"Stewart", u"Hungary", m1) id = intids.register(s4) cat.index_doc(id, s4) o1 = Organization(u"Zope.org") id = intids.register(o1) cat.index_doc(id, o1) # cat2 = zc.relation.catalog.Catalog(dumpRelation, loadRelation) # cat2.addValueIndex(IProjectRelation['project'], dumpObj, loadObj, btree=BTrees.family32.OO) # cat2.addValueIndex(IProjectRelation['mentor'], dumpObj, loadObj, btree=BTrees.family32.OO) # cat2.addDefaultQueryFactory(zc.relation.queryfactory.TransposingTransitive('project','mentor')) # # rel = ProjectRelation(m1, p1) # cat2.index(rel) # component.provideUtility(cat2, zc.relation.interfaces.ICatalog, name='rel-catalog') component.provideUtility(cat, ICatalog, name='foo-catalog')
def setupCatalog(test, optCount=10, unoptCount=10, halfCount=10): intids = IntIds() component.provideUtility(intids, IIntIds) component.provideAdapter(SimpleKeyReference) cat = Catalog() cat['opt_name'] = FieldIndex('name', IOptimizedClass) cat['opt_value'] = FieldIndex('value', IOptimizedClass) cat['half_name'] = FieldIndex('name', IHalfOptimizedClass) cat['half_valueOpt'] = FieldIndex('value', IHalfOptimizedClass) cat['all_opt'] = AllIndex(IOptimizedClass) cat['all_unopt'] = AllIndex(IUnOptimizedClass) cat['all_half'] = AllIndex(IHalfOptimizedClass) for i in range(optCount): o = OptimizedClass() o.value = i o.name = unicode(i) id = intids.register(o) cat.index_doc(id, o) for i in range(unoptCount): o = UnOptimizedClass() o.value = i o.name = unicode(i) id = intids.register(o) cat.index_doc(id, o) for i in range(halfCount): o = HalfOptimizedClass() o.valueOpt = i o.valueNoOpt = i o.name = unicode(i) id = intids.register(o) cat.index_doc(id, o) component.provideUtility(cat, ICatalog, name='foo-catalog')
def test_len_items(self): u = IntIds() obj = P() obj._p_jar = ConnectionStub() self.assertEquals(len(u), 0) self.assertEquals(u.items(), []) self.assertEquals(list(u), []) uid = u.register(obj) ref = KeyReferenceToPersistent(obj) self.assertEquals(len(u), 1) self.assertEquals(u.items(), [(uid, ref)]) self.assertEquals(list(u), [uid]) obj2 = P() obj2.__parent__ = obj uid2 = u.register(obj2) ref2 = KeyReferenceToPersistent(obj2) self.assertEquals(len(u), 2) result = u.items() expected = [(uid, ref), (uid2, ref2)] result.sort() expected.sort() self.assertEquals(result, expected) result = list(u) expected = [uid, uid2] result.sort() expected.sort() self.assertEquals(result, expected) u.unregister(obj) u.unregister(obj2) self.assertEquals(len(u), 0) self.assertEquals(u.items(), [])
def setUp(test): root = setup.placefulSetUp(site=True) testing.setUp() provideAdapter(connectionOfPersistent, (IPersistent,), IConnection) provideAdapter(KeyReferenceToPersistent, (IPersistent,), IKeyReference) utility = IntIds() provideUtility(utility, IIntIds) root._p_jar = ConnectionStub() root['folder1'] = Folder() root['folder1']['folder1_1'] = Folder() root['folder1']['folder1_1']['folder1_1_1'] = Folder() root['folder2'] = Folder() root['folder2']['folder2_2'] = Folder() root['folder2']['folder2_2']['folder2_2_2'] = Folder() utility.register(root) utility.register(root['folder1']) utility.register(root['folder1']['folder1_1']) utility.register(root['folder1']['folder1_1']['folder1_1_1']) utility.register(root['folder2']) utility.register(root['folder2']['folder2_2']) utility.register(root['folder2']['folder2_2']['folder2_2_2']) test.globs['root'] = root