Skip to content

Commit 0df845a

Browse files
committed
Don't use a custom asyncio loop in test
There is really no need to create our own loop for these tests, we can just use the loop provided by `pytest-asyncio` which already offers a lot of tools for detecting issues and configure how we want the loop to work. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent cd6ce02 commit 0df845a

File tree

1 file changed

+37
-55
lines changed

1 file changed

+37
-55
lines changed

tests/test_client.py

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ class SetupParams: # pylint: disable=too-many-instance-attributes
5252

5353
client: Client
5454
mock_stub: AsyncMock
55-
loop: asyncio.AbstractEventLoop
5655
gridpool_id: int
5756
delivery_area: DeliveryArea
5857
delivery_period: DeliveryPeriod
@@ -72,10 +71,6 @@ def set_up() -> Generator[Any, Any, Any]:
7271
mock_stub = AsyncMock()
7372
client._stub = mock_stub # pylint: disable=protected-access
7473

75-
# Create a new event loop for each test
76-
loop = asyncio.new_event_loop()
77-
asyncio.set_event_loop(loop)
78-
7974
# Set up the parameters for the orders
8075
# Setting delivery start to the next day 12:00
8176
delivery_start = (datetime.now(timezone.utc) + timedelta(days=1)).replace(
@@ -97,7 +92,6 @@ def set_up() -> Generator[Any, Any, Any]:
9792
yield SetupParams(
9893
client=client,
9994
mock_stub=mock_stub,
100-
loop=loop,
10195
gridpool_id=gridpool_id,
10296
delivery_area=delivery_area,
10397
delivery_period=delivery_period,
@@ -109,8 +103,6 @@ def set_up() -> Generator[Any, Any, Any]:
109103
valid_until=valid_until,
110104
)
111105

112-
loop.close()
113-
114106

115107
# pylint: disable=redefined-outer-name
116108
def set_up_order_detail_response(
@@ -213,7 +205,7 @@ async def test_receive_public_trades(
213205
]
214206

215207

216-
def test_create_gridpool_order(
208+
async def test_create_gridpool_order(
217209
set_up: SetupParams,
218210
) -> None:
219211
"""
@@ -229,17 +221,15 @@ def test_create_gridpool_order(
229221
)
230222
set_up.mock_stub.CreateGridpoolOrder.return_value = mock_response
231223

232-
set_up.loop.run_until_complete(
233-
set_up.client.create_gridpool_order(
234-
gridpool_id=set_up.gridpool_id,
235-
delivery_area=set_up.delivery_area,
236-
delivery_period=set_up.delivery_period,
237-
order_type=set_up.order_type,
238-
side=set_up.side,
239-
price=set_up.price,
240-
quantity=set_up.quantity,
241-
execution_option=set_up.order_execution_option, # optional field
242-
)
224+
await set_up.client.create_gridpool_order(
225+
gridpool_id=set_up.gridpool_id,
226+
delivery_area=set_up.delivery_area,
227+
delivery_period=set_up.delivery_period,
228+
order_type=set_up.order_type,
229+
side=set_up.side,
230+
price=set_up.price,
231+
quantity=set_up.quantity,
232+
execution_option=set_up.order_execution_option, # optional field
243233
)
244234

245235
set_up.mock_stub.CreateGridpoolOrder.assert_called_once()
@@ -253,7 +243,7 @@ def test_create_gridpool_order(
253243
assert args[0].order.execution_option == set_up.order_execution_option.to_pb()
254244

255245

256-
def test_update_gridpool_order(
246+
async def test_update_gridpool_order(
257247
set_up: SetupParams,
258248
) -> None:
259249
"""Test the method updating a gridpool order."""
@@ -265,13 +255,11 @@ def test_update_gridpool_order(
265255
)
266256
set_up.mock_stub.UpdateGridpoolOrder.return_value = mock_response
267257

268-
set_up.loop.run_until_complete(
269-
set_up.client.update_gridpool_order(
270-
gridpool_id=set_up.gridpool_id,
271-
order_id=1,
272-
quantity=set_up.quantity,
273-
valid_until=set_up.valid_until,
274-
)
258+
await set_up.client.update_gridpool_order(
259+
gridpool_id=set_up.gridpool_id,
260+
order_id=1,
261+
quantity=set_up.quantity,
262+
valid_until=set_up.valid_until,
275263
)
276264

277265
valid_until_pb = timestamp_pb2.Timestamp()
@@ -287,7 +275,7 @@ def test_update_gridpool_order(
287275
), "Price field should not be set."
288276

289277

290-
def test_cancel_gridpool_order(
278+
async def test_cancel_gridpool_order(
291279
set_up: SetupParams,
292280
) -> None:
293281
"""Test the method cancelling gridpool orders."""
@@ -303,10 +291,8 @@ def test_cancel_gridpool_order(
303291

304292
set_up.mock_stub.CancelGridpoolOrder.return_value = mock_response
305293

306-
set_up.loop.run_until_complete(
307-
set_up.client.cancel_gridpool_order(
308-
gridpool_id=set_up.gridpool_id, order_id=order_id
309-
)
294+
await set_up.client.cancel_gridpool_order(
295+
gridpool_id=set_up.gridpool_id, order_id=order_id
310296
)
311297

312298
set_up.mock_stub.CancelGridpoolOrder.assert_called_once()
@@ -413,7 +399,7 @@ async def test_list_gridpool_orders(
413399
),
414400
],
415401
)
416-
def test_create_gridpool_order_with_invalid_params(
402+
async def test_create_gridpool_order_with_invalid_params(
417403
# pylint: disable=too-many-arguments, too-many-positional-arguments
418404
set_up: SetupParams,
419405
price: Price,
@@ -425,18 +411,16 @@ def test_create_gridpool_order_with_invalid_params(
425411
) -> None:
426412
"""Test creating an order with invalid input parameters."""
427413
with pytest.raises(expected_exception):
428-
set_up.loop.run_until_complete(
429-
set_up.client.create_gridpool_order(
430-
gridpool_id=set_up.gridpool_id,
431-
delivery_area=set_up.delivery_area,
432-
delivery_period=delivery_period,
433-
order_type=OrderType.LIMIT,
434-
side=MarketSide.BUY,
435-
price=price,
436-
quantity=quantity,
437-
execution_option=execution_option,
438-
valid_until=valid_until,
439-
)
414+
await set_up.client.create_gridpool_order(
415+
gridpool_id=set_up.gridpool_id,
416+
delivery_area=set_up.delivery_area,
417+
delivery_period=delivery_period,
418+
order_type=OrderType.LIMIT,
419+
side=MarketSide.BUY,
420+
price=price,
421+
quantity=quantity,
422+
execution_option=execution_option,
423+
valid_until=valid_until,
440424
)
441425

442426

@@ -466,7 +450,7 @@ def test_create_gridpool_order_with_invalid_params(
466450
),
467451
],
468452
)
469-
def test_update_gridpool_order_with_invalid_params( # pylint: disable=too-many-arguments
453+
async def test_update_gridpool_order_with_invalid_params( # pylint: disable=too-many-arguments
470454
set_up: SetupParams,
471455
price: Price,
472456
quantity: Power,
@@ -475,12 +459,10 @@ def test_update_gridpool_order_with_invalid_params( # pylint: disable=too-many-
475459
) -> None:
476460
"""Test updating an order with invalid input parameters."""
477461
with pytest.raises(expected_exception):
478-
set_up.loop.run_until_complete(
479-
set_up.client.update_gridpool_order(
480-
gridpool_id=set_up.gridpool_id,
481-
order_id=1,
482-
price=price,
483-
quantity=quantity,
484-
valid_until=valid_until,
485-
)
462+
await set_up.client.update_gridpool_order(
463+
gridpool_id=set_up.gridpool_id,
464+
order_id=1,
465+
price=price,
466+
quantity=quantity,
467+
valid_until=valid_until,
486468
)

0 commit comments

Comments
 (0)