summaryrefslogtreecommitdiff
path: root/mock/tests/testwith.py
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2015-08-06 13:15:51 +0300
committerRobert Collins <rbtcollins@hp.com>2016-03-23 18:31:30 +1300
commit5efb4e504a6f4378dd65b825dd572d8e175ebd2e (patch)
treec2b86f01d1b34cb9883f8aa24e46802a070223a7 /mock/tests/testwith.py
parent456473c6c149088add1cebb7b8fae84d4c0295f4 (diff)
downloadmock-5efb4e504a6f4378dd65b825dd572d8e175ebd2e.tar.gz
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 'mock/tests/testwith.py')
-rw-r--r--mock/tests/testwith.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/mock/tests/testwith.py b/mock/tests/testwith.py
index b9cfab1..aa7812b 100644
--- a/mock/tests/testwith.py
+++ b/mock/tests/testwith.py
@@ -229,6 +229,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