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
bpo-32933: Implement dunder iter method on mock_open #5974
Changes from 2 commits
68e008d
3e65f55
1701b79
5af978e
c1e0ddb
a8c1d4f
e62f05a
a927fd2
646e9cd
7803a7c
a043f17
75a5d18
624e75f
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 |
---|---|---|
|
@@ -1450,6 +1450,14 @@ def test_mock_open_reuse_issue_21750(self): | |
f2_data = f2.read() | ||
self.assertEqual(f1_data, f2_data) | ||
|
||
def test_mock_open_dunder_iter_issue_32933(self): | ||
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. No need to add the issue number to the test name. 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. I was following the style of the other test methods in the same file - which is better ? 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. IMO, the version without the issue number is better. |
||
mocked_open = mock.mock_open(read_data='Remarkable Bird\nThe Norwegian Blue\nBeautiful Plumage') | ||
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. This line is too long: trim your code to fit in 79 characters. |
||
f1 = mocked_open('a-name') | ||
lines = [line for line in f1] | ||
self.assertEqual(lines[0], 'Remarkable Bird\n') | ||
self.assertEqual(lines[1], 'The Norwegian Blue\n') | ||
self.assertEqual(lines[2], 'Beautiful Plumage') | ||
|
||
def test_mock_open_write(self): | ||
# Test exception in file writing write() | ||
mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV')) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,6 +205,24 @@ def test_readline_data(self): | |
result = h.readline() | ||
self.assertEqual(result, 'foo') | ||
|
||
def test_dunder_iter_data(self): | ||
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. Style nit: I'd prefer one empty line as you already did, but we should be consistent and follow the existing style in the file. |
||
# Check that dunder_iter will return all the lines from the fake file | ||
# Added to test Issue 32933 | ||
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. No need to mention the issue number here too. |
||
mock = mock_open(read_data='foo\nbar\nbaz\n') | ||
with patch('%s.open' % __name__, mock, create=True): | ||
h = open('bar') | ||
lines = [l for l in h] | ||
self.assertEqual(lines[0], 'foo\n') | ||
self.assertEqual(lines[1], 'bar\n') | ||
self.assertEqual(lines[2], 'baz\n') | ||
|
||
# Check that we properly emulate a file that doesn't end in a newline | ||
mock = mock_open(read_data='foo') | ||
with patch('%s.open' % __name__, mock, create=True): | ||
h = open('bar') | ||
result = h.readline() | ||
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. Could you add another |
||
self.assertEqual(result, 'foo') | ||
|
||
|
||
def test_readlines_data(self): | ||
# Test that emulating a file that ends in a newline character works | ||
|
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.
Can you refactor
def _readline_side_effect():
to use this new method for clarity: