-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
bpo-30541: Add new method to seal mocks #1923
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9ba039e
ba16493
69be962
ed2a670
ef61569
3039bcf
4044ffa
0a9bf3f
e23d151
d94dbf5
0b908ae
b8fc6b7
2b9a271
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import unittest | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add "_" in the filename. For example, rename it to test_seal.py.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, didn't add it to be consistent with the rest of the tests in testmock folder. I can change the others as well in a separate commit if you want to. |
||
| from unittest import mock | ||
|
|
||
|
|
||
| class SampleObject: | ||
| def __init__(self): | ||
| self.attr_sample1 = 1 | ||
| self.attr_sample2 = 1 | ||
|
|
||
| def method_sample1(self): | ||
| pass | ||
|
|
||
| def method_sample2(self): | ||
| pass | ||
|
|
||
|
|
||
| class TestSealable(unittest.TestCase): | ||
|
|
||
| def test_attributes_return_more_mocks_by_default(self): | ||
| m = mock.Mock() | ||
|
|
||
| self.assertIsInstance(m.test, mock.Mock) | ||
| self.assertIsInstance(m.test(), mock.Mock) | ||
| self.assertIsInstance(m.test().test2(), mock.Mock) | ||
|
|
||
| def test_new_attributes_cannot_be_accessed_on_seal(self): | ||
| m = mock.Mock() | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError): | ||
| m.test | ||
| with self.assertRaises(AttributeError): | ||
| m() | ||
|
|
||
| def test_new_attributes_cannot_be_set_on_seal(self): | ||
| m = mock.Mock() | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError): | ||
| m.test = 1 | ||
|
|
||
| def test_existing_attributes_can_be_set_on_seal(self): | ||
| m = mock.Mock() | ||
| m.test.test2 = 1 | ||
|
|
||
| mock.seal(m) | ||
| m.test.test2 = 2 | ||
| self.assertEqual(m.test.test2, 2) | ||
|
|
||
| def test_new_attributes_cannot_be_set_on_child_of_seal(self): | ||
| m = mock.Mock() | ||
| m.test.test2 = 1 | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError): | ||
| m.test.test3 = 1 | ||
|
|
||
| def test_existing_attributes_allowed_after_seal(self): | ||
| m = mock.Mock() | ||
|
|
||
| m.test.return_value = 3 | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.test(), 3) | ||
|
|
||
| def test_initialized_attributes_allowed_after_seal(self): | ||
| m = mock.Mock(test_value=1) | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.test_value, 1) | ||
|
|
||
| def test_call_on_sealed_mock_fails(self): | ||
| m = mock.Mock() | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError): | ||
| m() | ||
|
|
||
| def test_call_on_defined_sealed_mock_succeeds(self): | ||
| m = mock.Mock(return_value=5) | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m(), 5) | ||
|
|
||
| def test_seals_recurse_on_added_attributes(self): | ||
| m = mock.Mock() | ||
|
|
||
| m.test1.test2().test3 = 4 | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.test1.test2().test3, 4) | ||
| with self.assertRaises(AttributeError): | ||
| m.test1.test2().test4 | ||
| with self.assertRaises(AttributeError): | ||
| m.test1.test3 | ||
|
|
||
| def test_seals_recurse_on_magic_methods(self): | ||
| m = mock.MagicMock() | ||
|
|
||
| m.test1.test2["a"].test3 = 4 | ||
| m.test1.test3[2:5].test3 = 4 | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.test1.test2["a"].test3, 4) | ||
| self.assertEqual(m.test1.test2[2:5].test3, 4) | ||
| with self.assertRaises(AttributeError): | ||
| m.test1.test2["a"].test4 | ||
| with self.assertRaises(AttributeError): | ||
| m.test1.test3[2:5].test4 | ||
|
|
||
| def test_seals_dont_recurse_on_manual_attributes(self): | ||
| m = mock.Mock(name="root_mock") | ||
|
|
||
| m.test1.test2 = mock.Mock(name="not_sealed") | ||
| m.test1.test2.test3 = 4 | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.test1.test2.test3, 4) | ||
| m.test1.test2.test4 # Does not raise | ||
| m.test1.test2.test4 = 1 # Does not raise | ||
|
|
||
| def test_integration_with_spec_att_definition(self): | ||
| """You are not restricted when using mock with spec""" | ||
| m = mock.Mock(SampleObject) | ||
|
|
||
| m.attr_sample1 = 1 | ||
| m.attr_sample3 = 3 | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.attr_sample1, 1) | ||
| self.assertEqual(m.attr_sample3, 3) | ||
| with self.assertRaises(AttributeError): | ||
| m.attr_sample2 | ||
|
|
||
| def test_integration_with_spec_method_definition(self): | ||
| """You need to defin the methods, even if they are in the spec""" | ||
| m = mock.Mock(SampleObject) | ||
|
|
||
| m.method_sample1.return_value = 1 | ||
|
|
||
| mock.seal(m) | ||
| self.assertEqual(m.method_sample1(), 1) | ||
| with self.assertRaises(AttributeError): | ||
| m.method_sample2() | ||
|
|
||
| def test_integration_with_spec_method_definition_respects_spec(self): | ||
| """You cannot define methods out of the spec""" | ||
| m = mock.Mock(SampleObject) | ||
|
|
||
| with self.assertRaises(AttributeError): | ||
| m.method_sample3.return_value = 3 | ||
|
|
||
| def test_sealed_exception_has_attribute_name(self): | ||
| m = mock.Mock() | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError) as cm: | ||
| m.SECRETE_name | ||
| self.assertIn("SECRETE_name", str(cm.exception)) | ||
|
|
||
| def test_attribute_chain_is_maintained(self): | ||
| m = mock.Mock(name="mock_name") | ||
| m.test1.test2.test3.test4 | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError) as cm: | ||
| m.test1.test2.test3.test4.boom | ||
| self.assertIn("mock_name.test1.test2.test3.test4.boom", str(cm.exception)) | ||
|
|
||
| def test_call_chain_is_maintained(self): | ||
| m = mock.Mock() | ||
| m.test1().test2.test3().test4 | ||
|
|
||
| mock.seal(m) | ||
| with self.assertRaises(AttributeError) as cm: | ||
| m.test1().test2.test3().test4() | ||
| self.assertIn("mock.test1().test2.test3().test4", str(cm.exception)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Add new function to seal a mock and prevent the automatically creation of | ||
| child mocks. Patch by Mario Corchero. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why you didn't use the same message format in line 720?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the attribute error was meaningful enough here.
It will look like:
Setting the attribute:
AttributeError('Cannot set mock.test1.test3.test4')Getting the attribute:
``AttributeError('mock.test1.test3.test4')`
Happy to change the get if you propose a new format