2 import market
, store
, portfolio
5 @unittest.skipUnless("unit" in limits
, "Unit skipped")
6 class MarketTest(WebMockTestCase
):
10 self
.ccxt
= mock
.Mock(spec
=market
.ccxt
.poloniexE
)
12 def test_values(self
):
13 m
= market
.Market(self
.ccxt
, self
.market_args())
15 self
.assertEqual(self
.ccxt
, m
.ccxt
)
16 self
.assertFalse(m
.debug
)
17 self
.assertIsInstance(m
.report
, market
.ReportStore
)
18 self
.assertIsInstance(m
.trades
, market
.TradeStore
)
19 self
.assertIsInstance(m
.balances
, market
.BalanceStore
)
20 self
.assertEqual(m
, m
.report
.market
)
21 self
.assertEqual(m
, m
.trades
.market
)
22 self
.assertEqual(m
, m
.balances
.market
)
23 self
.assertEqual(m
, m
.ccxt
._market
)
25 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=True))
26 self
.assertTrue(m
.debug
)
28 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=False))
29 self
.assertFalse(m
.debug
)
31 with mock
.patch("market.ReportStore") as report_store
:
32 with self
.subTest(quiet
=False):
33 m
= market
.Market(self
.ccxt
, self
.market_args(quiet
=False))
34 report_store
.assert_called_with(m
, verbose_print
=True)
35 report_store().log_market
.assert_called_once()
36 report_store
.reset_mock()
37 with self
.subTest(quiet
=True):
38 m
= market
.Market(self
.ccxt
, self
.market_args(quiet
=True))
39 report_store
.assert_called_with(m
, verbose_print
=False)
40 report_store().log_market
.assert_called_once()
42 @mock.patch("market.ccxt")
43 def test_from_config(self
, ccxt
):
44 with mock
.patch("market.ReportStore"):
45 ccxt
.poloniexE
.return_value
= self
.ccxt
47 m
= market
.Market
.from_config({"key": "key", "secred": "secret"}
, self
.market_args())
49 self
.assertEqual(self
.ccxt
, m
.ccxt
)
51 m
= market
.Market
.from_config({"key": "key", "secred": "secret"}
, self
.market_args(debug
=True))
52 self
.assertEqual(True, m
.debug
)
54 def test_get_tickers(self
):
55 self
.ccxt
.fetch_tickers
.side_effect
= [
60 m
= market
.Market(self
.ccxt
, self
.market_args())
61 self
.assertEqual("tickers", m
.get_tickers())
62 self
.assertEqual("tickers", m
.get_tickers())
63 self
.ccxt
.fetch_tickers
.assert_called_once()
65 self
.assertIsNone(m
.get_tickers(refresh
=self
.time
.time()))
67 def test_get_ticker(self
):
68 with self
.subTest(get_tickers
=True):
69 self
.ccxt
.fetch_tickers
.return_value
= {
70 "ETH/ETC": { "bid": 1, "ask": 3 }
,
71 "XVG/ETH": { "bid": 10, "ask": 40 }
,
73 m
= market
.Market(self
.ccxt
, self
.market_args())
75 ticker
= m
.get_ticker("ETH", "ETC")
76 self
.assertEqual(1, ticker
["bid"])
77 self
.assertEqual(3, ticker
["ask"])
78 self
.assertEqual(2, ticker
["average"])
79 self
.assertFalse(ticker
["inverted"])
81 ticker
= m
.get_ticker("ETH", "XVG")
82 self
.assertEqual(0.0625, ticker
["average"])
83 self
.assertTrue(ticker
["inverted"])
84 self
.assertIn("original", ticker
)
85 self
.assertEqual(10, ticker
["original"]["bid"])
86 self
.assertEqual(25, ticker
["original"]["average"])
88 ticker
= m
.get_ticker("XVG", "XMR")
89 self
.assertIsNone(ticker
)
91 with self
.subTest(get_tickers
=False):
92 self
.ccxt
.fetch_tickers
.return_value
= None
93 self
.ccxt
.fetch_ticker
.side_effect
= [
94 { "bid": 1, "ask": 3 }
,
95 market
.ExchangeError("foo"),
96 { "bid": 10, "ask": 40 }
,
97 market
.ExchangeError("foo"),
98 market
.ExchangeError("foo"),
101 m
= market
.Market(self
.ccxt
, self
.market_args())
103 ticker
= m
.get_ticker("ETH", "ETC")
104 self
.ccxt
.fetch_ticker
.assert_called_with("ETH/ETC")
105 self
.assertEqual(1, ticker
["bid"])
106 self
.assertEqual(3, ticker
["ask"])
107 self
.assertEqual(2, ticker
["average"])
108 self
.assertFalse(ticker
["inverted"])
110 ticker
= m
.get_ticker("ETH", "XVG")
111 self
.assertEqual(0.0625, ticker
["average"])
112 self
.assertTrue(ticker
["inverted"])
113 self
.assertIn("original", ticker
)
114 self
.assertEqual(10, ticker
["original"]["bid"])
115 self
.assertEqual(25, ticker
["original"]["average"])
117 ticker
= m
.get_ticker("XVG", "XMR")
118 self
.assertIsNone(ticker
)
120 def test_fetch_fees(self
):
121 m
= market
.Market(self
.ccxt
, self
.market_args())
122 self
.ccxt
.fetch_fees
.return_value
= "Foo"
123 self
.assertEqual("Foo", m
.fetch_fees())
124 self
.ccxt
.fetch_fees
.assert_called_once()
125 self
.ccxt
.reset_mock()
126 self
.assertEqual("Foo", m
.fetch_fees())
127 self
.ccxt
.fetch_fees
.assert_not_called()
129 @mock.patch.object(market
.Portfolio
, "repartition")
130 @mock.patch.object(market
.Market
, "get_ticker")
131 @mock.patch.object(market
.TradeStore
, "compute_trades")
132 def test_prepare_trades(self
, compute_trades
, get_ticker
, repartition
):
133 repartition
.return_value
= {
134 "XEM": (D("0.75"), "long"),
135 "BTC": (D("0.25"), "long"),
137 def _get_ticker(c1
, c2
):
138 if c1
== "USDT" and c2
== "BTC":
139 return { "average": D("0.0001") }
140 if c1
== "XVG" and c2
== "BTC":
141 return { "average": D("0.000001") }
142 self
.fail("Should be called with {}, {}".format(c1
, c2
))
143 get_ticker
.side_effect
= _get_ticker
145 with mock
.patch("market.ReportStore"):
146 m
= market
.Market(self
.ccxt
, self
.market_args())
147 self
.ccxt
.fetch_all_balances
.return_value
= {
149 "exchange_free": D("10000.0"),
150 "exchange_used": D("0.0"),
151 "exchange_total": D("10000.0"),
152 "total": D("10000.0")
155 "exchange_free": D("10000.0"),
156 "exchange_used": D("0.0"),
157 "exchange_total": D("10000.0"),
158 "total": D("10000.0")
162 m
.balances
.fetch_balances(tag
="tag")
165 compute_trades
.assert_called()
167 call
= compute_trades
.call_args
168 self
.assertEqual(1, call
[0][0]["USDT"].value
)
169 self
.assertEqual(D("0.01"), call
[0][0]["XVG"].value
)
170 self
.assertEqual(D("0.2525"), call
[0][1]["BTC"].value
)
171 self
.assertEqual(D("0.7575"), call
[0][1]["XEM"].value
)
172 m
.report
.log_stage
.assert_called_once_with("prepare_trades",
173 base_currency
='BTC', compute_value
='average',
174 liquidity
='medium', only
=None, repartition
=None)
175 m
.report
.log_balances
.assert_called_once_with(tag
="tag")
178 @mock.patch.object(market
.time
, "sleep")
179 @mock.patch.object(market
.TradeStore
, "all_orders")
180 def test_follow_orders(self
, all_orders
, time_mock
):
181 for debug
, sleep
in [
182 (False, None), (True, None),
183 (False, 12), (True, 12)]:
184 with self
.subTest(sleep
=sleep
, debug
=debug
), \
185 mock
.patch("market.ReportStore"):
186 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=debug
))
188 order_mock1
= mock
.Mock()
189 order_mock2
= mock
.Mock()
190 order_mock3
= mock
.Mock()
191 all_orders
.side_effect
= [
192 [order_mock1
, order_mock2
],
193 [order_mock1
, order_mock2
],
195 [order_mock1
, order_mock3
],
196 [order_mock1
, order_mock3
],
198 [order_mock1
, order_mock3
],
199 [order_mock1
, order_mock3
],
204 order_mock1
.get_status
.side_effect
= ["open", "open", "closed"]
205 order_mock2
.get_status
.side_effect
= ["open"]
206 order_mock3
.get_status
.side_effect
= ["open", "closed"]
208 order_mock1
.trade
= mock
.Mock()
209 order_mock2
.trade
= mock
.Mock()
210 order_mock3
.trade
= mock
.Mock()
212 m
.follow_orders(sleep
=sleep
)
214 order_mock1
.trade
.update_order
.assert_any_call(order_mock1
, 1)
215 order_mock1
.trade
.update_order
.assert_any_call(order_mock1
, 2)
216 self
.assertEqual(2, order_mock1
.trade
.update_order
.call_count
)
217 self
.assertEqual(3, order_mock1
.get_status
.call_count
)
219 order_mock2
.trade
.update_order
.assert_any_call(order_mock2
, 1)
220 self
.assertEqual(1, order_mock2
.trade
.update_order
.call_count
)
221 self
.assertEqual(1, order_mock2
.get_status
.call_count
)
223 order_mock3
.trade
.update_order
.assert_any_call(order_mock3
, 2)
224 self
.assertEqual(1, order_mock3
.trade
.update_order
.call_count
)
225 self
.assertEqual(2, order_mock3
.get_status
.call_count
)
226 m
.report
.log_stage
.assert_called()
228 mock
.call("follow_orders_begin"),
229 mock
.call("follow_orders_tick_1"),
230 mock
.call("follow_orders_tick_2"),
231 mock
.call("follow_orders_tick_3"),
232 mock
.call("follow_orders_end"),
234 m
.report
.log_stage
.assert_has_calls(calls
)
235 m
.report
.log_orders
.assert_called()
236 self
.assertEqual(3, m
.report
.log_orders
.call_count
)
238 mock
.call([order_mock1
, order_mock2
], tick
=1),
239 mock
.call([order_mock1
, order_mock3
], tick
=2),
240 mock
.call([order_mock1
, order_mock3
], tick
=3),
242 m
.report
.log_orders
.assert_has_calls(calls
)
244 mock
.call(order_mock1
, 3, finished
=True),
245 mock
.call(order_mock3
, 3, finished
=True),
247 m
.report
.log_order
.assert_has_calls(calls
)
251 m
.report
.log_debug_action
.assert_called_with("Set follow_orders tick to 7s")
252 time_mock
.assert_called_with(7)
254 time_mock
.assert_called_with(30)
256 time_mock
.assert_called_with(sleep
)
258 with self
.subTest("disappearing order"), \
259 mock
.patch("market.ReportStore"):
260 all_orders
.reset_mock()
261 m
= market
.Market(self
.ccxt
, self
.market_args())
263 order_mock1
= mock
.Mock()
264 order_mock2
= mock
.Mock()
265 all_orders
.side_effect
= [
266 [order_mock1
, order_mock2
],
267 [order_mock1
, order_mock2
],
269 [order_mock1
, order_mock2
],
270 [order_mock1
, order_mock2
],
275 order_mock1
.get_status
.side_effect
= ["open", "closed"]
276 order_mock2
.get_status
.side_effect
= ["open", "error_disappeared"]
278 order_mock1
.trade
= mock
.Mock()
279 trade_mock
= mock
.Mock()
280 order_mock2
.trade
= trade_mock
282 trade_mock
.tick_actions_recreate
.return_value
= "tick1"
286 trade_mock
.tick_actions_recreate
.assert_called_once_with(2)
287 trade_mock
.prepare_order
.assert_called_once_with(compute_value
="tick1")
288 m
.report
.log_error
.assert_called_once_with("follow_orders", message
=mock
.ANY
)
290 @mock.patch.object(market
.BalanceStore
, "fetch_balances")
291 def test_move_balance(self
, fetch_balances
):
292 for debug
in [True, False]:
293 with self
.subTest(debug
=debug
),\
294 mock
.patch("market.ReportStore"):
295 m
= market
.Market(self
.ccxt
, self
.market_args(debug
=debug
))
297 value_from
= portfolio
.Amount("BTC", "1.0")
298 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
299 value_to
= portfolio
.Amount("BTC", "10.0")
300 trade1
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
302 value_from
= portfolio
.Amount("BTC", "0.0")
303 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
304 value_to
= portfolio
.Amount("BTC", "-3.0")
305 trade2
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
307 value_from
= portfolio
.Amount("USDT", "0.0")
308 value_from
.linked_to
= portfolio
.Amount("XVG", "0.0")
309 value_to
= portfolio
.Amount("USDT", "-50.0")
310 trade3
= portfolio
.Trade(value_from
, value_to
, "XVG", m
)
312 m
.trades
.all
= [trade1
, trade2
, trade3
]
313 balance1
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
314 balance2
= portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }
)
315 balance3
= portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }
)
316 m
.balances
.all
= {"BTC": balance1, "USDT": balance2, "ETC": balance3}
320 fetch_balances
.assert_called_with()
321 m
.report
.log_move_balances
.assert_called_once()
324 m
.report
.log_debug_action
.assert_called()
325 self
.assertEqual(3, m
.report
.log_debug_action
.call_count
)
327 self
.ccxt
.transfer_balance
.assert_any_call("BTC", 3, "exchange", "margin")
328 self
.ccxt
.transfer_balance
.assert_any_call("USDT", 100, "exchange", "margin")
329 self
.ccxt
.transfer_balance
.assert_any_call("ETC", 5, "margin", "exchange")
331 m
.report
.reset_mock()
332 fetch_balances
.reset_mock()
333 with self
.subTest(retry
=True):
334 with mock
.patch("market.ReportStore"):
335 m
= market
.Market(self
.ccxt
, self
.market_args())
337 value_from
= portfolio
.Amount("BTC", "0.0")
338 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
339 value_to
= portfolio
.Amount("BTC", "-3.0")
340 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
342 m
.trades
.all
= [trade
]
343 balance
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
344 m
.balances
.all
= {"BTC": balance}
346 m
.ccxt
.transfer_balance
.side_effect
= [
347 market
.ccxt
.RequestTimeout
,
348 market
.ccxt
.InvalidNonce
,
352 self
.ccxt
.transfer_balance
.assert_has_calls([
353 mock
.call("BTC", 3, "exchange", "margin"),
354 mock
.call("BTC", 3, "exchange", "margin"),
355 mock
.call("BTC", 3, "exchange", "margin")
357 self
.assertEqual(3, fetch_balances
.call_count
)
358 m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying", exception
=mock
.ANY
)
359 self
.assertEqual(3, m
.report
.log_move_balances
.call_count
)
361 self
.ccxt
.transfer_balance
.reset_mock()
362 m
.report
.reset_mock()
363 fetch_balances
.reset_mock()
364 with self
.subTest(retry
=True, too_much
=True):
365 with mock
.patch("market.ReportStore"):
366 m
= market
.Market(self
.ccxt
, self
.market_args())
368 value_from
= portfolio
.Amount("BTC", "0.0")
369 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
370 value_to
= portfolio
.Amount("BTC", "-3.0")
371 trade
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
373 m
.trades
.all
= [trade
]
374 balance
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
375 m
.balances
.all
= {"BTC": balance}
377 m
.ccxt
.transfer_balance
.side_effect
= [
378 market
.ccxt
.RequestTimeout
,
379 market
.ccxt
.RequestTimeout
,
380 market
.ccxt
.RequestTimeout
,
381 market
.ccxt
.RequestTimeout
,
382 market
.ccxt
.RequestTimeout
,
384 with self
.assertRaises(market
.ccxt
.RequestTimeout
):
387 self
.ccxt
.transfer_balance
.reset_mock()
388 m
.report
.reset_mock()
389 fetch_balances
.reset_mock()
390 with self
.subTest(retry
=True, partial_result
=True):
391 with mock
.patch("market.ReportStore"):
392 m
= market
.Market(self
.ccxt
, self
.market_args())
394 value_from
= portfolio
.Amount("BTC", "1.0")
395 value_from
.linked_to
= portfolio
.Amount("ETH", "10.0")
396 value_to
= portfolio
.Amount("BTC", "10.0")
397 trade1
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
399 value_from
= portfolio
.Amount("BTC", "0.0")
400 value_from
.linked_to
= portfolio
.Amount("ETH", "0.0")
401 value_to
= portfolio
.Amount("BTC", "-3.0")
402 trade2
= portfolio
.Trade(value_from
, value_to
, "ETH", m
)
404 value_from
= portfolio
.Amount("USDT", "0.0")
405 value_from
.linked_to
= portfolio
.Amount("XVG", "0.0")
406 value_to
= portfolio
.Amount("USDT", "-50.0")
407 trade3
= portfolio
.Trade(value_from
, value_to
, "XVG", m
)
409 m
.trades
.all
= [trade1
, trade2
, trade3
]
410 balance1
= portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }
)
411 balance2
= portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }
)
412 balance3
= portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }
)
413 m
.balances
.all
= {"BTC": balance1, "USDT": balance2, "ETC": balance3}
415 call_counts
= { "BTC": 0, "USDT": 0, "ETC": 0 }
416 def _transfer_balance(currency
, amount
, from_
, to_
):
417 call_counts
[currency
] += 1
418 if currency
== "BTC":
419 m
.balances
.all
["BTC"] = portfolio
.Balance("BTC", { "margin_in_position": "0", "margin_available": "3" }
)
420 if currency
== "USDT":
421 if call_counts
["USDT"] == 1:
422 raise market
.ccxt
.RequestTimeout
424 m
.balances
.all
["USDT"] = portfolio
.Balance("USDT", { "margin_in_position": "100", "margin_available": "150" }
)
425 if currency
== "ETC":
426 m
.balances
.all
["ETC"] = portfolio
.Balance("ETC", { "margin_in_position": "10", "margin_available": "10" }
)
429 m
.ccxt
.transfer_balance
.side_effect
= _transfer_balance
432 self
.ccxt
.transfer_balance
.assert_has_calls([
433 mock
.call("BTC", 3, "exchange", "margin"),
434 mock
.call('USDT', 100, 'exchange', 'margin'),
435 mock
.call('USDT', 100, 'exchange', 'margin'),
436 mock
.call("ETC", 5, "margin", "exchange")
438 self
.assertEqual(2, fetch_balances
.call_count
)
439 m
.report
.log_error
.assert_called_with(mock
.ANY
, message
="Retrying", exception
=mock
.ANY
)
440 self
.assertEqual(2, m
.report
.log_move_balances
.call_count
)
441 m
.report
.log_move_balances
.asser_has_calls([
444 'BTC': portfolio
.Amount("BTC", "3"),
445 'USDT': portfolio
.Amount("USDT", "150"),
446 'ETC': portfolio
.Amount("ETC", "10"),
449 'BTC': portfolio
.Amount("BTC", "3"),
450 'USDT': portfolio
.Amount("USDT", "100"),
454 'BTC': portfolio
.Amount("BTC", "3"),
455 'USDT': portfolio
.Amount("USDT", "150"),
456 'ETC': portfolio
.Amount("ETC", "10"),
459 'BTC': portfolio
.Amount("BTC", "0"),
460 'USDT': portfolio
.Amount("USDT", "100"),
461 'ETC': portfolio
.Amount("ETC", "-5"),
466 def test_store_file_report(self
):
467 file_open
= mock
.mock_open()
468 m
= market
.Market(self
.ccxt
,
469 self
.market_args(report_path
="present"), user_id
=1)
470 with self
.subTest(file="present"),\
471 mock
.patch("market.open", file_open
),\
472 mock
.patch
.object(m
, "report") as report
,\
473 mock
.patch
.object(market
, "datetime") as time_mock
:
475 report
.print_logs
= [[time_mock
.now(), "Foo"], [time_mock
.now(), "Bar"]]
476 report
.to_json
.return_value
= "json_content"
478 m
.store_file_report(datetime
.datetime(2018, 2, 25))
480 file_open
.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
481 file_open
.assert_any_call("present/2018-02-25T00:00:00_1.log", "w")
482 file_open().write
.assert_any_call("json_content")
483 file_open().write
.assert_any_call("Foo\nBar")
484 m
.report
.to_json
.assert_called_once_with()
486 m
= market
.Market(self
.ccxt
, self
.market_args(report_path
="error"), user_id
=1)
487 with self
.subTest(file="error"),\
488 mock
.patch("market.open") as file_open
,\
489 mock
.patch
.object(m
, "report") as report
,\
490 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
491 file_open
.side_effect
= FileNotFoundError
493 m
.store_file_report(datetime
.datetime(2018, 2, 25))
495 self
.assertRegex(stdout_mock
.getvalue(), "impossible to store report file: FileNotFoundError;")
497 @mock.patch.object(market
, "psycopg2")
498 def test_store_database_report(self
, psycopg2
):
499 connect_mock
= mock
.Mock()
500 cursor_mock
= mock
.MagicMock()
502 connect_mock
.cursor
.return_value
= cursor_mock
503 psycopg2
.connect
.return_value
= connect_mock
504 m
= market
.Market(self
.ccxt
, self
.market_args(),
505 pg_config
={"config": "pg_config"}
, user_id
=1)
506 cursor_mock
.fetchone
.return_value
= [42]
508 with self
.subTest(error
=False),\
509 mock
.patch
.object(m
, "report") as report
:
510 report
.to_json_array
.return_value
= [
511 ("date1", "type1", "payload1"),
512 ("date2", "type2", "payload2"),
514 m
.store_database_report(datetime
.datetime(2018, 3, 24))
515 connect_mock
.assert_has_calls([
517 mock
.call
.cursor().execute('INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;', (datetime
.datetime(2018, 3, 24), None, False)),
518 mock
.call
.cursor().fetchone(),
519 mock
.call
.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date1', 42, 'type1', 'payload1')),
520 mock
.call
.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')),
522 mock
.call
.cursor().close(),
526 connect_mock
.reset_mock()
527 with self
.subTest(error
=True),\
528 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
529 psycopg2
.connect
.side_effect
= Exception("Bouh")
530 m
.store_database_report(datetime
.datetime(2018, 3, 24))
531 self
.assertEqual(stdout_mock
.getvalue(), "impossible to store report to database: Exception; Bouh\n")
533 @mock.patch.object(market
, "redis")
534 def test_store_redis_report(self
, redis
):
535 connect_mock
= mock
.Mock()
536 redis
.Redis
.return_value
= connect_mock
538 m
= market
.Market(self
.ccxt
, self
.market_args(),
539 redis_config
={"config": "redis_config"}
, market_id
=1)
541 with self
.subTest(error
=False),\
542 mock
.patch
.object(m
, "report") as report
:
543 report
.to_json_redis
.return_value
= [
544 ("type1", "payload1"),
545 ("type2", "payload2"),
547 m
.store_redis_report(datetime
.datetime(2018, 3, 24))
548 connect_mock
.assert_has_calls([
549 mock
.call
.set("/cryptoportfolio/1/2018-03-24T00:00:00/type1", "payload1", ex
=31*24*60*60),
550 mock
.call
.set("/cryptoportfolio/1/latest/type1", "payload1"),
551 mock
.call
.set("/cryptoportfolio/1/2018-03-24T00:00:00/type2", "payload2", ex
=31*24*60*60),
552 mock
.call
.set("/cryptoportfolio/1/latest/type2", "payload2"),
553 mock
.call
.set("/cryptoportfolio/1/latest/date", "2018-03-24T00:00:00"),
556 connect_mock
.reset_mock()
557 with self
.subTest(error
=True),\
558 mock
.patch('sys.stdout', new_callable
=StringIO
) as stdout_mock
:
559 redis
.Redis
.side_effect
= Exception("Bouh")
560 m
.store_redis_report(datetime
.datetime(2018, 3, 24))
561 self
.assertEqual(stdout_mock
.getvalue(), "impossible to store report to redis: Exception; Bouh\n")
563 def test_store_report(self
):
564 m
= market
.Market(self
.ccxt
, self
.market_args(report_db
=False), user_id
=1)
565 with self
.subTest(file=None, pg_config
=None),\
566 mock
.patch
.object(m
, "report") as report
,\
567 mock
.patch
.object(m
, "store_database_report") as db_report
,\
568 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
569 mock
.patch
.object(m
, "store_file_report") as file_report
:
571 report
.merge
.assert_called_with(store
.Portfolio
.report
)
573 file_report
.assert_not_called()
574 db_report
.assert_not_called()
575 redis_report
.assert_not_called()
578 m
= market
.Market(self
.ccxt
, self
.market_args(report_db
=False, report_path
="present"), user_id
=1)
579 with self
.subTest(file="present", pg_config
=None),\
580 mock
.patch
.object(m
, "report") as report
,\
581 mock
.patch
.object(m
, "store_file_report") as file_report
,\
582 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
583 mock
.patch
.object(m
, "store_database_report") as db_report
,\
584 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
586 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
590 report
.merge
.assert_called_with(store
.Portfolio
.report
)
591 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
592 db_report
.assert_not_called()
593 redis_report
.assert_not_called()
596 m
= market
.Market(self
.ccxt
, self
.market_args(report_db
=True, report_path
="present"), user_id
=1)
597 with self
.subTest(file="present", pg_config
=None, report_db
=True),\
598 mock
.patch
.object(m
, "report") as report
,\
599 mock
.patch
.object(m
, "store_file_report") as file_report
,\
600 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
601 mock
.patch
.object(m
, "store_database_report") as db_report
,\
602 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
604 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
608 report
.merge
.assert_called_with(store
.Portfolio
.report
)
609 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
610 db_report
.assert_not_called()
611 redis_report
.assert_not_called()
614 m
= market
.Market(self
.ccxt
, self
.market_args(report_db
=True), pg_config
="present", user_id
=1)
615 with self
.subTest(file=None, pg_config
="present"),\
616 mock
.patch
.object(m
, "report") as report
,\
617 mock
.patch
.object(m
, "store_file_report") as file_report
,\
618 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
619 mock
.patch
.object(m
, "store_database_report") as db_report
,\
620 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
622 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
626 report
.merge
.assert_called_with(store
.Portfolio
.report
)
627 file_report
.assert_not_called()
628 db_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
629 redis_report
.assert_not_called()
632 m
= market
.Market(self
.ccxt
, self
.market_args(report_db
=True, report_path
="present"),
633 pg_config
="pg_config", user_id
=1)
634 with self
.subTest(file="present", pg_config
="present"),\
635 mock
.patch
.object(m
, "report") as report
,\
636 mock
.patch
.object(m
, "store_file_report") as file_report
,\
637 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
638 mock
.patch
.object(m
, "store_database_report") as db_report
,\
639 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
641 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
645 report
.merge
.assert_called_with(store
.Portfolio
.report
)
646 file_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
647 db_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
648 redis_report
.assert_not_called()
651 m
= market
.Market(self
.ccxt
, self
.market_args(report_redis
=False),
652 redis_config
="redis_config", user_id
=1)
653 with self
.subTest(redis_config
="present", report_redis
=False),\
654 mock
.patch
.object(m
, "report") as report
,\
655 mock
.patch
.object(m
, "store_file_report") as file_report
,\
656 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
657 mock
.patch
.object(m
, "store_database_report") as db_report
,\
658 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
660 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
663 redis_report
.assert_not_called()
666 m
= market
.Market(self
.ccxt
, self
.market_args(report_redis
=True),
668 with self
.subTest(redis_config
="absent", report_redis
=True),\
669 mock
.patch
.object(m
, "report") as report
,\
670 mock
.patch
.object(m
, "store_file_report") as file_report
,\
671 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
672 mock
.patch
.object(m
, "store_database_report") as db_report
,\
673 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
675 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
678 redis_report
.assert_not_called()
681 m
= market
.Market(self
.ccxt
, self
.market_args(report_redis
=True),
682 redis_config
="redis_config", user_id
=1)
683 with self
.subTest(redis_config
="present", report_redis
=True),\
684 mock
.patch
.object(m
, "report") as report
,\
685 mock
.patch
.object(m
, "store_file_report") as file_report
,\
686 mock
.patch
.object(m
, "store_redis_report") as redis_report
,\
687 mock
.patch
.object(m
, "store_database_report") as db_report
,\
688 mock
.patch
.object(market
.datetime
, "datetime") as time_mock
:
690 time_mock
.now
.return_value
= datetime
.datetime(2018, 2, 25)
693 redis_report
.assert_called_once_with(datetime
.datetime(2018, 2, 25))
695 def test_print_tickers(self
):
696 m
= market
.Market(self
.ccxt
, self
.market_args())
698 with mock
.patch
.object(m
.balances
, "in_currency") as in_currency
,\
699 mock
.patch
.object(m
.report
, "log_stage") as log_stage
,\
700 mock
.patch
.object(m
.balances
, "fetch_balances") as fetch_balances
,\
701 mock
.patch
.object(m
.report
, "print_log") as print_log
:
703 in_currency
.return_value
= {
704 "BTC": portfolio
.Amount("BTC", "0.65"),
705 "ETH": portfolio
.Amount("BTC", "0.3"),
710 print_log
.assert_has_calls([
712 mock
.call(portfolio
.Amount("BTC", "0.95")),
715 @mock.patch("market.Processor.process")
716 @mock.patch("market.ReportStore.log_error")
717 @mock.patch("market.Market.store_report")
718 def test_process(self
, store_report
, log_error
, process
):
719 m
= market
.Market(self
.ccxt
, self
.market_args())
720 with self
.subTest(actions
=[], before
=False, after
=False):
723 process
.assert_not_called()
724 store_report
.assert_called_once()
725 log_error
.assert_not_called()
728 log_error
.reset_mock()
729 store_report
.reset_mock()
730 with self
.subTest(before
=True, after
=False):
731 m
.process(["foo"], before
=True)
733 process
.assert_called_once_with("foo", steps
="before")
734 store_report
.assert_called_once()
735 log_error
.assert_not_called()
738 log_error
.reset_mock()
739 store_report
.reset_mock()
740 with self
.subTest(before
=False, after
=True):
741 m
.process(["sell_all"], after
=True)
743 process
.assert_called_once_with("sell_all", steps
="after")
744 store_report
.assert_called_once()
745 log_error
.assert_not_called()
748 log_error
.reset_mock()
749 store_report
.reset_mock()
750 with self
.subTest(before
=False, after
=False):
753 process
.assert_called_once_with("foo", steps
="all")
754 store_report
.assert_called_once()
755 log_error
.assert_not_called()
758 log_error
.reset_mock()
759 store_report
.reset_mock()
760 with self
.subTest(before
=True, after
=True):
761 m
.process(["sell_all"], before
=True, after
=True)
763 process
.assert_called_once_with("sell_all", steps
="all")
764 store_report
.assert_called_once()
765 log_error
.assert_not_called()
768 log_error
.reset_mock()
769 store_report
.reset_mock()
770 with self
.subTest(unhandled_exception
=True):
771 process
.side_effect
= Exception("bouh")
773 m
.process(["some_action"], before
=True)
774 log_error
.assert_called_with("market_process", exception
=mock
.ANY
)
775 store_report
.assert_called_once()
778 @unittest.skipUnless("unit" in limits
, "Unit skipped")
779 class ProcessorTest(WebMockTestCase
):
780 def test_values(self
):
781 processor
= market
.Processor(self
.m
)
783 self
.assertEqual(self
.m
, processor
.market
)
785 def test_run_action(self
):
786 processor
= market
.Processor(self
.m
)
788 with mock
.patch
.object(processor
, "parse_args") as parse_args
:
789 method_mock
= mock
.Mock()
790 parse_args
.return_value
= [method_mock
, { "foo": "bar" }
]
792 processor
.run_action("foo", "bar", "baz")
794 parse_args
.assert_called_with("foo", "bar", "baz")
796 method_mock
.assert_called_with(foo
="bar")
798 processor
.run_action("wait_for_recent", "bar", "baz")
800 method_mock
.assert_called_with(foo
="bar")
802 def test_select_step(self
):
803 processor
= market
.Processor(self
.m
)
805 scenario
= processor
.scenarios
["sell_all"]
807 self
.assertEqual(scenario
, processor
.select_steps(scenario
, "all"))
808 self
.assertEqual(["all_sell"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "before"))))
809 self
.assertEqual(["wait", "all_buy"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "after"))))
810 self
.assertEqual(["wait"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, 2))))
811 self
.assertEqual(["wait"], list(map(lambda x
: x
["name"], processor
.select_steps(scenario
, "wait"))))
813 with self
.assertRaises(TypeError):
814 processor
.select_steps(scenario
, ["wait"])
816 def test_can_process(self
):
817 processor
= market
.Processor(self
.m
)
819 with self
.subTest(True):
820 self
.assertTrue(processor
.can_process("sell_all"))
822 with self
.subTest(False):
823 self
.assertFalse(processor
.can_process("unknown_action"))
825 @mock.patch("market.Processor.process_step")
826 def test_process(self
, process_step
):
827 with self
.subTest("unknown action"):
828 processor
= market
.Processor(self
.m
)
829 with self
.assertRaises(TypeError):
830 processor
.process("unknown_action")
832 with self
.subTest("nominal case"):
833 processor
= market
.Processor(self
.m
)
835 processor
.process("sell_all", foo
="bar")
836 self
.assertEqual(3, process_step
.call_count
)
838 steps
= list(map(lambda x
: x
[1][1]["name"], process_step
.mock_calls
))
839 scenario_names
= list(map(lambda x
: x
[1][0], process_step
.mock_calls
))
840 kwargs
= list(map(lambda x
: x
[1][2], process_step
.mock_calls
))
841 self
.assertEqual(["all_sell", "wait", "all_buy"], steps
)
842 self
.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names
)
843 self
.assertEqual([{"foo":"bar"}
, {"foo":"bar"}
, {"foo":"bar"}
], kwargs
)
845 process_step
.reset_mock()
847 processor
.process("sell_needed", steps
=["before", "after"])
848 self
.assertEqual(3, process_step
.call_count
)
850 def test_method_arguments(self
):
851 ccxt
= mock
.Mock(spec
=market
.ccxt
.poloniexE
)
852 m
= market
.Market(ccxt
, self
.market_args())
854 processor
= market
.Processor(m
)
856 method
, arguments
= processor
.method_arguments("wait_for_recent")
857 self
.assertEqual(market
.Portfolio
.wait_for_recent
, method
)
858 self
.assertEqual(["delta", "poll"], arguments
)
860 method
, arguments
= processor
.method_arguments("prepare_trades")
861 self
.assertEqual(m
.prepare_trades
, method
)
862 self
.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only'], arguments
)
864 method
, arguments
= processor
.method_arguments("prepare_orders")
865 self
.assertEqual(m
.trades
.prepare_orders
, method
)
867 method
, arguments
= processor
.method_arguments("move_balances")
868 self
.assertEqual(m
.move_balances
, method
)
870 method
, arguments
= processor
.method_arguments("run_orders")
871 self
.assertEqual(m
.trades
.run_orders
, method
)
873 method
, arguments
= processor
.method_arguments("follow_orders")
874 self
.assertEqual(m
.follow_orders
, method
)
876 method
, arguments
= processor
.method_arguments("close_trades")
877 self
.assertEqual(m
.trades
.close_trades
, method
)
879 method
, arguments
= processor
.method_arguments("print_tickers")
880 self
.assertEqual(m
.print_tickers
, method
)
882 def test_process_step(self
):
883 processor
= market
.Processor(self
.m
)
885 with mock
.patch
.object(processor
, "run_action") as run_action
:
886 step
= processor
.scenarios
["sell_needed"][1]
888 processor
.process_step("foo", step
, {"foo":"bar"}
)
890 self
.m
.report
.log_stage
.assert_has_calls([
891 mock
.call("process_foo__1_sell_begin"),
892 mock
.call("process_foo__1_sell_end"),
894 self
.m
.balances
.fetch_balances
.assert_has_calls([
895 mock
.call(tag
="process_foo__1_sell_begin"),
896 mock
.call(tag
="process_foo__1_sell_end"),
899 self
.assertEqual(5, run_action
.call_count
)
901 run_action
.assert_has_calls([
902 mock
.call('prepare_trades', {}, {'foo': 'bar'}
),
903 mock
.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}
, {'foo': 'bar'}
),
904 mock
.call('run_orders', {}, {'foo': 'bar'}
),
905 mock
.call('follow_orders', {}, {'foo': 'bar'}
),
906 mock
.call('close_trades', {}, {'foo': 'bar'}
),
910 with mock
.patch
.object(processor
, "run_action") as run_action
:
911 step
= processor
.scenarios
["sell_needed"][0]
913 processor
.process_step("foo", step
, {"foo":"bar"}
)
914 self
.m
.balances
.fetch_balances
.assert_not_called()
916 def test_parse_args(self
):
917 processor
= market
.Processor(self
.m
)
919 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
920 method_mock
= mock
.Mock()
921 method_arguments
.return_value
= [
925 method
, args
= processor
.parse_args("action", {"foo": "bar", "foo2": "bar"}
, {"foo": "bar2", "bla": "bla"}
)
927 self
.assertEqual(method_mock
, method
)
928 self
.assertEqual({"foo": "bar2", "foo2": "bar"}
, args
)
930 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
931 method_mock
= mock
.Mock()
932 method_arguments
.return_value
= [
936 method
, args
= processor
.parse_args("action", {"repartition": { "base_currency": 1 }
}, {})
938 self
.assertEqual(1, len(args
["repartition"]))
939 self
.assertIn("BTC", args
["repartition"])
941 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
942 method_mock
= mock
.Mock()
943 method_arguments
.return_value
= [
945 ["repartition", "base_currency"]
947 method
, args
= processor
.parse_args("action", {"repartition": { "base_currency": 1 }
}, {"base_currency": "USDT"}
)
949 self
.assertEqual(1, len(args
["repartition"]))
950 self
.assertIn("USDT", args
["repartition"])
952 with mock
.patch
.object(processor
, "method_arguments") as method_arguments
:
953 method_mock
= mock
.Mock()
954 method_arguments
.return_value
= [
956 ["repartition", "base_currency"]
958 method
, args
= processor
.parse_args("action", {"repartition": { "ETH": 1 }
}, {"base_currency": "USDT"}
)
960 self
.assertEqual(1, len(args
["repartition"]))
961 self
.assertIn("ETH", args
["repartition"])