Merge pull request #42084 from terminalmage/assert-called-once

Fix usage of assert_called_once in Python 3 < 3.6
This commit is contained in:
Nicole Thomas 2017-07-05 09:22:39 -06:00 committed by GitHub
commit 9197055ea5
4 changed files with 14 additions and 5 deletions

View File

@ -306,6 +306,16 @@ class TestCase(_TestCase):
)
# return _TestCase.failIfAlmostEqual(self, *args, **kwargs)
@staticmethod
def assert_called_once(mock):
'''
mock.assert_called_once only exists in PY3 in 3.6 and newer
'''
try:
mock.assert_called_once()
except AttributeError:
log.warning('assert_called_once invoked, but not available')
if six.PY2:
def assertRegexpMatches(self, *args, **kwds):
raise DeprecationWarning(

View File

@ -105,7 +105,7 @@ class GCETestCase(TestCase, LoaderModuleMockMixin):
get_deps = gce.get_dependencies()
self.assertEqual(get_deps, True)
if LooseVersion(mock_version) >= LooseVersion('2.0.0'):
p.assert_called_once()
self.assert_called_once(p)
def test_provider_matches(self):
"""

View File

@ -127,7 +127,7 @@ class DiskTestCase(TestCase, LoaderModuleMockMixin):
kwargs = {'read-ahead': 512, 'filesystem-read-ahead': 1024}
disk.tune('/dev/sda', **kwargs)
mock.assert_called_once()
self.assert_called_once(mock)
args, kwargs = mock.call_args

View File

@ -38,9 +38,8 @@ class MdadmTestCase(TestCase, LoaderModuleMockMixin):
)
self.assertEqual('salt', ret)
# Only available in 3.6 and above on py3
if hasattr(mock, 'assert_called_once'):
mock.assert_called_once()
self.assert_called_once(mock)
args, kwargs = mock.call_args
# expected cmd is
# mdadm -C /dev/md0 -R -v --chunk 256 --force -l 5 -e default -n 3 /dev/sdb1 /dev/sdc1 /dev/sdd1