Skip to content

Commit

Permalink
fix multiple tests warnings and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume De Saint Martin committed Jun 7, 2020
1 parent 03cb356 commit 5f2c6b3
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 128 deletions.
15 changes: 8 additions & 7 deletions octobot_trading/producers/ohlcv_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ async def start(self):
"""
if not self.is_initialized:
await self._initialize()
if self.channel.is_paused:
await self.pause()
else:
self.tasks = [
asyncio.create_task(self._candle_callback(time_frame, pair))
for time_frame in self.channel.exchange_manager.exchange_config.traded_time_frames
for pair in self.channel.exchange_manager.exchange_config.traded_symbol_pairs]
if self.channel is not None:
if self.channel.is_paused:
await self.pause()
else:
self.tasks = [
asyncio.create_task(self._candle_callback(time_frame, pair))
for time_frame in self.channel.exchange_manager.exchange_config.traded_time_frames
for pair in self.channel.exchange_manager.exchange_config.traded_symbol_pairs]

def _get_traded_pairs(self):
return self.channel.exchange_manager.exchange_config.traded_symbol_pairs
Expand Down
10 changes: 6 additions & 4 deletions octobot_trading/producers/recent_trade_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ async def start(self):
elif refresh_threshold is RestExchangePairsRefreshMaxThresholds.SLOW:
self.refresh_time = 15
await self.init_recent_trades()
if self.channel.is_paused:
await self.pause()
else:
await self.start_update_loop()
# check if channel is not None to avoid attribute error if channel has been concurrently closed
if self.channel is not None:
if self.channel.is_paused:
await self.pause()
else:
await self.start_update_loop()

async def start_update_loop(self):
while not self.should_stop and not self.channel.is_paused:
Expand Down
9 changes: 1 addition & 8 deletions tests/data/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import time

import pytest
from octobot_trading.constants import SIMULATOR_LAST_PRICES_TO_CHECK

from octobot_trading.enums import TradeOrderSide, OrderStatus, TraderOrderType

from octobot_trading.data.order import Order

from tests.exchanges import exchange_manager
from tests.traders import trader_simulator
from tests.traders import trader

# All test coroutines will be treated as marked.
from tests.util.random_numbers import random_price

# All test coroutines will be treated as marked.
pytestmark = pytest.mark.asyncio


Expand Down
22 changes: 10 additions & 12 deletions tests/data_manager/test_price_events_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import os
from asyncio import Event

import pytest
from mock import AsyncMock, patch
from asyncio import Event
from mock import patch, Mock

from tests.data_manager import price_events_manager
from tests import event_loop

from tests.util.random_numbers import random_recent_trade, random_price, random_timestamp

# All test coroutines will be treated as marked.
Expand All @@ -48,7 +46,7 @@ async def test_handle_recent_trades(price_events_manager):
random_price_1 = random_price()
random_timestamp_1 = random_timestamp(max_value=1000)
price_event_1 = price_events_manager.add_event(random_price_1, random_timestamp_1, True)
with patch.object(price_event_1, 'set', new=AsyncMock()) as price_event_1_set:
with patch.object(price_event_1, 'set', new=Mock()) as price_event_1_set:
price_events_manager.handle_recent_trades([])
with pytest.raises(AssertionError):
price_event_1_set.assert_called_once()
Expand Down Expand Up @@ -77,8 +75,8 @@ async def test_handle_recent_trades_multiple_events(price_events_manager):
random_timestamp_2 = random_timestamp(min_value=random_timestamp_1 + 2, max_value=5000)
price_event_1 = price_events_manager.add_event(random_price_1, random_timestamp_1, True)
price_event_2 = price_events_manager.add_event(random_price_2, random_timestamp_2, True)
with patch.object(price_event_1, 'set', new=AsyncMock()) as price_event_1_set, \
patch.object(price_event_2, 'set', new=AsyncMock()) as price_event_2_set:
with patch.object(price_event_1, 'set', new=Mock()) as price_event_1_set, \
patch.object(price_event_2, 'set', new=Mock()) as price_event_2_set:
price_events_manager.handle_recent_trades(
[random_recent_trade(price=random_price(max_value=random_price_1 - 1)),
random_recent_trade(price=random_price(max_value=random_price_1 - 1)),
Expand All @@ -104,8 +102,8 @@ async def test_handle_recent_trades_multiple_events(price_events_manager):

price_event_1 = price_events_manager.add_event(random_price_1, random_timestamp_1, True)
price_event_2 = price_events_manager.add_event(random_price_2, random_timestamp_2, True)
with patch.object(price_event_1, 'set', new=AsyncMock()) as price_event_1_set, \
patch.object(price_event_2, 'set', new=AsyncMock()) as price_event_2_set:
with patch.object(price_event_1, 'set', new=Mock()) as price_event_1_set, \
patch.object(price_event_2, 'set', new=Mock()) as price_event_2_set:
price_events_manager.handle_recent_trades(
[random_recent_trade(price=random_price(max_value=random_price_1 - 1),
timestamp=random_timestamp(max_value=random_timestamp_1 - 1)),
Expand All @@ -116,8 +114,8 @@ async def test_handle_recent_trades_multiple_events(price_events_manager):

price_event_1 = price_events_manager.add_event(random_price_1, random_timestamp_1, True)
price_event_2 = price_events_manager.add_event(random_price_2, random_timestamp_2, True)
with patch.object(price_event_1, 'set', new=AsyncMock()) as price_event_1_set, \
patch.object(price_event_2, 'set', new=AsyncMock()) as price_event_2_set:
with patch.object(price_event_1, 'set', new=Mock()) as price_event_1_set, \
patch.object(price_event_2, 'set', new=Mock()) as price_event_2_set:
price_events_manager.handle_recent_trades(
[random_recent_trade(price=random_price(min_value=random_price_1, max_value=random_price_2 - 1),
timestamp=random_timestamp(min_value=random_timestamp_1 - 1)),
Expand All @@ -132,7 +130,7 @@ async def test_handle_price(price_events_manager):
random_price_1 = random_price()
random_timestamp_1 = random_timestamp(max_value=1000)
price_event_1 = price_events_manager.add_event(random_price_1, random_timestamp_1, True)
with patch.object(price_event_1, 'set', new=AsyncMock()) as price_event_1_set:
with patch.object(price_event_1, 'set', new=Mock()) as price_event_1_set:
price_events_manager.handle_price(0, random_timestamp())
with pytest.raises(AssertionError):
price_event_1_set.assert_called_once()
Expand Down
5 changes: 5 additions & 0 deletions tests/exchanges/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from octobot_backtesting.backtesting import Backtesting
from octobot_backtesting.constants import CONFIG_BACKTESTING
from octobot_backtesting.data_manager.time_manager import TimeManager
from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_commons.constants import CONFIG_ENABLED_OPTION
from octobot_commons.enums import TimeFrames

Expand Down Expand Up @@ -48,6 +49,8 @@ async def exchange_manager(request):
await exchange_manager_instance.initialize()
yield exchange_manager_instance
await exchange_manager_instance.stop()
# let updaters gracefully shutdown
await wait_asyncio_next_cycle()


@pytest.yield_fixture
Expand Down Expand Up @@ -101,6 +104,8 @@ async def simulated_exchange_manager(request):
await exchange_manager_instance.initialize()
yield exchange_manager_instance
await exchange_manager_instance.stop()
# let updaters gracefully shutdown
await wait_asyncio_next_cycle()


@pytest.fixture
Expand Down
9 changes: 9 additions & 0 deletions tests/exchanges/test_exchanges.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from mock import patch
from datetime import datetime

from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_commons.constants import MSECONDS_TO_MINUTE
from octobot_commons.enums import TimeFrames, TimeFramesMinutes
from octobot_commons.tests.test_config import load_test_config
Expand Down Expand Up @@ -61,6 +62,8 @@ async def test_add_exchange(self):
await exchange_manager_binance.stop()
await exchange_manager_bitmex.stop()
await exchange_manager_poloniex.stop()
# let updaters gracefully shutdown
await wait_asyncio_next_cycle()

async def test_get_exchange(self):
config = await self.init_default()
Expand All @@ -87,6 +90,8 @@ async def test_get_exchange(self):
await exchange_manager_binance.stop()
await exchange_manager_bitmex.stop()
await exchange_manager_poloniex.stop()
# let updaters gracefully shutdown
await wait_asyncio_next_cycle()

async def test_del_exchange(self):
config = await self.init_default()
Expand Down Expand Up @@ -116,6 +121,8 @@ async def test_del_exchange(self):
await exchange_manager_binance.stop()
await exchange_manager_bitmex.stop()
await exchange_manager_poloniex.stop()
# let updaters gracefully shutdown
await wait_asyncio_next_cycle()

async def test_get_all_exchanges(self):
config = await self.init_default()
Expand All @@ -140,6 +147,8 @@ async def test_get_all_exchanges(self):
await exchange_manager_binance.stop()
await exchange_manager_bitmex.stop()
await exchange_manager_poloniex.stop()
# let updaters gracefully shutdown
await wait_asyncio_next_cycle()

async def test_ms_timestamp_operations(self):
config = await self.init_default()
Expand Down
5 changes: 1 addition & 4 deletions tests/orders/types/limit/test_buy_limit_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import asyncio

import pytest

from octobot_trading.enums import TradeOrderType
from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from tests import event_loop
from tests.exchanges import simulated_trader, simulated_exchange_manager
from tests.orders import buy_limit_order

from tests.util.random_numbers import random_price, random_quantity, random_recent_trade, random_timestamp
from tests.util.random_numbers import random_price, random_quantity, random_recent_trade

pytestmark = pytest.mark.asyncio

Expand Down
8 changes: 2 additions & 6 deletions tests/orders/types/limit/test_sell_limit_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.

import asyncio

import pytest

from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_trading.enums import TradeOrderType, OrderStatus
from octobot_trading.enums import TradeOrderType
from tests import event_loop
from tests.exchanges import simulated_trader, simulated_exchange_manager
from tests.orders import sell_limit_order

from tests.util.random_numbers import random_price, random_quantity, random_recent_trade, random_timestamp
from tests.util.random_numbers import random_price, random_quantity, random_recent_trade

pytestmark = pytest.mark.asyncio

Expand Down
5 changes: 1 addition & 4 deletions tests/orders/types/limit/test_stop_loss_limit_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.


import pytest

from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_trading.enums import TradeOrderType
from tests import event_loop
from tests.exchanges import simulated_trader, simulated_exchange_manager
from tests.orders import stop_loss_limit_order

from tests.util.random_numbers import random_price, random_quantity, random_recent_trade, random_timestamp
from tests.util.random_numbers import random_price, random_quantity, random_recent_trade

pytestmark = pytest.mark.asyncio

Expand Down
9 changes: 2 additions & 7 deletions tests/orders/types/limit/test_take_profit_limit_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,14 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.


import asyncio

import pytest

from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_trading.enums import TradeOrderType, OrderStatus
from octobot_trading.enums import TradeOrderType
from tests import event_loop
from tests.exchanges import simulated_trader, simulated_exchange_manager
from tests.orders import take_profit_limit_order

from tests.util.random_numbers import random_price, random_quantity, random_recent_trade, random_timestamp
from tests.util.random_numbers import random_price, random_quantity, random_recent_trade

pytestmark = pytest.mark.asyncio

Expand Down
7 changes: 2 additions & 5 deletions tests/orders/types/market/test_buy_market_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import asyncio

import pytest

from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_trading.enums import TradeOrderType, OrderStatus
from octobot_trading.enums import TradeOrderType
from tests import event_loop
from tests.exchanges import simulated_trader, simulated_exchange_manager
from tests.orders import buy_market_order

from tests.util.random_numbers import random_price, random_quantity, random_recent_trade, random_timestamp
from tests.util.random_numbers import random_price, random_quantity, random_recent_trade

pytestmark = pytest.mark.asyncio

Expand Down
8 changes: 2 additions & 6 deletions tests/orders/types/market/test_sell_market_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,14 @@
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.

import asyncio

import pytest

from octobot_commons.asyncio_tools import wait_asyncio_next_cycle
from octobot_trading.enums import TradeOrderType, OrderStatus
from octobot_trading.enums import TradeOrderType
from tests import event_loop
from tests.exchanges import simulated_trader, simulated_exchange_manager
from tests.orders import sell_market_order

from tests.util.random_numbers import random_price, random_quantity, random_recent_trade, random_timestamp
from tests.util.random_numbers import random_price, random_quantity, random_recent_trade

pytestmark = pytest.mark.asyncio

Expand Down
3 changes: 1 addition & 2 deletions tests/traders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import pytest

from octobot_commons.constants import CONFIG_ENABLED_OPTION
from octobot_commons.tests.test_config import load_test_config
from octobot_trading.constants import CONFIG_SIMULATOR, CONFIG_TRADER

from octobot_trading.traders.trader_simulator import TraderSimulator

from octobot_trading.traders.trader import Trader
from tests.exchanges import exchange_manager

Expand Down

0 comments on commit 5f2c6b3

Please sign in to comment.