Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
and enable FTI lookup caching in tests by providing a global TestRequest
  • Loading branch information
petschki committed Sep 21, 2021
1 parent 9cabb70 commit 4caa6a9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
1 change: 0 additions & 1 deletion plone/dexterity/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ def __get__(self, inst, cls=None):
if fti is None:
fti = queryUtility(IDexterityFTI, name=portal_type)
if fti is None:
print(f"No FTI found for {portal_type}")
return spec
setattr(inst, "_v__cached_fti", fti)

Expand Down
4 changes: 3 additions & 1 deletion plone/dexterity/tests/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import unittest
import zope.component
import zope.component.testing

import zope.globalrequest

try:
from unittest.mock import Mock
Expand All @@ -24,6 +24,8 @@ class MockTestCase(unittest.TestCase):

def tearDown(self):
zope.component.testing.tearDown(self)
zope.globalrequest.setRequest(None)

if self._replaced_globals is not None:
for mock, orig in self._replaced_globals.items():
_global_replace(mock, orig)
Expand Down
11 changes: 11 additions & 0 deletions plone/dexterity/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from zope.component import provideAdapter
from zope.interface import alsoProvides
from zope.interface import Interface
from zope.globalrequest import setRequest
from zope.publisher.browser import TestRequest

import six
import zope.schema
Expand All @@ -42,6 +44,7 @@

class TestContent(MockTestCase):
def setUp(self):
setRequest(TestRequest())
SCHEMA_CACHE.clear()
provideAdapter(DefaultOrdering)
provideAdapter(AttributeAnnotations)
Expand Down Expand Up @@ -74,6 +77,7 @@ class IMarker(Interface):
fti_mock = Mock(wraps=DexterityFTI("testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

self.assertFalse(ISchema.implementedBy(Item))

Expand Down Expand Up @@ -129,6 +133,7 @@ class IMarker(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

self.assertFalse(ISchema.implementedBy(MyItem))

Expand Down Expand Up @@ -175,6 +180,7 @@ class IMarker(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

self.assertFalse(ISchema.implementedBy(MyItem))

Expand Down Expand Up @@ -358,6 +364,7 @@ class IMarker3(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

# start clean
SCHEMA_CACHE.invalidate("testtype")
Expand Down Expand Up @@ -411,6 +418,7 @@ class ISchema(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

SCHEMA_CACHE.invalidate("testtype")

Expand All @@ -433,6 +441,7 @@ class ISchema(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

SCHEMA_CACHE.invalidate("testtype")

Expand Down Expand Up @@ -462,6 +471,7 @@ class ISchema(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

SCHEMA_CACHE.invalidate("testtype")

Expand All @@ -487,6 +497,7 @@ class ISchema(Interface):
fti_mock = Mock(wraps=DexterityFTI(u"testtype"))
fti_mock.lookupSchema = Mock(return_value=ISchema)
self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")
alsoProvides(fti_mock, IDexterityFTI)

SCHEMA_CACHE.invalidate("testtype")

Expand Down
4 changes: 3 additions & 1 deletion plone/dexterity/tests/test_schema_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from plone.dexterity.interfaces import IDexterityFTI
from plone.dexterity.schema import SCHEMA_CACHE
from zope.interface import Interface

from zope.globalrequest import setRequest
from zope.publisher.browser import TestRequest

try:
from unittest.mock import Mock
Expand All @@ -19,6 +20,7 @@

class TestSchemaCache(MockTestCase):
def setUp(self):
setRequest(TestRequest())
SCHEMA_CACHE.clear()

def test_repeated_get_lookup(self):
Expand Down
3 changes: 3 additions & 0 deletions plone/dexterity/tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from zope.interface import provider
from zope.security.interfaces import IPermission
from zope.security.permission import Permission
from zope.globalrequest import setRequest
from zope.publisher.browser import TestRequest

import zope.schema

Expand All @@ -22,6 +24,7 @@

class TestAttributeProtection(MockTestCase):
def setUp(self):
setRequest(TestRequest())
SCHEMA_CACHE.clear()

def test_item(self):
Expand Down

0 comments on commit 4caa6a9

Please sign in to comment.