Beispiel #1
0
    def test_non_keyreferences(self):
        u = IntIds()
        obj = object()

        self.assert_(u.queryId(obj) is None)
        self.assert_(u.unregister(obj) is None)
        self.assertRaises(KeyError, u.getId, obj)
    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()
Beispiel #3
0
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')
Beispiel #4
0
 def test_getenrateId(self):
     u = IntIds()
     self.assertEquals(u._v_nextid, None)
     id1 = u._generateId()
     self.assert_(u._v_nextid is not None)
     id2 = u._generateId()
     self.assert_(id1 + 1, id2)
     u.refs[id2 + 1] = "Taken"
     id3 = u._generateId()
     self.assertNotEqual(id3, id2 + 1)
     self.assertNotEqual(id3, id2)
     self.assertNotEqual(id3, id1)
Beispiel #5
0
    def setUp(test):
        functional.FunctionalTestSetup().setUp()

        newInteraction()

        def fake_utcnow(self):
            return datetime.datetime(2015, 7, 30, 8, 0, 0)

        curse(datetime.datetime, 'utcnow', classmethod(fake_utcnow))

        root = functional.getRootFolder()
        setSite(root)
        sm = root.getSiteManager()

        # IIntIds
        root['intids'] = IntIds()
        sm.registerUtility(root['intids'], IIntIds)
        root['intids'].register(root)

        # catalog
        root['catalog'] = Catalog()
        sm.registerUtility(root['catalog'], ICatalog)

        # PluggableAuthentication
        pau = PluggableAuthentication(u'')
        event.notify(ObjectCreatedEvent(pau))
        sm[u'auth'] = pau
        sm.registerUtility(pau, IAuthentication)

        # Credentials Plugin
        defaultCreds.install()
        defaultCreds.activate()

        # people
        people = PersonalSpaceManager(title=u'People')
        event.notify(ObjectCreatedEvent(people))
        root['people'] = people
        sm.registerUtility(root['people'], IPersonalSpaceManager)

        user = sm.getUtility(IAuthentication).getPrincipal('zope.mgr')
        people.assignPersonalSpace(user)

        user = sm.getUtility(IAuthentication).getPrincipal('zope.user')
        people.assignPersonalSpace(user)

        # default content
        content = Content(u'Content1', u'Some Content1')
        event.notify(ObjectCreatedEvent(content))
        IOwnership(content).ownerId = 'zope.user'
        root['content1'] = content

        content = Content(u'Content2', u'Some Content2')
        event.notify(ObjectCreatedEvent(content))
        IOwnership(content).ownerId = 'zope.user'
        root['content2'] = content

        endInteraction()
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}
Beispiel #7
0
 def _createIntIds(self, site):
     """Create intids if needed, and return it.
     """
     intids = zope.component.queryUtility(
         IIntIds, context=site, default=None)
     if intids is not None:
         return intids
     intids = IntIds()
     setupUtility(site, intids, IIntIds)
     return intids
Beispiel #8
0
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')
Beispiel #9
0
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')
Beispiel #10
0
    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)
Beispiel #11
0
    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(), [])
Beispiel #12
0
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