Skip to content

Commit

Permalink
Unit tests: Replaced all mock_calls occurrences by call_args_list (
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia authored Sep 26, 2023
1 parent 6bf1e24 commit dc4771c
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def test____iter_received_packets____stop_if_an_error_occurs(
packets = [p async for p in client.iter_received_packets(timeout=timeout)]

# Assert
assert client.mock_recv_packet.mock_calls == [mocker.call() for _ in range(2)]
assert client.mock_recv_packet.call_args_list == [mocker.call() for _ in range(2)]
assert packets == [mocker.sentinel.packet_a]

async def test____iter_received_packets____timeout_decrement(
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_test/test_async/test_api/test_client/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async def test____dunder_init____connect_to_remote(
mock_stream_socket_adapter.socket.assert_called_once_with()
mock_tcp_socket.getsockname.assert_called_once_with()
mock_tcp_socket.getpeername.assert_called_once_with()
assert mock_tcp_socket.setsockopt.mock_calls == [
assert mock_tcp_socket.setsockopt.call_args_list == [
mocker.call(IPPROTO_TCP, TCP_NODELAY, True),
mocker.call(SOL_SOCKET, SO_KEEPALIVE, True),
]
Expand Down Expand Up @@ -275,7 +275,7 @@ async def test____dunder_init____use_given_socket(
mock_stream_socket_adapter.socket.assert_called_once_with()
mock_tcp_socket.getsockname.assert_called_once_with()
mock_tcp_socket.getpeername.assert_called_once_with()
assert mock_tcp_socket.setsockopt.mock_calls == [
assert mock_tcp_socket.setsockopt.call_args_list == [
mocker.call(IPPROTO_TCP, TCP_NODELAY, True),
mocker.call(SOL_SOCKET, SO_KEEPALIVE, True),
]
Expand Down Expand Up @@ -451,7 +451,7 @@ async def test____dunder_init____ssl(
mock_stream_socket_adapter.socket.assert_called_once_with()
mock_tcp_socket.getsockname.assert_called_once_with()
mock_tcp_socket.getpeername.assert_called_once_with()
assert mock_tcp_socket.setsockopt.mock_calls == [
assert mock_tcp_socket.setsockopt.call_args_list == [
mocker.call(IPPROTO_TCP, TCP_NODELAY, True),
mocker.call(SOL_SOCKET, SO_KEEPALIVE, True),
]
Expand Down Expand Up @@ -1203,8 +1203,8 @@ async def test____recv_packet____partial_data(

# Assert
mock_backend.coro_yield.assert_not_awaited()
assert mock_stream_socket_adapter.recv.mock_calls == [mocker.call(MAX_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_data_consumer.feed.mock_calls == [mocker.call(b"pac"), mocker.call(b"ket\n")]
assert mock_stream_socket_adapter.recv.call_args_list == [mocker.call(MAX_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_data_consumer.feed.call_args_list == [mocker.call(b"pac"), mocker.call(b"ket\n")]
assert packet is mocker.sentinel.packet

@pytest.mark.usefixtures("setup_consumer_mock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ async def test____iter_received_packets_from____yields_available_packets_until_e

# Assert
assert mock_datagram_socket_adapter.recvfrom.await_args_list == [mocker.call(MAX_DATAGRAM_BUFSIZE) for _ in range(3)]
assert mock_datagram_protocol.build_packet_from_datagram.mock_calls == [
assert mock_datagram_protocol.build_packet_from_datagram.call_args_list == [
mocker.call(b"packet_1"),
mocker.call(b"packet_2"),
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ async def test____create_tcp_listeners____bind_to_any_interfaces(
reuse_address=mocker.ANY, # Determined according to OS
reuse_port=mocker.sentinel.reuse_port,
)
assert mock_ListenerSocketAdapter.mock_calls == [
assert mock_ListenerSocketAdapter.call_args_list == [
mocker.call(mock_tcp_socket, event_loop, expected_factory) for _ in range(2)
]
assert listener_sockets == [mocker.sentinel.listener_socket, mocker.sentinel.listener_socket]
Expand Down Expand Up @@ -1064,7 +1064,7 @@ async def test____create_tcp_listeners____bind_to_several_hosts(
reuse_address=mocker.ANY, # Determined according to OS
reuse_port=mocker.sentinel.reuse_port,
)
assert mock_ListenerSocketAdapter.mock_calls == [
assert mock_ListenerSocketAdapter.call_args_list == [
mocker.call(mock_tcp_socket, event_loop, expected_factory) for _ in range(2)
]
assert listener_sockets == [mocker.sentinel.listener_socket, mocker.sentinel.listener_socket]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ async def test____recvfrom____connection_lost____transport_closed_by_protocol_wh

# Assert
assert exc_info.value.errno == ECONNABORTED
assert len(mock_asyncio_exception_queue.get_nowait.mock_calls) == 2
assert len(mock_asyncio_exception_queue.get_nowait.call_args_list) == 2
mock_asyncio_recv_queue.get.assert_awaited_once_with()

async def test____recvfrom____raise_exception____protocol_already_sent_exception_in_queue(
Expand Down Expand Up @@ -343,7 +343,7 @@ def get_nowait_side_effect() -> Exception:
await endpoint.recvfrom()

# Assert
assert len(mock_asyncio_exception_queue.get_nowait.mock_calls) == 2
assert len(mock_asyncio_exception_queue.get_nowait.call_args_list) == 2
mock_asyncio_recv_queue.get.assert_awaited_once_with()

@pytest.mark.parametrize("address", [("127.0.0.1", 12345), None], ids=repr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def _busy_socket_task(
) -> asyncio.Task[Any]:
accept_task = event_loop.create_task(coroutine)
async with asyncio.timeout(5):
while len(mock_socket_method.mock_calls) == 0:
while len(mock_socket_method.call_args_list) == 0:
await asyncio.sleep(0)
mock_socket_method.reset_mock()
return accept_task
Expand Down
10 changes: 5 additions & 5 deletions tests/unit_test/test_async/test_asyncio_backend/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ async def test____create_connection____first_failed(
socket = await create_connection(remote_host, remote_port, event_loop, local_address=local_address)

# Assert
assert mock_socket_cls.mock_calls == [
assert mock_socket_cls.call_args_list == [
mocker.call(AF_INET, SOCK_STREAM, IPPROTO_TCP),
mocker.call(AF_INET6, SOCK_STREAM, IPPROTO_TCP),
]
Expand Down Expand Up @@ -359,7 +359,7 @@ async def test____create_connection____all_failed(
assert all(isinstance(exc, OSError) for exc in os_errors.exceptions)
del os_errors

assert mock_socket_cls.mock_calls == [
assert mock_socket_cls.call_args_list == [
mocker.call(AF_INET, SOCK_STREAM, IPPROTO_TCP),
mocker.call(AF_INET6, SOCK_STREAM, IPPROTO_TCP),
]
Expand Down Expand Up @@ -595,7 +595,7 @@ async def test____create_datagram_socket____first_failed(
)

# Assert
assert mock_socket_cls.mock_calls == [
assert mock_socket_cls.call_args_list == [
mocker.call(AF_INET, SOCK_DGRAM, IPPROTO_UDP),
mocker.call(AF_INET6, SOCK_DGRAM, IPPROTO_UDP),
]
Expand Down Expand Up @@ -668,7 +668,7 @@ async def test____create_datagram_socket____all_failed(
assert all(isinstance(exc, OSError) for exc in os_errors.exceptions)
del os_errors

assert mock_socket_cls.mock_calls == [
assert mock_socket_cls.call_args_list == [
mocker.call(AF_INET, SOCK_DGRAM, IPPROTO_UDP),
mocker.call(AF_INET6, SOCK_DGRAM, IPPROTO_UDP),
]
Expand Down Expand Up @@ -888,7 +888,7 @@ def test____open_listener_sockets_from_getaddrinfo_result____create_listener_soc

# Assert
assert len(sockets) == len(addrinfo_list)
assert mock_socket_cls.mock_calls == [mocker.call(f, t, p) for f, t, p, _, _ in addrinfo_list]
assert mock_socket_cls.call_args_list == [mocker.call(f, t, p) for f, t, p, _, _ in addrinfo_list]
for socket, (sock_family, _, _, _, sock_addr) in zip(sockets, addrinfo_list, strict=True):
if reuse_address and SO_REUSEADDR_available:
socket.setsockopt.assert_any_call(SOL_SOCKET, SO_REUSEADDR, True)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_test/test_serializers/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def side_effect(file: IO[bytes]) -> Any:
packet, remaining_data = exc_info.value.value

# Assert
assert len(mock_load_from_file_func.mock_calls) == 4
assert len(mock_load_from_file_func.call_args_list) == 4
assert packet is mocker.sentinel.packet
assert remaining_data == b""

Expand Down
6 changes: 3 additions & 3 deletions tests/unit_test/test_serializers/test_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ def test____incremental_deserialize____decompress_chunks(

# Assert
mock_serializer_new_decompressor_stream.assert_called_once_with()
assert mock_decompressor_stream.decompress.mock_calls == [mocker.call(b"chunk 1"), mocker.call(b"chunk 2")]
assert len(mock_decompressor_stream_eof.mock_calls) == 3
assert mock_decompressor_stream.decompress.call_args_list == [mocker.call(b"chunk 1"), mocker.call(b"chunk 2")]
assert len(mock_decompressor_stream_eof.call_args_list) == 3
mock_decompressor_stream_unused_data.assert_called_once()
mock_serializer.deserialize.assert_called_once_with(b"decompressed chunk 1 decompressed chunk 2")
assert packet is mocker.sentinel.packet
Expand Down Expand Up @@ -354,7 +354,7 @@ def test____incremental_deserialize____translate_deserialize_errors(
# Assert
mock_serializer_new_decompressor_stream.assert_called_once_with()
mock_decompressor_stream.decompress.assert_called_once_with(b"chunk")
assert len(mock_decompressor_stream_eof.mock_calls) == 2
assert len(mock_decompressor_stream_eof.call_args_list) == 2
mock_decompressor_stream_unused_data.assert_called_once()
mock_serializer.deserialize.assert_called_once_with(b"decompressed chunk")
assert exception.__cause__ is mock_serializer.deserialize.side_effect
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_test/test_sync/test_client/test_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def side_effect(timeout: Any) -> Any:
packets = list(client.iter_received_packets(timeout=123456789))

# Assert
assert client.mock_recv_packet.mock_calls == [mocker.call(123456789) for _ in range(4)]
assert client.mock_recv_packet.call_args_list == [mocker.call(123456789) for _ in range(4)]
assert packets == [mocker.sentinel.packet_a, mocker.sentinel.packet_b, mocker.sentinel.packet_c]

@pytest.mark.parametrize("timeout", [123456789, None])
Expand All @@ -97,7 +97,7 @@ def test____iter_received_packets____with_given_timeout_stop_if_an_error_occurs(
packets = list(client.iter_received_packets(timeout=timeout))

# Assert
assert client.mock_recv_packet.mock_calls == [mocker.call(timeout) for _ in range(2)]
assert client.mock_recv_packet.call_args_list == [mocker.call(timeout) for _ in range(2)]
assert packets == [mocker.sentinel.packet_a]

def test____iter_received_packets____timeout_decrement(
Expand Down
36 changes: 18 additions & 18 deletions tests/unit_test/test_sync/test_client/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ def test____dunder_init____connect_to_remote(
mock_ssl_socket.getsockname.assert_not_called()
mock_ssl_socket.getpeername.assert_not_called()

assert used_socket.setsockopt.mock_calls == [
assert used_socket.setsockopt.call_args_list == [
mocker.call(IPPROTO_TCP, TCP_NODELAY, True),
mocker.call(SOL_SOCKET, SO_KEEPALIVE, True),
]
Expand Down Expand Up @@ -402,7 +402,7 @@ def test____dunder_init____use_given_socket(
mock_ssl_socket.getsockname.assert_not_called()
mock_ssl_socket.getpeername.assert_not_called()

assert used_socket.setsockopt.mock_calls == [
assert used_socket.setsockopt.call_args_list == [
mocker.call(IPPROTO_TCP, TCP_NODELAY, True),
mocker.call(SOL_SOCKET, SO_KEEPALIVE, True),
]
Expand Down Expand Up @@ -942,11 +942,11 @@ def test____dunder_init____ssl____handshake_timeout(

# Assert
if expected_timeout == 0:
assert len(mock_ssl_socket.do_handshake.mock_calls) == 1
assert len(mock_ssl_socket.do_handshake.call_args_list) == 1
mock_selector_register.assert_not_called()
mock_selector_select.assert_not_called()
else:
assert len(mock_ssl_socket.do_handshake.mock_calls) == 2
assert len(mock_ssl_socket.do_handshake.call_args_list) == 2
mock_selector_register.assert_called_once_with(mock_ssl_socket, would_block_event)
mock_selector_select.assert_called_once_with(expected_timeout)

Expand Down Expand Up @@ -1240,7 +1240,7 @@ def test____send_packet____blocking_operation(
if send_timeout != 0:
mock_selector_register.assert_called_with(mock_used_socket, EVENT_WRITE)
mock_selector_select.assert_called()
assert mock_used_socket.send.mock_calls == [
assert mock_used_socket.send.call_args_list == [
mocker.call(b"packet\n"),
mocker.call(b"packet\n"),
mocker.call(b"et\n"),
Expand All @@ -1249,7 +1249,7 @@ def test____send_packet____blocking_operation(
else:
mock_selector_register.assert_not_called()
mock_selector_select.assert_not_called()
assert mock_used_socket.send.mock_calls == [mocker.call(b"packet\n")]
assert mock_used_socket.send.call_args_list == [mocker.call(b"packet\n")]
mock_used_socket.getsockopt.assert_not_called()

@pytest.mark.usefixtures("setup_producer_mock")
Expand Down Expand Up @@ -1514,8 +1514,8 @@ def test____recv_packet____blocking____partial_data(
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
assert mock_used_socket.recv.mock_calls == [mocker.call(MAX_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_data_consumer.feed.mock_calls == [mocker.call(b"pac"), mocker.call(b"ket\n")]
assert mock_used_socket.recv.call_args_list == [mocker.call(MAX_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_data_consumer.feed.call_args_list == [mocker.call(b"pac"), mocker.call(b"ket\n")]
assert packet is mocker.sentinel.packet

@pytest.mark.parametrize("recv_timeout", [0], indirect=True) # Only test with timeout==0
Expand Down Expand Up @@ -1544,8 +1544,8 @@ def test____recv_packet_____non_blocking____partial_data(
if max_recv_size == 3:
packet: Any = client.recv_packet(timeout=recv_timeout)

assert mock_used_socket.recv.mock_calls == [mocker.call(max_recv_size) for _ in range(3)]
assert mock_stream_data_consumer.feed.mock_calls == [mocker.call(b"pac"), mocker.call(b"ket"), mocker.call(b"\n")]
assert mock_used_socket.recv.call_args_list == [mocker.call(max_recv_size) for _ in range(3)]
assert mock_stream_data_consumer.feed.call_args_list == [mocker.call(b"pac"), mocker.call(b"ket"), mocker.call(b"\n")]
assert packet is mocker.sentinel.packet
else:
with pytest.raises(TimeoutError, match=r"^recv_packet\(\) timed out$"):
Expand Down Expand Up @@ -1751,11 +1751,11 @@ def test____recv_packet____timeout(
_ = client.recv_packet(timeout=recv_timeout)

if recv_timeout == 0:
assert len(mock_used_socket.recv.mock_calls) == 1
assert len(mock_used_socket.recv.call_args_list) == 1
mock_selector_register.assert_not_called()
mock_selector_select.assert_not_called()
else:
assert len(mock_used_socket.recv.mock_calls) == 2
assert len(mock_used_socket.recv.call_args_list) == 2
mock_selector_register.assert_called_with(mock_used_socket, EVENT_READ)
mock_selector_select.assert_any_call(recv_timeout)
mock_stream_data_consumer.feed.assert_not_called()
Expand Down Expand Up @@ -1796,8 +1796,8 @@ def test____iter_received_packets____yields_available_packets_with_given_timeout
packets = list(client.iter_received_packets(timeout=recv_timeout))

# Assert
assert mock_used_socket.recv.mock_calls == [mocker.call(max_recv_size) for _ in range(7)]
assert mock_stream_data_consumer.feed.mock_calls == [
assert mock_used_socket.recv.call_args_list == [mocker.call(max_recv_size) for _ in range(7)]
assert mock_stream_data_consumer.feed.call_args_list == [
mocker.call(b"pac"),
mocker.call(b"ket"),
mocker.call(b"_1\np"),
Expand Down Expand Up @@ -1825,8 +1825,8 @@ def test____iter_received_packets____yields_available_packets_until_eof(
packets = list(client.iter_received_packets(timeout=recv_timeout))

# Assert
assert mock_used_socket.recv.mock_calls == [mocker.call(max_recv_size) for _ in range(7)]
assert mock_stream_data_consumer.feed.mock_calls == [
assert mock_used_socket.recv.call_args_list == [mocker.call(max_recv_size) for _ in range(7)]
assert mock_stream_data_consumer.feed.call_args_list == [
mocker.call(b"pac"),
mocker.call(b"ket"),
mocker.call(b"_1\np"),
Expand Down Expand Up @@ -1854,8 +1854,8 @@ def test____iter_received_packets____yields_available_packets_until_error(
packets = list(client.iter_received_packets(timeout=recv_timeout))

# Assert
assert mock_used_socket.recv.mock_calls == [mocker.call(max_recv_size) for _ in range(7)]
assert mock_stream_data_consumer.feed.mock_calls == [
assert mock_used_socket.recv.call_args_list == [mocker.call(max_recv_size) for _ in range(7)]
assert mock_stream_data_consumer.feed.call_args_list == [
mocker.call(b"pac"),
mocker.call(b"ket"),
mocker.call(b"_1\np"),
Expand Down
Loading

0 comments on commit dc4771c

Please sign in to comment.