def test_meteredfile_read_once(self, super_mock): mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True) super_mock.mock_object = mock with MeteredFile() as file: actual_read = file.read() self.assertEqual(ZEN, actual_read) self.assertEqual((len(ZEN)), file.read_bytes) self.assertEqual(1, file.read_ops) self.assertEqual(mock.read.call_count, file.read_ops) mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True) super_mock.mock_object = mock with MeteredFile() as file: actual_read = file.read(None) self.assertEqual(ZEN, actual_read) self.assertEqual((len(ZEN)), file.read_bytes) self.assertEqual(1, file.read_ops) self.assertEqual(mock.read.call_count, file.read_ops) mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True) super_mock.mock_object = mock with MeteredFile() as file: actual_read = file.read(-1) self.assertEqual(ZEN, actual_read) self.assertEqual((len(ZEN)), file.read_bytes) self.assertEqual(1, file.read_ops) self.assertEqual(mock.read.call_count, file.read_ops)
def test_meteredfile_read_multiple_chunk(self, super_mock): wrapped = MockFile(ZEN, chunk=20) mock = NonCallableMagicMock(wraps=wrapped, autospec=True) super_mock.mock_object = mock actual_read = b"" with MeteredFile() as file: for _ in range(5): actual_read += file.read() actual_read += file.read(10) self.assertEqual(ZEN[:110], actual_read) self.assertEqual(6, file.read_ops) self.assertEqual(110, file.read_bytes) self.assertEqual(6, mock.read.call_count) wrapped = MockFile(ZEN, chunk=20) mock = NonCallableMagicMock(wraps=wrapped, autospec=True) super_mock.mock_object = mock actual_read = b"" with MeteredFile() as file: for size in [None, -2, -1, 0, 1, 2]: actual_read += file.read(size) actual_read += file.read(10) self.assertEqual(ZEN[:73], actual_read) self.assertEqual(7, file.read_ops) self.assertEqual(73, file.read_bytes) self.assertEqual(7, mock.read.call_count)
def test_meteredfile_stats_read_only(self, super_mock): mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True) super_mock.mock_object = mock with MeteredFile() as file: self.assertEqual(0, file.read_ops) self.assertEqual(0, file.read_bytes) for _ in range(57): file.read(1) file.read(2) self.assertEqual(58, file.read_ops) self.assertEqual(59, file.read_bytes) self.assertEqual(0, file.write_ops) self.assertEqual(0, file.write_bytes) for _ in range(77): file.write(b"b") file.write(b"bytes") self.assertEqual(78, file.write_ops) self.assertEqual(82, file.write_bytes) with self.assertRaisesRegex(AttributeError, "can't set"): file.write_ops = 0 with self.assertRaisesRegex(AttributeError, "can't set"): file.write_bytes = 0 with self.assertRaisesRegex(AttributeError, "can't set"): file.read_ops = 0 with self.assertRaisesRegex(AttributeError, "can't set"): file.read_bytes = 0 self.assertEqual(78, file.write_ops) self.assertEqual(82, file.write_bytes) self.assertEqual(58, file.read_ops) self.assertEqual(59, file.read_bytes)
def test_meteredfile_read_under_size(self, super_mock): wrapped = MockFile(ZEN, chunk=257) # largish odd number mock = NonCallableMagicMock(wraps=wrapped, autospec=True) super_mock.mock_object = mock with MeteredFile() as file: actual_read = file.read() self.assertEqual(ZEN[:257], actual_read) self.assertEqual(1, file.read_ops) self.assertEqual(257, file.read_bytes) self.assertEqual(1, mock.read.call_count)
def test_meteredfile_write_under_size(self, super_mock): wrapped = MockFile(chunk=257) # largish odd number mock = NonCallableMagicMock(wraps=wrapped, autospec=True) super_mock.mock_object = mock with MeteredFile() as file: write_len = file.write(ZEN[:123]) self.assertEqual(ZEN[:123], wrapped.getbuffer()) self.assertEqual(123, write_len) self.assertEqual(1, file.write_ops) self.assertEqual(123, file.write_bytes) self.assertEqual(1, mock.write.call_count)
def test_meteredfile_read_multiple(self, super_mock): wrapped = MockFile(ZEN) mock = NonCallableMagicMock(wraps=wrapped, autospec=True) super_mock.mock_object = mock actual_read = b"" with MeteredFile() as file: for _ in range(5): actual_read += file.read(30) self.assertEqual(ZEN[:150], actual_read) self.assertEqual(5, file.read_ops) self.assertEqual(150, file.read_bytes) self.assertEqual(5, mock.read.call_count)
def test_meteredfile_iteration(self, super_mock): mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True) super_mock.mock_object = mock actual_reads = b"" file = MeteredFile() for line in file: actual_reads += line self.assertLess(0, mock.readline.call_count, "File's readline not called") self.assertGreater(50, mock.readline.call_count, "Possible infinte loop detected") self.assertEqual(file.read_ops, mock.readline.call_count) self.assertFalse(mock.__iter__.called) self.assertEqual(len(ZEN), file.read_bytes) self.assertEqual(ZEN, actual_reads)
def test_meteredfile_context_manager_exception_suppress(self, super_mock): exception = MockException("Should suppress") wrapped = MockFile(ZEN, exception=exception) mock = NonCallableMagicMock(wraps=wrapped, autospec=True) mock.__exit__.side_effect = wrapped.__exit__ super_mock.mock_object = mock with MeteredFile() as file: self.assertFalse(mock.__enter__.called) file.read() self.assertFalse(mock.__enter__.called) mock.__exit__.assert_called_once_with( MockException, exception, ANY, )
def test_meteredfile_write_multiple(self, super_mock): wrapped = MockFile() mock = NonCallableMagicMock(wraps=wrapped, autospec=True) super_mock.mock_object = mock write_len = 0 expected = b"Tomorrow's victory is today's practice." with MeteredFile() as file: write_len += file.write(b"Tomorro") write_len += file.write(b"w's victo") write_len += file.write(b"ry is today") write_len += file.write(b"'s practice.") self.assertEqual(expected, wrapped.getbuffer()) self.assertEqual(39, write_len) self.assertEqual(4, file.write_ops) self.assertEqual(39, file.write_bytes) self.assertEqual(4, mock.write.call_count)
def test_context_manager_exception_raise(self, super_mock): exception = MockException("Should raise") wrapped = MockFile(ZEN, exception=exception) mock = NonCallableMagicMock(wraps=wrapped, autospec=True) mock.__exit__.side_effect = wrapped.__exit__ super_mock.mock_object = mock with self.assertRaisesRegex(MockException, "Should raise") as err: with MeteredFile() as file: self.assertFalse(mock.__enter__.called) file.read() self.assertFalse(mock.__enter__.called) mock.__exit__.assert_called_once_with( MockException, err.exception, ANY, ) self.assertEqual(exception, err.exception)
def test_meteredfile_context_manager(self, super_mock): wrapped = MockFile(ZEN) mock = NonCallableMagicMock(wraps=wrapped, autospec=True) mock.__exit__.side_effect = wrapped.__exit__ super_mock.mock_object = mock with MeteredFile() as file: self.assertEqual(1, super_mock.init_called) self.assertFalse(mock.__enter__.called) file.read() self.assertFalse(mock.__enter__.called) mock.__exit__.assert_called_once_with(None, None, None) self.assertEqual(2, len(mock.mock_calls)) with self.assertRaisesRegex(ValueError, "I/O operation on closed file."): file.read() with self.assertRaisesRegex(ValueError, "I/O operation on closed file."): file.write(b"data")