def test_get_order_book_key_error(mocker): parser = parsers.Parser() parser.exchange = mocker.Mock() order_book_link_maker_mock = mocker.Mock() order_book_link_maker_mock.return_value = 'test request' parser.order_book_link_maker = order_book_link_maker_mock response_mock = mocker.Mock() response_mock.text = 'test text response' get_mock = mocker.patch('parsers.requests.get') get_mock.return_value = response_mock datetime = mocker.patch('parsers.datetime') datetime.now.return_value = 'NOW' error_handler_mock = mocker.Mock() parser.error_handler = error_handler_mock mocker.patch('parsers.Parser.parse_order_book', side_effect=KeyError) with patch('builtins.open', mock_open()): assert parser.get_order_book(Pair('TST', 'TST')) is None error_handler_mock.assert_called_with( 'test request', get_mock(), 'NOW - Request: test request\nResponse: test text response\n' )
def test_exchanges_orders_combiner(mocker): exchanges_order_book_1 = Mock() exchanges_order_book_1.exchange = 'test first exchange' exchanges_order_book_1.order_book.asks = 'test first order_book asks' exchanges_order_book_1.order_book.bids = 'test first order_book bids' exchanges_order_book_2 = Mock() exchanges_order_book_2.exchange = 'test second exchange' exchanges_order_book_2.order_book.asks = 'test second order_book asks' exchanges_order_book_2.order_book.bids = 'test second order_book bids' exchanges_order_books = [exchanges_order_book_1, exchanges_order_book_2] collect_deals_mock = mocker.patch('watcher.collect_deals') pair = Pair('TST', 'TST') with patch('builtins.open', mock_open()) as mock_file: watcher.exchanges_orders_combiner(exchanges_order_books, pair) collect_deals_mock.assert_has_calls([ call(deals=[], sell_exchange='test first exchange', buy_exchange='test second exchange', sell_orders='test first order_book asks', buy_orders='test second order_book bids', pair=pair), call(deals=[], sell_exchange='test second exchange', buy_exchange='test first exchange', sell_orders='test second order_book asks', buy_orders='test first order_book bids', pair=pair), ])
def test_EXMOParser_parse_order_book(mocker): response = mocker.Mock() response.json.return_value = { 'TST2_TST1': { 'ask': 'test asks', 'bid': 'test bids', } } assert parsers.EXMOParser().parse_order_book(response, Pair('TST1', 'TST2')) == \ parsers.OrderBook(asks='test asks', bids='test bids')
def test_calculate_gain(mocker): sell_exchange = mocker.Mock() sell_exchange.fiat_fee = {'TST': Fee(0.0001, 0.0002, 'test')} sell_exchange.coin_fee = {'TST': Fee(0.0003, 0.0004, 'test')} sell_exchange.trade_fee = 0.001 buy_exchange = mocker.Mock() buy_exchange.fiat_fee = {'TST': Fee(0.0005, 0.0006, 'test')} buy_exchange.coin_fee = {'TST': Fee(0.0007, 0.0008, 'test')} buy_exchange.trade_fee = 0.002 sell_order = Order(10, 0.1) buy_order = Order(12, 0.2) pair = Pair('TST', 'TST') assert revenue.calculate_gain(sell_exchange, buy_exchange, sell_order, buy_order, pair) == 0.18338
def test_get_order_book(mocker): parser = parsers.Parser() exchange = mocker.Mock() exchange.name = 'test exchange name' parser.exchange = exchange order_book_link_maker_mock = mocker.Mock() order_book_link_maker_mock.return_value = 'test request' parser.order_book_link_maker = order_book_link_maker_mock response_mock = mocker.Mock() response_mock.text = 'test text response' get_mock = mocker.patch('parsers.requests.get') get_mock.return_value = response_mock datetime = mocker.patch('parsers.datetime') datetime.now.return_value = 'NOW' parse_order_book = mocker.patch('parsers.Parser.parse_order_book') parse_order_book.return_value = 'test order_book' error_handler_mock = mocker.Mock() parser.error_handler = error_handler_mock test_pair = Pair('TST', 'TST') with patch('builtins.open', mock_open()) as mock_file: order_book = parser.get_order_book(test_pair) assert order_book == parsers.ExchangeOrderBook( exchange=exchange, order_book='test order_book', pair=test_pair ) mock_file().write.assert_called_once_with( 'NOW - Request: test request\nResponse: test text response\n') parse_order_book.assert_called_with(response_mock, test_pair) order_book_link_maker_mock.assert_called_with(test_pair) get_mock.assert_called_with('test request') error_handler_mock.assert_not_called()
def test_run(mocker): parser_to_run_1 = Mock() parser_to_run_1.return_value.get_order_book.return_value = 'test order_book 1' parser_to_run_2 = Mock() parser_to_run_2.return_value.get_order_book.return_value = None watcher.PARSERS_TO_RUN = [parser_to_run_1, parser_to_run_2] test_pair = Pair('TST', 'TST') watcher.PAIRS = [test_pair] mocker.patch('watcher.sleep') exchanges_orders_combiner_mock = mocker.patch( 'watcher.exchanges_orders_combiner') watcher.run() exchanges_orders_combiner_mock.assert_called_with(['test order_book 1'], test_pair) parser_to_run_1.return_value.get_order_book.assert_called_with(test_pair) parser_to_run_2.return_value.get_order_book.assert_called_with(test_pair)
def test_order_book_link_maker(mocker): parsers.Parser.exchange = mocker.Mock() parsers.Parser.exchange.order_book_link = 'coin={coin}, fiat={fiat}' assert parsers.Parser().order_book_link_maker(Pair('FAT', 'CIN')) == 'coin=CIN, fiat=FAT'
from watcher.log_helpers import setup_logger from itertools import permutations from time import sleep from watcher.parsers import EXMOParser, CEXParser from watcher.lib import Pair from watcher.revenue import collect_deals PARSERS_TO_RUN = [EXMOParser, CEXParser] PAIRS = [ Pair('USD', 'BTC'), Pair('USD', 'ETH'), # Pair('USD', 'ZEC'), ] def exchanges_orders_combiner(exchanges_order_books, pair, logger): """ Combine exchanges orders straight and vice versa. [(exch_1:sell, exch_2:buy), (exch_2:sell, exch_1:buy)] """ for first_exchange, second_exchange in permutations( exchanges_order_books, 2): collect_deals(sell_exchange=first_exchange.exchange, buy_exchange=second_exchange.exchange, sell_orders=first_exchange.order_book.asks, buy_orders=second_exchange.order_book.bids, pair=pair, logger=logger)