Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions exercises/practice/paasio/paasio_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from paasio import MeteredFile, MeteredSocket


class PaasioTest(unittest.TestCase):
def test_meteredsocket_context_manager(self):
wrapped = MockSock()
Expand Down Expand Up @@ -34,7 +35,9 @@ def test_meteredsocket_context_manager_exception_raise(self):
socket.recv(4096)
self.assertFalse(mock.__enter__.called)
mock.__exit__.assert_called_once_with(
MockException, err.exception, ANY,
MockException,
err.exception,
ANY,
)
self.assertEqual(exception, err.exception)

Expand All @@ -48,7 +51,9 @@ def test_meteredsocket_context_manager_exception_suppress(self):
socket.recv(4096)
self.assertFalse(mock.__enter__.called)
mock.__exit__.assert_called_once_with(
MockException, exception, ANY,
MockException,
exception,
ANY,
)

def test_meteredsocket_recv_once(self):
Expand Down Expand Up @@ -220,14 +225,11 @@ def test_meteredfile_context_manager(self, super_mock):
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."
):
with self.assertRaisesRegex(ValueError, "I/O operation on closed file."):
file.read()
with self.assertRaisesRegex(
ValueError, "I/O operation on closed file."
):
with self.assertRaisesRegex(ValueError, "I/O operation on closed file."):
file.write(b"data")

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_context_manager_exception_raise(self, super_mock):
exception = MockException("Should raise")
Expand All @@ -241,9 +243,12 @@ def test_meteredfile_context_manager_exception_raise(self, super_mock):
file.read()
self.assertFalse(mock.__enter__.called)
mock.__exit__.assert_called_once_with(
MockException, err.exception, ANY,
MockException,
err.exception,
ANY,
)
self.assertEqual(exception, err.exception)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_context_manager_exception_suppress(self, super_mock):
exception = MockException("Should suppress")
Expand All @@ -256,8 +261,11 @@ def test_meteredfile_context_manager_exception_suppress(self, super_mock):
file.read()
self.assertFalse(mock.__enter__.called)
mock.__exit__.assert_called_once_with(
MockException, exception, ANY,
MockException,
exception,
ANY,
)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_iteration(self, super_mock):
mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True)
Expand All @@ -266,16 +274,15 @@ def test_meteredfile_iteration(self, super_mock):
file = MeteredFile()
for line in file:
actual_reads += line
self.assertLess(
0, mock.readline.call_count, "File's readline not called"
)
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)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_read_once(self, super_mock):
mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True)
Expand All @@ -302,6 +309,7 @@ def test_meteredfile_read_once(self, super_mock):
self.assertEqual((len(ZEN)), file.read_bytes)
self.assertEqual(1, file.read_ops)
self.assertEqual(mock.read.call_count, file.read_ops)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_read_multiple(self, super_mock):
wrapped = MockFile(ZEN)
Expand All @@ -315,6 +323,7 @@ def test_meteredfile_read_multiple(self, super_mock):
self.assertEqual(5, file.read_ops)
self.assertEqual(150, file.read_bytes)
self.assertEqual(5, mock.read.call_count)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_read_multiple_chunk(self, super_mock):
wrapped = MockFile(ZEN, chunk=20)
Expand All @@ -341,6 +350,7 @@ def test_meteredfile_read_multiple_chunk(self, super_mock):
self.assertEqual(7, file.read_ops)
self.assertEqual(73, file.read_bytes)
self.assertEqual(7, mock.read.call_count)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_read_under_size(self, super_mock):
wrapped = MockFile(ZEN, chunk=257) # largish odd number
Expand All @@ -352,6 +362,7 @@ def test_meteredfile_read_under_size(self, super_mock):
self.assertEqual(1, file.read_ops)
self.assertEqual(257, file.read_bytes)
self.assertEqual(1, mock.read.call_count)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_write_once(self, super_mock):
wrapped = MockFile(chunk=257) # largish odd number
Expand All @@ -364,6 +375,7 @@ def test_meteredfile_write_once(self, super_mock):
self.assertEqual(1, file.write_ops)
self.assertEqual(257, file.write_bytes)
self.assertEqual(1, mock.write.call_count)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_write_multiple(self, super_mock):
wrapped = MockFile()
Expand All @@ -381,6 +393,7 @@ def test_meteredfile_write_multiple(self, super_mock):
self.assertEqual(4, file.write_ops)
self.assertEqual(39, file.write_bytes)
self.assertEqual(4, mock.write.call_count)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_write_under_size(self, super_mock):
wrapped = MockFile(chunk=257) # largish odd number
Expand All @@ -393,6 +406,7 @@ def test_meteredfile_write_under_size(self, super_mock):
self.assertEqual(1, file.write_ops)
self.assertEqual(123, file.write_bytes)
self.assertEqual(1, mock.write.call_count)

@patch("paasio.super", create=True, new_callable=SuperMock)
def test_meteredfile_stats_read_only(self, super_mock):
mock = NonCallableMagicMock(wraps=MockFile(ZEN), autospec=True)
Expand Down
17 changes: 4 additions & 13 deletions exercises/practice/paasio/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os



ZEN = b"""Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Expand Down Expand Up @@ -84,14 +83,10 @@ def recv(self, bufsize, flags=0):
if self.__closed:
raise OSError(errno.EBADF, os.strerror(errno.EBADF))
if bufsize is None:
raise TypeError(
"'NoneType' object cannot be interpreted as an integer"
)
raise TypeError("'NoneType' object cannot be interpreted as an integer")
if not isinstance(flags, int):
raise TypeError(
"an integer is required (got type {})".format(
type(flags).__name__
)
"an integer is required (got type {})".format(type(flags).__name__)
)
self.flags = flags
if self.__exception is not None:
Expand All @@ -106,9 +101,7 @@ def send(self, data, flags=0):
raise OSError(errno.EBADF, os.strerror(errno.EBADF))
if not isinstance(flags, int):
raise TypeError(
"an integer is required (got type {})".format(
type(flags).__name__
)
"an integer is required (got type {})".format(type(flags).__name__)
)
self.flags = flags
if self.__chunk is None:
Expand All @@ -130,9 +123,7 @@ def __call__(self, *args, **kwargs):
if frame is None:
raise RuntimeError("Could not get current frame object")
stack = inspect.getouterframes(frame)
if any(
frame[3] == "__init__" and "paasio" in frame[1] for frame in stack
):
if any(frame[3] == "__init__" and "paasio" in frame[1] for frame in stack):
return self
else:
return self.mock_object
Expand Down