Skip to content

Commit f20a69c

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 d60e406 commit f20a69c

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
@@ -42,7 +42,6 @@ class SetupParams: # pylint: disable=too-many-instance-attributes
4242

4343
client: Client
4444
mock_stub: AsyncMock
45-
loop: asyncio.AbstractEventLoop
4645
gridpool_id: int
4746
delivery_area: DeliveryArea
4847
delivery_period: DeliveryPeriod
@@ -62,10 +61,6 @@ def set_up() -> Generator[Any, Any, Any]:
6261
mock_stub = AsyncMock()
6362
client._stub = mock_stub # pylint: disable=protected-access
6463

65-
# Create a new event loop for each test
66-
loop = asyncio.new_event_loop()
67-
asyncio.set_event_loop(loop)
68-
6964
# Set up the parameters for the orders
7065
# Setting delivery start to the next day 12:00
7166
delivery_start = (datetime.now(timezone.utc) + timedelta(days=1)).replace(
@@ -87,7 +82,6 @@ def set_up() -> Generator[Any, Any, Any]:
8782
yield SetupParams(
8883
client=client,
8984
mock_stub=mock_stub,
90-
loop=loop,
9185
gridpool_id=gridpool_id,
9286
delivery_area=delivery_area,
9387
delivery_period=delivery_period,
@@ -99,8 +93,6 @@ def set_up() -> Generator[Any, Any, Any]:
9993
valid_until=valid_until,
10094
)
10195

102-
loop.close()
103-
10496

10597
# pylint: disable=redefined-outer-name
10698
def set_up_order_detail_response(
@@ -189,7 +181,7 @@ async def test_receive_public_trades(
189181
]
190182

191183

192-
def test_create_gridpool_order(
184+
async def test_create_gridpool_order(
193185
set_up: SetupParams,
194186
) -> None:
195187
"""
@@ -205,17 +197,15 @@ def test_create_gridpool_order(
205197
)
206198
set_up.mock_stub.CreateGridpoolOrder.return_value = mock_response
207199

208-
set_up.loop.run_until_complete(
209-
set_up.client.create_gridpool_order(
210-
gridpool_id=set_up.gridpool_id,
211-
delivery_area=set_up.delivery_area,
212-
delivery_period=set_up.delivery_period,
213-
order_type=set_up.order_type,
214-
side=set_up.side,
215-
price=set_up.price,
216-
quantity=set_up.quantity,
217-
execution_option=set_up.order_execution_option, # optional field
218-
)
200+
await set_up.client.create_gridpool_order(
201+
gridpool_id=set_up.gridpool_id,
202+
delivery_area=set_up.delivery_area,
203+
delivery_period=set_up.delivery_period,
204+
order_type=set_up.order_type,
205+
side=set_up.side,
206+
price=set_up.price,
207+
quantity=set_up.quantity,
208+
execution_option=set_up.order_execution_option, # optional field
219209
)
220210

221211
set_up.mock_stub.CreateGridpoolOrder.assert_called_once()
@@ -229,7 +219,7 @@ def test_create_gridpool_order(
229219
assert args[0].order.execution_option == set_up.order_execution_option.to_pb()
230220

231221

232-
def test_update_gridpool_order(
222+
async def test_update_gridpool_order(
233223
set_up: SetupParams,
234224
) -> None:
235225
"""Test the method updating a gridpool order."""
@@ -241,13 +231,11 @@ def test_update_gridpool_order(
241231
)
242232
set_up.mock_stub.UpdateGridpoolOrder.return_value = mock_response
243233

244-
set_up.loop.run_until_complete(
245-
set_up.client.update_gridpool_order(
246-
gridpool_id=set_up.gridpool_id,
247-
order_id=1,
248-
quantity=set_up.quantity,
249-
valid_until=set_up.valid_until,
250-
)
234+
await set_up.client.update_gridpool_order(
235+
gridpool_id=set_up.gridpool_id,
236+
order_id=1,
237+
quantity=set_up.quantity,
238+
valid_until=set_up.valid_until,
251239
)
252240

253241
valid_until_pb = timestamp_pb2.Timestamp()
@@ -263,7 +251,7 @@ def test_update_gridpool_order(
263251
), "Price field should not be set."
264252

265253

266-
def test_cancel_gridpool_order(
254+
async def test_cancel_gridpool_order(
267255
set_up: SetupParams,
268256
) -> None:
269257
"""Test the method cancelling gridpool orders."""
@@ -279,10 +267,8 @@ def test_cancel_gridpool_order(
279267

280268
set_up.mock_stub.CancelGridpoolOrder.return_value = mock_response
281269

282-
set_up.loop.run_until_complete(
283-
set_up.client.cancel_gridpool_order(
284-
gridpool_id=set_up.gridpool_id, order_id=order_id
285-
)
270+
await set_up.client.cancel_gridpool_order(
271+
gridpool_id=set_up.gridpool_id, order_id=order_id
286272
)
287273

288274
set_up.mock_stub.CancelGridpoolOrder.assert_called_once()
@@ -389,7 +375,7 @@ async def test_list_gridpool_orders(
389375
),
390376
],
391377
)
392-
def test_create_gridpool_order_with_invalid_params(
378+
async def test_create_gridpool_order_with_invalid_params(
393379
# pylint: disable=too-many-arguments, too-many-positional-arguments
394380
set_up: SetupParams,
395381
price: Price,
@@ -401,18 +387,16 @@ def test_create_gridpool_order_with_invalid_params(
401387
) -> None:
402388
"""Test creating an order with invalid input parameters."""
403389
with pytest.raises(expected_exception):
404-
set_up.loop.run_until_complete(
405-
set_up.client.create_gridpool_order(
406-
gridpool_id=set_up.gridpool_id,
407-
delivery_area=set_up.delivery_area,
408-
delivery_period=delivery_period,
409-
order_type=OrderType.LIMIT,
410-
side=MarketSide.BUY,
411-
price=price,
412-
quantity=quantity,
413-
execution_option=execution_option,
414-
valid_until=valid_until,
415-
)
390+
await set_up.client.create_gridpool_order(
391+
gridpool_id=set_up.gridpool_id,
392+
delivery_area=set_up.delivery_area,
393+
delivery_period=delivery_period,
394+
order_type=OrderType.LIMIT,
395+
side=MarketSide.BUY,
396+
price=price,
397+
quantity=quantity,
398+
execution_option=execution_option,
399+
valid_until=valid_until,
416400
)
417401

418402

@@ -442,7 +426,7 @@ def test_create_gridpool_order_with_invalid_params(
442426
),
443427
],
444428
)
445-
def test_update_gridpool_order_with_invalid_params( # pylint: disable=too-many-arguments
429+
async def test_update_gridpool_order_with_invalid_params( # pylint: disable=too-many-arguments
446430
set_up: SetupParams,
447431
price: Price,
448432
quantity: Power,
@@ -451,12 +435,10 @@ def test_update_gridpool_order_with_invalid_params( # pylint: disable=too-many-
451435
) -> None:
452436
"""Test updating an order with invalid input parameters."""
453437
with pytest.raises(expected_exception):
454-
set_up.loop.run_until_complete(
455-
set_up.client.update_gridpool_order(
456-
gridpool_id=set_up.gridpool_id,
457-
order_id=1,
458-
price=price,
459-
quantity=quantity,
460-
valid_until=valid_until,
461-
)
438+
await set_up.client.update_gridpool_order(
439+
gridpool_id=set_up.gridpool_id,
440+
order_id=1,
441+
price=price,
442+
quantity=quantity,
443+
valid_until=valid_until,
462444
)

0 commit comments

Comments
 (0)