Example #1
0
 def setUp(self):
     self.portal = self.layer['portal']
     self.browser = Browser(self.layer['app'])
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     # Invalidate schema cache
     SCHEMA_CACHE.invalidate('Audio')
     SCHEMA_CACHE.invalidate('OGG Audio File')
Example #2
0
    def test_getattr_on_container_returns_children(self):

        content = Container()
        content.id = u"id"
        content.portal_type = u"testtype"

        content['foo'] = Item('foo')
        content['quux'] = Item('quux')

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        SCHEMA_CACHE.invalidate('testtype')

        # Schema field masks contained item
        self.assertEqual(u"foo_default", content.foo)

        # But we can still obtain an item
        self.assertTrue(isinstance(content['foo'], Item))
        self.assertEqual('foo', content['foo'].id)

        # And if the item isn't masked by an attribute, we can still getattr it
        self.assertTrue(isinstance(content['quux'], Item))
        self.assertEqual('quux', content['quux'].id)

        self.assertTrue(isinstance(getattr(content, 'quux'), Item))
        self.assertEqual('quux', getattr(content, 'quux').id)
Example #3
0
    def test_getattr_consults_schema_item_default_factory_with_context(self):

        content = Item()
        content.id = u"id"
        content.portal_type = u"testtype"

        from zope.interface import provider
        from zope.schema.interfaces import IContextAwareDefaultFactory

        @provider(IContextAwareDefaultFactory)
        def defaultFactory(context):
            return u"{0:s}_{1:s}".format(context.id, context.portal_type)

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo",
                                       defaultFactory=defaultFactory)
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        SCHEMA_CACHE.invalidate('testtype')

        self.assertEqual(u"id_testtype", content.foo)
        self.assertEqual(None, content.bar)
        self.assertEqual(u"id", content.id)
        self.assertRaises(AttributeError, getattr, content, 'baz')
Example #4
0
    def test_getattr_consults_schema_item_default_factory_with_context(self):

        content = Item()
        content.id = u"id"
        content.portal_type = u"testtype"

        from zope.interface import provider
        from zope.schema.interfaces import IContextAwareDefaultFactory

        @provider(IContextAwareDefaultFactory)
        def defaultFactory(context):
            return u"{0:s}_{1:s}".format(context.id, context.portal_type)

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo",
                                       defaultFactory=defaultFactory)
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        SCHEMA_CACHE.invalidate('testtype')

        self.assertEqual(u"id_testtype", content.foo)
        self.assertEqual(None, content.bar)
        self.assertEqual(u"id", content.id)
        self.assertRaises(AttributeError, getattr, content, 'baz')
Example #5
0
    def test_getattr_on_container_returns_children(self):

        content = Container()
        content.id = u"id"
        content.portal_type = u"testtype"

        content['foo'] = Item('foo')
        content['quux'] = Item('quux')

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        SCHEMA_CACHE.invalidate('testtype')

        # Schema field masks contained item
        self.assertEqual(u"foo_default", content.foo)

        # But we can still obtain an item
        self.assertTrue(isinstance(content['foo'], Item))
        self.assertEqual('foo', content['foo'].id)

        # And if the item isn't masked by an attribute, we can still getattr it
        self.assertTrue(isinstance(content['quux'], Item))
        self.assertEqual('quux', content['quux'].id)

        self.assertTrue(isinstance(getattr(content, 'quux'), Item))
        self.assertEqual('quux', getattr(content, 'quux').id)
    def test_attribute_and_value_error_not_cached(self):

        class ISchema1(Interface):
            pass

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).throw(AttributeError)
        self.expect(fti_mock.lookupSchema()).throw(ValueError)
        self.expect(fti_mock.lookupSchema()).result(ISchema1)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        schema1 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema2 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema3 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is None)
        self.assertTrue(schema2 is None)
        self.assertTrue(schema3 is ISchema1)
Example #7
0
def inactivate_filing_number(portal):
    unregister_layer('opengever.dossier.filing')

    portal_types = getToolByName(portal, 'portal_types')
    fti = portal_types.get('opengever.dossier.businesscasedossier')
    fti.behaviors = [behavior for behavior in fti.behaviors
                     if not behavior.endswith('IFilingNumber')]

    SCHEMA_CACHE.invalidate('opengever.dossier.businesscasedossier')
 def setUp(self):
     self.portal = self.layer['portal']
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     self.portal.invokeFactory('Folder', 'test-folder')
     # Invalidate schema cache
     SCHEMA_CACHE.invalidate('Person')
     setRoles(self.portal, TEST_USER_ID, ['Member'])
     self.folder = self.portal['test-folder']
     self.setup_content(self.folder)
Example #9
0
def inactivate_filing_number(portal):
    unregister_layer('opengever.dossier.filing')

    portal_types = getToolByName(portal, 'portal_types')
    fti = portal_types.get('opengever.dossier.businesscasedossier')
    fti.behaviors = [behavior for behavior in fti.behaviors
                     if not behavior.endswith('IFilingNumber')]

    SCHEMA_CACHE.invalidate('opengever.dossier.businesscasedossier')
 def setUp(self):
     self.portal = self.layer["portal"]
     setRoles(self.portal, TEST_USER_ID, ["Manager"])
     self.portal.invokeFactory("Folder", "test-folder")
     # Invalidate schema cache
     SCHEMA_CACHE.invalidate("Audio")
     SCHEMA_CACHE.invalidate("OGG Audio File")
     self.folder = self.portal["test-folder"]
     audio_id = self.folder.invokeFactory("Audio", "my-audio")
     self.audio = self.folder[audio_id]
     self.setup_content_data()
     self.audio.invokeFactory("OGG Audio File", "file.ogg")
     self.ogg_audio = self.audio["file.ogg"]
 def setUp(self):
     self.portal = self.layer['portal']
     with api.env.adopt_roles([
             'Manager',
     ]):
         self.folder = api.content.create(type='Folder',
                                          container=self.portal,
                                          id='test-folder')
         # Invalidate schema cache
         SCHEMA_CACHE.invalidate('ExternalContent')
         self.content = api.content.create(type='ExternalContent',
                                           container=self.folder,
                                           id='external')
         self.setup_content_data()
 def setUp(self):
     self.portal = self.layer['portal']
     # Invalidate schema cache
     SCHEMA_CACHE.invalidate('ExternalContent')
     with api.env.adopt_roles(['Manager', ]):
         self.folder = api.content.create(
             type='Folder',
             container=self.portal,
             id='test-folder'
         )
         self.content = api.content.create(
             type='ExternalContent',
             container=self.folder,
             id='external'
         )
Example #13
0
    def test_repeated_lookup_with_changed_schema_and_invalidation(self):
        class ISchema1(Interface):
            pass

        class ISchema2(Interface):
            pass

        fti = DexterityFTI(u"testtype")
        fti.lookupSchema = Mock(side_effect=[ISchema1, ISchema2])
        self.mock_utility(fti, IDexterityFTI, name=u"testtype")

        schema1 = SCHEMA_CACHE.get(u"testtype")
        SCHEMA_CACHE.invalidate(u"testtype")
        schema2 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is ISchema1)
        self.assertTrue(schema2 is ISchema2)
Example #14
0
    def setUp(self):
        self.portal = self.layer["portal"]
        self.request = self.layer["request"]

        self.portal.invokeFactory(
            "Document",
            id="doc1",
        )

        fti = queryUtility(IDexterityFTI, name="Plone Site")
        if fti is not None:
            behavior_list = [a for a in fti.behaviors]
            behavior_list.append("volto.blocks")
            fti.behaviors = tuple(behavior_list)
            # Invalidating the cache is required for the FTI to be applied
            # on the existing object
            SCHEMA_CACHE.invalidate("Plone Site")
 def setUp(self):
     self.portal = self.layer['portal']
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     # Invalidate schema cache
     SCHEMA_CACHE.invalidate('Audio')
     SCHEMA_CACHE.invalidate('OGG Audio File')
     self.folder = api.content.create(type='Folder',
                                      container=self.portal,
                                      id='test-folder')
     self.audio = api.content.create(type='Audio',
                                     container=self.folder,
                                     id='my-audio')
     self.setup_content_data()
     self.ogg_audio = api.content.create(type='OGG Audio File',
                                         container=self.audio,
                                         id='file.ogg')
     self.ogg_audio.file = self.ogg
     self.ogg_audio.reindexObject()
    def test_repeated_lookup_with_changed_schema_and_invalidation(self):

        class ISchema1(Interface):
            pass

        class ISchema2(Interface):
            pass

        fti = DexterityFTI(u"testtype")
        fti.lookupSchema = Mock(side_effect=[ISchema1, ISchema2])
        self.mock_utility(fti, IDexterityFTI, name=u"testtype")

        schema1 = SCHEMA_CACHE.get(u"testtype")
        SCHEMA_CACHE.invalidate(u"testtype")
        schema2 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is ISchema1)
        self.assertTrue(schema2 is ISchema2)
Example #17
0
    def test_attribute_and_value_error_not_cached(self):
        class ISchema1(Interface):
            pass

        fti = DexterityFTI(u"testtype")
        fti.lookupSchema = Mock(
            side_effect=[AttributeError, ValueError, ISchema1])
        self.mock_utility(fti, IDexterityFTI, name=u"testtype")

        schema1 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema2 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema3 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is None)
        self.assertTrue(schema2 is None)
        self.assertTrue(schema3 is ISchema1)
    def test_attribute_and_value_error_not_cached(self):

        class ISchema1(Interface):
            pass

        fti = DexterityFTI(u"testtype")
        fti.lookupSchema = Mock(
            side_effect=[AttributeError, ValueError, ISchema1])
        self.mock_utility(fti, IDexterityFTI, name=u"testtype")

        schema1 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema2 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema3 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is None)
        self.assertTrue(schema2 is None)
        self.assertTrue(schema3 is ISchema1)
Example #19
0
    def test_repeated_lookup_with_changed_schema_and_invalidation(self):
        class ISchema1(Interface):
            pass

        class ISchema2(Interface):
            pass

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(ISchema1)
        self.expect(fti_mock.lookupSchema()).result(ISchema2).count(0, None)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        schema1 = SCHEMA_CACHE.get(u"testtype")
        SCHEMA_CACHE.invalidate(u"testtype")
        schema2 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is ISchema1)
        self.assertTrue(schema2 is ISchema2)
Example #20
0
    def test_getattr_consults_schema_container(self):

        content = Container()
        content.id = u"id"
        content.portal_type = u"testtype"

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        SCHEMA_CACHE.invalidate('testtype')

        self.assertEqual(u"foo_default", content.foo)
        self.assertEqual(None, content.bar)
        self.assertEqual(u"id", content.id)
        self.assertRaises(AttributeError, getattr, content, 'baz')
Example #21
0
    def test_repeated_lookup_with_changed_schema_and_invalidation(self):
        class ISchema1(Interface):
            pass

        class ISchema2(Interface):
            pass

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(ISchema1)
        self.expect(fti_mock.lookupSchema()).result(ISchema2).count(0, None)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        schema1 = SCHEMA_CACHE.get(u"testtype")
        SCHEMA_CACHE.invalidate(u"testtype")
        schema2 = SCHEMA_CACHE.get(u"testtype")

        self.failUnless(schema1 is ISchema1)
        self.failUnless(schema2 is ISchema2)
Example #22
0
    def test_getattr_consults_schema_container(self):

        content = Container()
        content.id = u"id"
        content.portal_type = u"testtype"

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        SCHEMA_CACHE.invalidate('testtype')

        self.assertEqual(u"foo_default", content.foo)
        self.assertEqual(None, content.bar)
        self.assertEqual(u"id", content.id)
        self.assertRaises(AttributeError, getattr, content, 'baz')
Example #23
0
    def test_getattr_consults_schema_item(self):

        content = Item()
        content.id = u"id"
        content.portal_type = u"testtype"

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        SCHEMA_CACHE.invalidate('testtype')

        self.assertEqual(u"foo_default", content.foo)
        self.assertEqual(None, content.bar)
        self.assertEqual(u"id", content.id)
        self.assertRaises(AttributeError, getattr, content, 'baz')
Example #24
0
    def test_getattr_consults_schema_item(self):

        content = Item()
        content.id = u"id"
        content.portal_type = u"testtype"

        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        SCHEMA_CACHE.invalidate('testtype')

        self.assertEqual(u"foo_default", content.foo)
        self.assertEqual(None, content.bar)
        self.assertEqual(u"id", content.id)
        self.assertRaises(AttributeError, getattr, content, 'baz')
Example #25
0
    def test_attribute_and_value_error_not_cached(self):
        class ISchema1(Interface):
            pass

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).throw(AttributeError)
        self.expect(fti_mock.lookupSchema()).throw(ValueError)
        self.expect(fti_mock.lookupSchema()).result(ISchema1)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        schema1 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema2 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema3 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is None)
        self.assertTrue(schema2 is None)
        self.assertTrue(schema3 is ISchema1)
 def setUp(self):
     self.portal = self.layer['portal']
     setRoles(self.portal, TEST_USER_ID, ['Manager'])
     # Invalidate schema cache
     SCHEMA_CACHE.invalidate('Audio')
     SCHEMA_CACHE.invalidate('OGG Audio File')
     self.folder = api.content.create(
         type='Folder',
         container=self.portal,
         id='test-folder'
     )
     self.audio = api.content.create(
         type='Audio',
         container=self.folder,
         id='my-audio'
     )
     self.setup_content_data()
     self.ogg_audio = api.content.create(
         type='OGG Audio File',
         container=self.audio,
         id='file.ogg'
     )
     self.ogg_audio.file = self.ogg
     self.ogg_audio.reindexObject()
Example #27
0
    def test_none_not_cached(self):
        class ISchema1(Interface):
            pass

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(None)
        self.expect(fti_mock.lookupSchema()).result(ISchema1).count(2)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        SCHEMA_CACHE.invalidate('testtype')
        schema1 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema2 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema3 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is None)
        self.assertTrue(schema2 is schema3 is ISchema1)
    def test_none_not_cached(self):

        class ISchema1(Interface):
            pass

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(None)
        self.expect(fti_mock.lookupSchema()).result(ISchema1).count(2)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        SCHEMA_CACHE.invalidate('testtype')
        schema1 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema2 = SCHEMA_CACHE.get(u"testtype")

        SCHEMA_CACHE.invalidate('testtype')
        schema3 = SCHEMA_CACHE.get(u"testtype")

        self.assertTrue(schema1 is None)
        self.assertTrue(schema2 is schema3 is ISchema1)
Example #29
0
    def test_provided_by_behavior_subtype_invalidation(self):

        # Dummy type
        class MyItem(Item):
            pass

        # Fake data manager
        class FauxDataManager(object):
            def setstate(self, obj):
                pass

            def oldstate(self, obj, tid):
                pass

            def register(self, obj):
                pass

        # Dummy instance
        item = MyItem(id=u'id')
        item.portal_type = 'testtype'

        # Without a persistence jar, the _p_changed check doesn't work. In
        # this case, the cache is a bit slower.
        item._p_jar = FauxDataManager()

        # Dummy schema
        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # Schema is not implemented by class or provided by instance
        self.assertEquals(False, ISchema.implementedBy(MyItem))
        self.assertEquals(False, ISchema.providedBy(item))

        # Behaviors - one with a subtype and one without

        class IBehavior1(Interface):
            pass

        class IBehavior2(Interface):
            pass

        class IBehavior3(Interface):
            pass

        class ISubtype1(Interface):
            pass

        class ISubtype2(Interface):
            pass

        behavior1 = BehaviorRegistration(u"Behavior1", "", IBehavior1, None,
                                         None)
        behavior2 = BehaviorRegistration(u"Behavior2", "", IBehavior2,
                                         ISubtype1, None)
        behavior3 = BehaviorRegistration(u"Behavior3", "", IBehavior3,
                                         ISubtype2, None)

        self.mock_utility(behavior1, IBehavior, name="behavior1")
        self.mock_utility(behavior2, IBehavior, name="behavior2")
        self.mock_utility(behavior3, IBehavior, name="behavior3")

        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(ISchema).count(
            2)  # twice, since we invalidate

        # First time around, we have only these behaviors
        self.expect(fti_mock.behaviors).result(['behavior1',
                                                'behavior2']).count(1)

        # Second time around, we add another one
        self.expect(fti_mock.behaviors).result(
            ['behavior1', 'behavior2', 'behavior3']).count(1)

        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        self.replay()

        self.assertEquals(False, ISchema.implementedBy(MyItem))

        # Schema as looked up in FTI is now provided by item ...
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(False, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))

        # If the _v_ attribute cache does not work, then we'd expect to have
        # to look up the schema more than once (since we invalidated)
        # the cache. This is not the case, as evidenced by .count(1) above.
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(False, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))

        # If we now invalidate the schema cache, we should get the second set
        # of behaviors
        SCHEMA_CACHE.invalidate('testtype')

        # Schema as looked up in FTI is now provided by item ...

        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(True, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))

        # If the _v_ attribute cache does not work, then we'd expect to have
        # to look up the schema more than once (since we invalidated)
        # the cache. This is not the case, as evidenced by .count(1) above.
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(True, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))
Example #30
0
    def test_provided_by_behavior_subtype_invalidation(self):

        # Dummy type
        class MyItem(Item):
            pass

        # Fake data manager
        class FauxDataManager(object):
            def setstate(self, obj):
                pass

            def oldstate(self, obj, tid):
                pass

            def register(self, obj):
                pass

        # Dummy instance
        item = MyItem(id=u'id')
        item.portal_type = 'testtype'

        # Without a persistence jar, the _p_changed check doesn't work. In
        # this case, the cache is a bit slower.
        item._p_jar = FauxDataManager()

        # Dummy schema
        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # Schema is not implemented by class or provided by instance
        self.assertFalse(ISchema.implementedBy(MyItem))
        self.assertFalse(ISchema.providedBy(item))

        # Behaviors - one with a marker and one without
        class IBehavior1(Interface):
            pass

        behavior1 = BehaviorRegistration(
            u"Behavior1",
            "",
            IBehavior1,
            None,
            None
        )
        self.mock_utility(behavior1, IBehavior, name="behavior1")

        class IBehavior2(Interface):
            pass

        class IMarker2(Interface):
            pass

        behavior2 = BehaviorRegistration(
            u"Behavior2",
            "",
            IBehavior2,
            IMarker2,
            None
        )
        self.mock_utility(behavior2, IBehavior, name="behavior2")

        class IBehavior3(Interface):
            pass

        class IMarker3(Interface):
            pass

        behavior3 = BehaviorRegistration(
            u"Behavior3",
            "",
            IBehavior3,
            IMarker3,
            None
        )
        self.mock_utility(behavior3, IBehavior, name="behavior3")

        self.mock_adapter(
            DexterityBehaviorAssignable,
            IBehaviorAssignable,
            (IDexterityContent,)
        )

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # start clean
        SCHEMA_CACHE.invalidate('testtype')
        fti_mock.behaviors = ['behavior1', 'behavior2']

        # implementedBy does not look into the fti
        self.assertFalse(ISchema.implementedBy(MyItem))

        # Main schema as looked up in FTI is now provided by item ...
        self.assertTrue(ISchema.providedBy(item))

        # Behaviors with its behavior or if provided merker as looked up in
        # FTI is now provided by item ...
        self.assertFalse(IBehavior1.providedBy(item))
        self.assertTrue(IMarker2.providedBy(item))
        self.assertFalse(IMarker3.providedBy(item))

        # If we now invalidate the schema cache, we should get the
        # SECOND set of behaviors (which includes behavior3)
        SCHEMA_CACHE.invalidate('testtype')
        fti_mock.behaviors = ['behavior1', 'behavior2', 'behavior3']

        # Main schema as looked up in FTI is now provided by item ...
        self.assertTrue(ISchema.providedBy(item))

        # Behaviors with its behavior or if provided merker as looked up in
        # FTI is now provided by item ...
        self.assertFalse(IBehavior1.providedBy(item))
        self.assertTrue(IMarker2.providedBy(item))
        self.assertTrue(IMarker3.providedBy(item))

        # If the _v_ attribute cache does not work, then we'd expect to have
        # to look up the schema more than once (since we invalidated)
        # the cache. This is not the case, as evidenced by .count(1) above.
        self.assertTrue(ISchema.providedBy(item))
        self.assertFalse(IBehavior1.providedBy(item))
        self.assertTrue(IMarker2.providedBy(item))
        self.assertTrue(IMarker3.providedBy(item))
Example #31
0
    def test_provided_by_behavior_subtype_invalidation(self):
        
        # Dummy type
        class MyItem(Item):
            pass
        
        # Fake data manager
        class FauxDataManager(object):
            def setstate(self, obj): pass
            def oldstate(self, obj, tid): pass
            def register(self, obj): pass

        
        # Dummy instance
        item = MyItem(id=u'id')
        item.portal_type = 'testtype'
        
        # Without a persistence jar, the _p_changed check doesn't work. In
        # this case, the cache is a bit slower.
        item._p_jar = FauxDataManager()
        
        # Dummy schema
        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")
        
        # Schema is not implemented by class or provided by instance
        self.assertEquals(False, ISchema.implementedBy(MyItem))
        self.assertEquals(False, ISchema.providedBy(item))
        
        # Behaviors - one with a subtype and one without
        
        class IBehavior1(Interface):
            pass
        
        class IBehavior2(Interface):
            pass
        
        class IBehavior3(Interface):
            pass
        
        class ISubtype1(Interface):
            pass
        
        class ISubtype2(Interface):
            pass
        
        behavior1 = BehaviorRegistration(u"Behavior1", "", IBehavior1, None, None)
        behavior2 = BehaviorRegistration(u"Behavior2", "", IBehavior2, ISubtype1, None)
        behavior3 = BehaviorRegistration(u"Behavior3", "", IBehavior3, ISubtype2, None)
        
        self.mock_utility(behavior1, IBehavior, name="behavior1")
        self.mock_utility(behavior2, IBehavior, name="behavior2")
        self.mock_utility(behavior3, IBehavior, name="behavior3")
        
        # FTI mock
        fti_mock = self.mocker.proxy(DexterityFTI(u"testtype"))
        self.expect(fti_mock.lookupSchema()).result(ISchema).count(2) # twice, since we invalidate
        
        # First time around, we have only these behaviors
        self.expect(fti_mock.behaviors).result(['behavior1', 'behavior2']).count(1)
        
        # Second time around, we add another one
        self.expect(fti_mock.behaviors).result(['behavior1', 'behavior2', 'behavior3']).count(1)
        
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
        
        self.replay()
        
        self.assertEquals(False, ISchema.implementedBy(MyItem))
        
        # Schema as looked up in FTI is now provided by item ...
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(False, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))
        
        # If the _v_ attribute cache does not work, then we'd expect to have
        # to look up the schema more than once (since we invalidated)
        # the cache. This is not the case, as evidenced by .count(1) above.
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(False, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))
        
        # If we now invalidate the schema cache, we should get the second set
        # of behaviors
        SCHEMA_CACHE.invalidate('testtype')
        
        # Schema as looked up in FTI is now provided by item ...
        
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(True, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))
        
        # If the _v_ attribute cache does not work, then we'd expect to have
        # to look up the schema more than once (since we invalidated)
        # the cache. This is not the case, as evidenced by .count(1) above.
        self.assertEquals(True, ISubtype1.providedBy(item))
        self.assertEquals(True, ISubtype2.providedBy(item))
        self.assertEquals(True, ISchema.providedBy(item))
Example #32
0
    def test_provided_by_behavior_subtype_invalidation(self):

        # Dummy type
        class MyItem(Item):
            pass

        # Fake data manager
        class FauxDataManager(object):
            def setstate(self, obj):
                pass

            def oldstate(self, obj, tid):
                pass

            def register(self, obj):
                pass

        # Dummy instance
        item = MyItem(id=u'id')
        item.portal_type = 'testtype'

        # Without a persistence jar, the _p_changed check doesn't work. In
        # this case, the cache is a bit slower.
        item._p_jar = FauxDataManager()

        # Dummy schema
        class ISchema(Interface):
            foo = zope.schema.TextLine(title=u"foo", default=u"foo_default")
            bar = zope.schema.TextLine(title=u"bar")

        # Schema is not implemented by class or provided by instance
        self.assertFalse(ISchema.implementedBy(MyItem))
        self.assertFalse(ISchema.providedBy(item))

        # Behaviors - one with a marker and one without
        class IBehavior1(Interface):
            pass

        behavior1 = BehaviorRegistration(u"Behavior1", "", IBehavior1, None,
                                         None)
        self.mock_utility(behavior1, IBehavior, name="behavior1")

        class IBehavior2(Interface):
            pass

        class IMarker2(Interface):
            pass

        behavior2 = BehaviorRegistration(u"Behavior2", "", IBehavior2,
                                         IMarker2, None)
        self.mock_utility(behavior2, IBehavior, name="behavior2")

        class IBehavior3(Interface):
            pass

        class IMarker3(Interface):
            pass

        behavior3 = BehaviorRegistration(u"Behavior3", "", IBehavior3,
                                         IMarker3, None)
        self.mock_utility(behavior3, IBehavior, name="behavior3")

        self.mock_adapter(DexterityBehaviorAssignable, IBehaviorAssignable,
                          (IDexterityContent, ))

        # FTI mock
        fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
        fti_mock.lookupSchema = Mock(return_value=ISchema)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # start clean
        SCHEMA_CACHE.invalidate('testtype')
        fti_mock.behaviors = ['behavior1', 'behavior2']

        # implementedBy does not look into the fti
        self.assertFalse(ISchema.implementedBy(MyItem))

        # Main schema as looked up in FTI is now provided by item ...
        self.assertTrue(ISchema.providedBy(item))

        # Behaviors with its behavior or if provided merker as looked up in
        # FTI is now provided by item ...
        self.assertFalse(IBehavior1.providedBy(item))
        self.assertTrue(IMarker2.providedBy(item))
        self.assertFalse(IMarker3.providedBy(item))

        # If we now invalidate the schema cache, we should get the
        # SECOND set of behaviors (which includes behavior3)
        SCHEMA_CACHE.invalidate('testtype')
        fti_mock.behaviors = ['behavior1', 'behavior2', 'behavior3']

        # Main schema as looked up in FTI is now provided by item ...
        self.assertTrue(ISchema.providedBy(item))

        # Behaviors with its behavior or if provided merker as looked up in
        # FTI is now provided by item ...
        self.assertFalse(IBehavior1.providedBy(item))
        self.assertTrue(IMarker2.providedBy(item))
        self.assertTrue(IMarker3.providedBy(item))

        # If the _v_ attribute cache does not work, then we'd expect to have
        # to look up the schema more than once (since we invalidated)
        # the cache. This is not the case, as evidenced by .count(1) above.
        self.assertTrue(ISchema.providedBy(item))
        self.assertFalse(IBehavior1.providedBy(item))
        self.assertTrue(IMarker2.providedBy(item))
        self.assertTrue(IMarker3.providedBy(item))