diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2015-08-06 10:16:52 (GMT) |
---|---|---|
committer | Berker Peksag <berker.peksag@gmail.com> | 2015-08-06 10:16:52 (GMT) |
commit | abfaecdfdd9c10732e221eada3c259523fd14a8f (patch) | |
tree | bf41008c5b0dada835fb70aa471617b55348e03c /Lib/unittest/test/testmock | |
parent | 888a6729da50a819438c6aa47e5ea7999d039b24 (diff) | |
parent | 86b34da5ef249b865281704a5f6721391edb0c1c (diff) | |
download | cpython-abfaecdfdd9c10732e221eada3c259523fd14a8f.zip cpython-abfaecdfdd9c10732e221eada3c259523fd14a8f.tar.gz cpython-abfaecdfdd9c10732e221eada3c259523fd14a8f.tar.bz2 |
Issue #23004: mock_open() now reads binary data correctly when the type of read_data is bytes.
Initial patch by Aaron Hill.
Diffstat (limited to 'Lib/unittest/test/testmock')
-rw-r--r-- | Lib/unittest/test/testmock/testwith.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py index b6bfb75..a7bee73 100644 --- a/Lib/unittest/test/testmock/testwith.py +++ b/Lib/unittest/test/testmock/testwith.py @@ -224,6 +224,34 @@ class TestMockOpen(unittest.TestCase): self.assertEqual(result, ['foo\n', 'bar\n', 'baz']) + def test_read_bytes(self): + mock = mock_open(read_data=b'\xc6') + with patch('%s.open' % __name__, mock, create=True): + with open('abc', 'rb') as f: + result = f.read() + self.assertEqual(result, b'\xc6') + + + def test_readline_bytes(self): + m = mock_open(read_data=b'abc\ndef\nghi\n') + with patch('%s.open' % __name__, m, create=True): + with open('abc', 'rb') as f: + line1 = f.readline() + line2 = f.readline() + line3 = f.readline() + self.assertEqual(line1, b'abc\n') + self.assertEqual(line2, b'def\n') + self.assertEqual(line3, b'ghi\n') + + + def test_readlines_bytes(self): + m = mock_open(read_data=b'abc\ndef\nghi\n') + with patch('%s.open' % __name__, m, create=True): + with open('abc', 'rb') as f: + result = f.readlines() + self.assertEqual(result, [b'abc\n', b'def\n', b'ghi\n']) + + def test_mock_open_read_with_argument(self): # At one point calling read with an argument was broken # for mocks returned by mock_open |