Skip to content

Commit

Permalink
Merge pull request #140 from plone/138-fixed-part1
Browse files Browse the repository at this point in the history
Use mock from unittest on Python 3
  • Loading branch information
ale-rt authored Oct 5, 2020
2 parents b9385b7 + 32f9233 commit ec4d8ff
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 35 deletions.
1 change: 1 addition & 0 deletions news/138.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use mock from unittest on Python 3 [ale-rt]
7 changes: 5 additions & 2 deletions plone/dexterity/tests/case.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from mock import Mock

import gc
import pkg_resources
import six
Expand All @@ -15,6 +13,11 @@
except pkg_resources.DistributionNotFound:
HAS_ZSERVER = False

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock


class MockTestCase(unittest.TestCase):
"""Base class for tests using mocks.
Expand Down
34 changes: 18 additions & 16 deletions plone/dexterity/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from datetime import date
from datetime import datetime
from DateTime import DateTime
from mock import Mock
from plone.autoform.interfaces import IFormFieldProvider
from plone.behavior.interfaces import IBehavior
from plone.behavior.interfaces import IBehaviorAssignable
Expand All @@ -30,6 +29,17 @@
import zope.schema


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

try:
from unittest.mock import patch
except ImportError:
from mock import patch


class TestContent(MockTestCase):

def setUp(self):
Expand Down Expand Up @@ -600,12 +610,10 @@ def test_name_unicode_id_str(self):

def test_item_dublincore(self):
from DateTime import DateTime
# Mock Zope DateTime
import mock

import plone.dexterity
datetime_patcher = mock.patch.object(
plone.dexterity.content, 'DateTime'
)

datetime_patcher = patch.object(plone.dexterity.content, 'DateTime')
mocked_datetime = datetime_patcher.start()
mocked_datetime.return_value = DateTime(2014, 6, 1)
self.addCleanup(datetime_patcher.stop)
Expand Down Expand Up @@ -661,12 +669,9 @@ def test_item_dublincore(self):

def test_item_dublincore_date(self):
from DateTime import DateTime
# Mock Zope DateTime
import mock

import plone.dexterity
datetime_patcher = mock.patch.object(
plone.dexterity.content, 'DateTime'
)
datetime_patcher = patch.object(plone.dexterity.content, 'DateTime')
mocked_datetime = datetime_patcher.start()
mocked_datetime.return_value = DateTime(2014, 6, 1)
self.addCleanup(datetime_patcher.stop)
Expand Down Expand Up @@ -707,12 +712,9 @@ def test_item_dublincore_date(self):

def test_item_dublincore_datetime(self):
from DateTime import DateTime
# Mock Zope DateTime
import mock

import plone.dexterity
datetime_patcher = mock.patch.object(
plone.dexterity.content, 'DateTime'
)
datetime_patcher = patch.object(plone.dexterity.content, 'DateTime')
mocked_datetime = datetime_patcher.start()
mocked_datetime.return_value = DateTime(2014, 6, 1)
self.addCleanup(datetime_patcher.stop)
Expand Down
6 changes: 4 additions & 2 deletions plone/dexterity/tests/test_factory.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from plone.dexterity.factory import DexterityFactory
from plone.dexterity.fti import DexterityFTI
from plone.dexterity.interfaces import IDexterityFTI
from zope.interface import Interface

import unittest

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

class IDummy(Interface):
pass
Expand Down
7 changes: 6 additions & 1 deletion plone/dexterity/tests/test_fti.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from plone.dexterity import utils
from plone.dexterity.schema import portalTypeToSchemaName
from plone.dexterity.factory import DexterityFactory
Expand Down Expand Up @@ -34,6 +33,12 @@
import zope.schema


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock


class TestClass(object):
meta_type = "Test Class"

Expand Down
6 changes: 5 additions & 1 deletion plone/dexterity/tests/test_primary.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from plone.dexterity.content import Item
from plone.dexterity.fti import DexterityFTI
from plone.dexterity.interfaces import IDexterityFTI
Expand All @@ -11,6 +10,11 @@
from zope.interface import Interface


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

class PrimaryFieldInfoTestCase(MockTestCase):
def test_primary_field_info(self):

Expand Down
6 changes: 5 additions & 1 deletion plone/dexterity/tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from plone.dexterity import schema
from plone.dexterity.fti import DexterityFTI
from plone.dexterity.interfaces import IContentType
Expand All @@ -16,6 +15,11 @@
import zope.schema


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

class TestSchemaModuleFactory(MockTestCase):

def test_transient_schema(self):
Expand Down
21 changes: 13 additions & 8 deletions plone/dexterity/tests/test_schema_cache.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from mock import patch
from plone.dexterity.fti import DexterityFTI
from plone.dexterity.interfaces import IDexterityFTI
from plone.dexterity.schema import SCHEMA_CACHE
from zope.interface import Interface

import unittest
import warnings

try:
from unittest.mock import Mock
except ImportError:
from mock import Mock

try:
from unittest.mock import patch
except ImportError:
from mock import patch


class TestSchemaCache(MockTestCase):
Expand Down Expand Up @@ -66,16 +72,15 @@ def test_unexistent_behaviors_lookup(self):
fti.behaviors = ["foo.bar"]

with patch("warnings.warn") as mock_warnings:
warning_list = [call[1][0] for call in mock_warnings.mock_calls]
SCHEMA_CACHE.behavior_registrations(u'testtype')
# Verify the warning has been issued
self.assertEqual(
mock_warnings.mock_calls[-1].args[0],
mock_warnings.assert_called_once_with(
(
'No behavior registration found for behavior named '
'"foo.bar" for factory "testtype" - trying deprecated '
'fallback lookup (will be removed in 3.0)..."'
)
),
DeprecationWarning,
)

def test_repeated_subtypes_lookup(self):
Expand Down
5 changes: 4 additions & 1 deletion plone/dexterity/tests/test_security.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from plone.autoform.interfaces import READ_PERMISSIONS_KEY
from plone.dexterity.content import Container
from plone.dexterity.content import Item
Expand All @@ -15,6 +14,10 @@
import zope.schema


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock
class TestAttributeProtection(MockTestCase):

def setUp(self):
Expand Down
7 changes: 6 additions & 1 deletion plone/dexterity/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from mock import Mock
from plone.dexterity import utils
from plone.dexterity.fti import DexterityFTI


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock


class TestUtils(MockTestCase):

def test_getAdditionalSchemata(self):
Expand Down
7 changes: 6 additions & 1 deletion plone/dexterity/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from .case import MockTestCase
from AccessControl import Unauthorized
from mock import Mock
from plone.autoform.interfaces import IFormFieldProvider
from plone.behavior.interfaces import IBehaviorAssignable
from plone.dexterity.browser.add import DefaultAddForm
Expand Down Expand Up @@ -41,6 +40,12 @@
import six


try:
from unittest.mock import Mock
except ImportError:
from mock import Mock


class TestRequest(TestRequestBase):
"""Zope 3's TestRequest doesn't support item assignment, but Zope 2's
request does.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def read(filename):
'test': [
'plone.testing',
'Products.CMFPlone',
'mock',
"mock;python_version<'3'"
]
},
entry_points="""
Expand Down

0 comments on commit ec4d8ff

Please sign in to comment.