summaryrefslogtreecommitdiff
path: root/nova/tests/unit/test_hacking.py
diff options
context:
space:
mode:
authorTakashi NATSUME <natsume.takashi@lab.ntt.co.jp>2019-08-21 11:45:22 +0900
committerTakashi NATSUME <natsume.takashi@lab.ntt.co.jp>2019-08-21 05:23:02 +0000
commitd4ed9ed93f91e2ffa43b563abdc5a3b90ab358a1 (patch)
treedc7841f0049e97f43c8bc1bf57a8ca72364a0abc /nova/tests/unit/test_hacking.py
parent5ccdbc7189640d02cc17e2156c635e8f7eafb02e (diff)
downloadnova-d4ed9ed93f91e2ffa43b563abdc5a3b90ab358a1.tar.gz
Add a hacking rule for non-existent assertions
Add a hacking rule for non-existent mock assertion methods and attributes. [N364] Non existent mock assertion method or attribute (<name>) is used. Check a typo or whether the assertion method should begin with 'assert_'. Change-Id: Ic6860e373120086a1a2ae9953f09a7bbaa032a7b
Diffstat (limited to 'nova/tests/unit/test_hacking.py')
-rw-r--r--nova/tests/unit/test_hacking.py78
1 files changed, 78 insertions, 0 deletions
diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py
index e1455d7105..ab570f4d31 100644
--- a/nova/tests/unit/test_hacking.py
+++ b/nova/tests/unit/test_hacking.py
@@ -913,3 +913,81 @@ class HackingTestCase(test.NoDBTestCase):
or foo in (method_call_should_this_work()):
"""
self._assert_has_no_errors(code, checks.did_you_mean_tuple)
+
+ def test_nonexistent_assertion_methods_and_attributes(self):
+ code = """
+ mock_sample.called_once()
+ mock_sample.called_once_with(a, "TEST")
+ mock_sample.has_calls([mock.call(x), mock.call(y)])
+ mock_sample.mock_assert_called()
+ mock_sample.mock_assert_called_once()
+ mock_sample.mock_assert_called_with(a, "xxxx")
+ mock_sample.mock_assert_called_once_with(a, b)
+ mock_sample.mock_assert_any_call(1, 2, instance=instance)
+ sample.mock_assert_has_calls([mock.call(x), mock.call(y)])
+ mock_sample.mock_assert_not_called()
+ mock_sample.asser_called()
+ mock_sample.asser_called_once()
+ mock_sample.asser_called_with(a, "xxxx")
+ mock_sample.asser_called_once_with(a, b)
+ mock_sample.asser_any_call(1, 2, instance=instance)
+ mock_sample.asser_has_calls([mock.call(x), mock.call(y)])
+ mock_sample.asser_not_called()
+ mock_sample.asset_called()
+ mock_sample.asset_called_once()
+ mock_sample.asset_called_with(a, "xxxx")
+ mock_sample.asset_called_once_with(a, b)
+ mock_sample.asset_any_call(1, 2, instance=instance)
+ mock_sample.asset_has_calls([mock.call(x), mock.call(y)])
+ mock_sample.asset_not_called()
+ mock_sample.asssert_called()
+ mock_sample.asssert_called_once()
+ mock_sample.asssert_called_with(a, "xxxx")
+ mock_sample.asssert_called_once_with(a, b)
+ mock_sample.asssert_any_call(1, 2, instance=instance)
+ mock_sample.asssert_has_calls([mock.call(x), mock.call(y)])
+ mock_sample.asssert_not_called()
+ mock_sample.assset_called()
+ mock_sample.assset_called_once()
+ mock_sample.assset_called_with(a, "xxxx")
+ mock_sample.assset_called_once_with(a, b)
+ mock_sample.assset_any_call(1, 2, instance=instance)
+ mock_sample.assset_has_calls([mock.call(x), mock.call(y)])
+ mock_sample.assset_not_called()
+ mock_sample.retrun_value = 100
+ sample.call_method(mock_sample.retrun_value, 100)
+ mock.Mock(retrun_value=100)
+ """
+ errors = [(x + 1, 0, 'N364') for x in range(41)]
+ # Check errors in 'nova/tests' directory.
+ self._assert_has_errors(
+ code, checks.nonexistent_assertion_methods_and_attributes,
+ expected_errors=errors, filename="nova/tests/unit/test_context.py")
+ # Check no errors in other than 'nova/tests' directory.
+ self._assert_has_no_errors(
+ code, checks.nonexistent_assertion_methods_and_attributes,
+ filename="nova/compute/api.py")
+
+ code = """
+ mock_sample.assert_called()
+ mock_sample.assert_called_once()
+ mock_sample.assert_called_with(a, "xxxx")
+ mock_sample.assert_called_once_with(a, b)
+ mock_sample.assert_any_call(1, 2, instance=instance)
+ mock_sample.assert_has_calls([mock.call(x), mock.call(y)])
+ mock_sample.assert_not_called()
+ mock_sample.return_value = 100
+ sample.call_method(mock_sample.return_value, 100)
+ mock.Mock(return_value=100)
+
+ sample.has_other_calls([1, 2, 3, 4])
+ sample.get_called_once("TEST")
+ sample.check_called_once_with("TEST")
+ mock_assert_method.assert_called()
+ test.asset_has_all(test)
+ test_retrun_value = 99
+ test.retrun_values = 100
+ """
+ self._assert_has_no_errors(
+ code, checks.nonexistent_assertion_methods_and_attributes,
+ filename="nova/tests/unit/test_context.py")