]>
Commit | Line | Data |
---|---|---|
c682bdf4 | 1 | from .helper import * |
30700830 | 2 | import market, store, portfolio, dbs |
c682bdf4 IB |
3 | import datetime |
4 | ||
3080f31d | 5 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
c682bdf4 IB |
6 | class MarketTest(WebMockTestCase): |
7 | def setUp(self): | |
8 | super().setUp() | |
9 | ||
10 | self.ccxt = mock.Mock(spec=market.ccxt.poloniexE) | |
11 | ||
12 | def test_values(self): | |
13 | m = market.Market(self.ccxt, self.market_args()) | |
14 | ||
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) | |
24 | ||
25 | m = market.Market(self.ccxt, self.market_args(debug=True)) | |
26 | self.assertTrue(m.debug) | |
27 | ||
28 | m = market.Market(self.ccxt, self.market_args(debug=False)) | |
29 | self.assertFalse(m.debug) | |
30 | ||
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() | |
41 | ||
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 | |
46 | ||
47 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args()) | |
48 | ||
49 | self.assertEqual(self.ccxt, m.ccxt) | |
50 | ||
51 | m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True)) | |
52 | self.assertEqual(True, m.debug) | |
53 | ||
54 | def test_get_tickers(self): | |
55 | self.ccxt.fetch_tickers.side_effect = [ | |
56 | "tickers", | |
57 | market.NotSupported | |
58 | ] | |
59 | ||
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() | |
64 | ||
65 | self.assertIsNone(m.get_tickers(refresh=self.time.time())) | |
66 | ||
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 }, | |
72 | } | |
73 | m = market.Market(self.ccxt, self.market_args()) | |
74 | ||
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"]) | |
80 | ||
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"]) | |
87 | ||
88 | ticker = m.get_ticker("XVG", "XMR") | |
89 | self.assertIsNone(ticker) | |
90 | ||
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"), | |
99 | ] | |
100 | ||
101 | m = market.Market(self.ccxt, self.market_args()) | |
102 | ||
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"]) | |
109 | ||
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"]) | |
116 | ||
117 | ticker = m.get_ticker("XVG", "XMR") | |
118 | self.assertIsNone(ticker) | |
119 | ||
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() | |
128 | ||
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): | |
96959cea IB |
133 | with self.subTest(available_balance_only=False),\ |
134 | mock.patch("market.ReportStore"): | |
135 | def _get_ticker(c1, c2): | |
136 | if c1 == "USDT" and c2 == "BTC": | |
137 | return { "average": D("0.0001") } | |
138 | if c1 == "XVG" and c2 == "BTC": | |
139 | return { "average": D("0.000001") } | |
140 | self.fail("Should not be called with {}, {}".format(c1, c2)) | |
141 | get_ticker.side_effect = _get_ticker | |
142 | ||
143 | repartition.return_value = { | |
144 | "XEM": (D("0.75"), "long"), | |
145 | "BTC": (D("0.25"), "long"), | |
146 | } | |
c682bdf4 IB |
147 | m = market.Market(self.ccxt, self.market_args()) |
148 | self.ccxt.fetch_all_balances.return_value = { | |
149 | "USDT": { | |
150 | "exchange_free": D("10000.0"), | |
151 | "exchange_used": D("0.0"), | |
152 | "exchange_total": D("10000.0"), | |
153 | "total": D("10000.0") | |
154 | }, | |
155 | "XVG": { | |
156 | "exchange_free": D("10000.0"), | |
157 | "exchange_used": D("0.0"), | |
158 | "exchange_total": D("10000.0"), | |
159 | "total": D("10000.0") | |
160 | }, | |
161 | } | |
162 | ||
163 | m.balances.fetch_balances(tag="tag") | |
164 | ||
165 | m.prepare_trades() | |
166 | compute_trades.assert_called() | |
167 | ||
168 | call = compute_trades.call_args | |
169 | self.assertEqual(1, call[0][0]["USDT"].value) | |
170 | self.assertEqual(D("0.01"), call[0][0]["XVG"].value) | |
171 | self.assertEqual(D("0.2525"), call[0][1]["BTC"].value) | |
172 | self.assertEqual(D("0.7575"), call[0][1]["XEM"].value) | |
173 | m.report.log_stage.assert_called_once_with("prepare_trades", | |
174 | base_currency='BTC', compute_value='average', | |
96959cea IB |
175 | available_balance_only=False, liquidity='medium', |
176 | only=None, repartition=None) | |
bb127bc8 | 177 | m.report.log_balances.assert_called_once_with(tag="tag", checkpoint=None) |
c682bdf4 | 178 | |
96959cea IB |
179 | compute_trades.reset_mock() |
180 | with self.subTest(available_balance_only=True),\ | |
181 | mock.patch("market.ReportStore"): | |
182 | def _get_ticker(c1, c2): | |
183 | if c1 == "ZRC" and c2 == "BTC": | |
184 | return { "average": D("0.0001") } | |
185 | if c1 == "DOGE" and c2 == "BTC": | |
186 | return { "average": D("0.000001") } | |
187 | if c1 == "ETH" and c2 == "BTC": | |
188 | return { "average": D("0.1") } | |
3d6f74ee IB |
189 | if c1 == "FOO" and c2 == "BTC": |
190 | return { "average": D("0.1") } | |
96959cea IB |
191 | self.fail("Should not be called with {}, {}".format(c1, c2)) |
192 | get_ticker.side_effect = _get_ticker | |
193 | ||
194 | repartition.return_value = { | |
3d6f74ee IB |
195 | "DOGE": (D("0.20"), "short"), |
196 | "BTC": (D("0.20"), "long"), | |
197 | "ETH": (D("0.20"), "long"), | |
198 | "XMR": (D("0.20"), "long"), | |
199 | "FOO": (D("0.20"), "long"), | |
96959cea IB |
200 | } |
201 | m = market.Market(self.ccxt, self.market_args()) | |
202 | self.ccxt.fetch_all_balances.return_value = { | |
203 | "ZRC": { | |
204 | "exchange_free": D("2.0"), | |
205 | "exchange_used": D("0.0"), | |
206 | "exchange_total": D("2.0"), | |
207 | "total": D("2.0") | |
208 | }, | |
209 | "DOGE": { | |
210 | "exchange_free": D("5.0"), | |
211 | "exchange_used": D("0.0"), | |
212 | "exchange_total": D("5.0"), | |
213 | "total": D("5.0") | |
214 | }, | |
215 | "BTC": { | |
3d6f74ee | 216 | "exchange_free": D("0.065"), |
96959cea | 217 | "exchange_used": D("0.02"), |
3d6f74ee IB |
218 | "exchange_total": D("0.085"), |
219 | "margin_available": D("0.035"), | |
96959cea | 220 | "margin_in_position": D("0.01"), |
3d6f74ee | 221 | "margin_total": D("0.045"), |
96959cea IB |
222 | "total": D("0.13") |
223 | }, | |
224 | "ETH": { | |
225 | "exchange_free": D("1.0"), | |
226 | "exchange_used": D("0.0"), | |
227 | "exchange_total": D("1.0"), | |
228 | "total": D("1.0") | |
229 | }, | |
3d6f74ee IB |
230 | "FOO": { |
231 | "exchange_free": D("0.1"), | |
232 | "exchange_used": D("0.0"), | |
233 | "exchange_total": D("0.1"), | |
234 | "total": D("0.1"), | |
235 | }, | |
96959cea IB |
236 | } |
237 | ||
238 | m.balances.fetch_balances(tag="tag") | |
239 | m.prepare_trades(available_balance_only=True) | |
240 | compute_trades.assert_called_once() | |
241 | ||
242 | call = compute_trades.call_args[0] | |
243 | values_in_base = call[0] | |
244 | new_repartition = call[1] | |
245 | ||
246 | self.assertEqual(portfolio.Amount("BTC", "-0.025"), | |
247 | new_repartition["DOGE"] - values_in_base["DOGE"]) | |
96959cea | 248 | self.assertEqual(0, |
3d6f74ee IB |
249 | new_repartition["ETH"] - values_in_base["ETH"]) |
250 | self.assertIsNone(new_repartition.get("ZRC")) | |
96959cea IB |
251 | self.assertEqual(portfolio.Amount("BTC", "0.025"), |
252 | new_repartition["XMR"]) | |
3d6f74ee IB |
253 | self.assertEqual(portfolio.Amount("BTC", "0.015"), |
254 | new_repartition["FOO"] - values_in_base["FOO"]) | |
96959cea IB |
255 | |
256 | compute_trades.reset_mock() | |
257 | with self.subTest(available_balance_only=True, balance=0),\ | |
258 | mock.patch("market.ReportStore"): | |
259 | def _get_ticker(c1, c2): | |
260 | if c1 == "ETH" and c2 == "BTC": | |
261 | return { "average": D("0.1") } | |
262 | self.fail("Should not be called with {}, {}".format(c1, c2)) | |
263 | get_ticker.side_effect = _get_ticker | |
264 | ||
265 | repartition.return_value = { | |
266 | "BTC": (D("0.5"), "long"), | |
267 | "ETH": (D("0.5"), "long"), | |
268 | } | |
269 | m = market.Market(self.ccxt, self.market_args()) | |
270 | self.ccxt.fetch_all_balances.return_value = { | |
271 | "ETH": { | |
272 | "exchange_free": D("1.0"), | |
273 | "exchange_used": D("0.0"), | |
274 | "exchange_total": D("1.0"), | |
275 | "total": D("1.0") | |
276 | }, | |
277 | } | |
278 | ||
279 | m.balances.fetch_balances(tag="tag") | |
280 | m.prepare_trades(available_balance_only=True) | |
281 | compute_trades.assert_called_once() | |
282 | ||
283 | call = compute_trades.call_args[0] | |
284 | values_in_base = call[0] | |
285 | new_repartition = call[1] | |
286 | ||
287 | self.assertEqual(new_repartition["ETH"], values_in_base["ETH"]) | |
c682bdf4 IB |
288 | |
289 | @mock.patch.object(market.time, "sleep") | |
290 | @mock.patch.object(market.TradeStore, "all_orders") | |
291 | def test_follow_orders(self, all_orders, time_mock): | |
292 | for debug, sleep in [ | |
293 | (False, None), (True, None), | |
294 | (False, 12), (True, 12)]: | |
295 | with self.subTest(sleep=sleep, debug=debug), \ | |
296 | mock.patch("market.ReportStore"): | |
297 | m = market.Market(self.ccxt, self.market_args(debug=debug)) | |
298 | ||
299 | order_mock1 = mock.Mock() | |
300 | order_mock2 = mock.Mock() | |
301 | order_mock3 = mock.Mock() | |
302 | all_orders.side_effect = [ | |
303 | [order_mock1, order_mock2], | |
304 | [order_mock1, order_mock2], | |
305 | ||
306 | [order_mock1, order_mock3], | |
307 | [order_mock1, order_mock3], | |
308 | ||
309 | [order_mock1, order_mock3], | |
310 | [order_mock1, order_mock3], | |
311 | ||
312 | [] | |
313 | ] | |
314 | ||
315 | order_mock1.get_status.side_effect = ["open", "open", "closed"] | |
316 | order_mock2.get_status.side_effect = ["open"] | |
317 | order_mock3.get_status.side_effect = ["open", "closed"] | |
318 | ||
319 | order_mock1.trade = mock.Mock() | |
320 | order_mock2.trade = mock.Mock() | |
321 | order_mock3.trade = mock.Mock() | |
322 | ||
323 | m.follow_orders(sleep=sleep) | |
324 | ||
325 | order_mock1.trade.update_order.assert_any_call(order_mock1, 1) | |
326 | order_mock1.trade.update_order.assert_any_call(order_mock1, 2) | |
327 | self.assertEqual(2, order_mock1.trade.update_order.call_count) | |
328 | self.assertEqual(3, order_mock1.get_status.call_count) | |
329 | ||
330 | order_mock2.trade.update_order.assert_any_call(order_mock2, 1) | |
331 | self.assertEqual(1, order_mock2.trade.update_order.call_count) | |
332 | self.assertEqual(1, order_mock2.get_status.call_count) | |
333 | ||
334 | order_mock3.trade.update_order.assert_any_call(order_mock3, 2) | |
335 | self.assertEqual(1, order_mock3.trade.update_order.call_count) | |
336 | self.assertEqual(2, order_mock3.get_status.call_count) | |
337 | m.report.log_stage.assert_called() | |
338 | calls = [ | |
339 | mock.call("follow_orders_begin"), | |
340 | mock.call("follow_orders_tick_1"), | |
341 | mock.call("follow_orders_tick_2"), | |
342 | mock.call("follow_orders_tick_3"), | |
343 | mock.call("follow_orders_end"), | |
344 | ] | |
345 | m.report.log_stage.assert_has_calls(calls) | |
346 | m.report.log_orders.assert_called() | |
347 | self.assertEqual(3, m.report.log_orders.call_count) | |
348 | calls = [ | |
349 | mock.call([order_mock1, order_mock2], tick=1), | |
350 | mock.call([order_mock1, order_mock3], tick=2), | |
351 | mock.call([order_mock1, order_mock3], tick=3), | |
352 | ] | |
353 | m.report.log_orders.assert_has_calls(calls) | |
354 | calls = [ | |
355 | mock.call(order_mock1, 3, finished=True), | |
356 | mock.call(order_mock3, 3, finished=True), | |
357 | ] | |
358 | m.report.log_order.assert_has_calls(calls) | |
359 | ||
360 | if sleep is None: | |
361 | if debug: | |
362 | m.report.log_debug_action.assert_called_with("Set follow_orders tick to 7s") | |
363 | time_mock.assert_called_with(7) | |
364 | else: | |
365 | time_mock.assert_called_with(30) | |
366 | else: | |
367 | time_mock.assert_called_with(sleep) | |
368 | ||
369 | with self.subTest("disappearing order"), \ | |
370 | mock.patch("market.ReportStore"): | |
371 | all_orders.reset_mock() | |
372 | m = market.Market(self.ccxt, self.market_args()) | |
373 | ||
374 | order_mock1 = mock.Mock() | |
375 | order_mock2 = mock.Mock() | |
376 | all_orders.side_effect = [ | |
377 | [order_mock1, order_mock2], | |
378 | [order_mock1, order_mock2], | |
379 | ||
380 | [order_mock1, order_mock2], | |
381 | [order_mock1, order_mock2], | |
382 | ||
383 | [] | |
384 | ] | |
385 | ||
386 | order_mock1.get_status.side_effect = ["open", "closed"] | |
387 | order_mock2.get_status.side_effect = ["open", "error_disappeared"] | |
388 | ||
389 | order_mock1.trade = mock.Mock() | |
390 | trade_mock = mock.Mock() | |
391 | order_mock2.trade = trade_mock | |
392 | ||
393 | trade_mock.tick_actions_recreate.return_value = "tick1" | |
af928d32 IB |
394 | new_order_mock = mock.Mock() |
395 | trade_mock.prepare_order.return_value = new_order_mock | |
c682bdf4 IB |
396 | |
397 | m.follow_orders() | |
398 | ||
399 | trade_mock.tick_actions_recreate.assert_called_once_with(2) | |
400 | trade_mock.prepare_order.assert_called_once_with(compute_value="tick1") | |
401 | m.report.log_error.assert_called_once_with("follow_orders", message=mock.ANY) | |
af928d32 IB |
402 | m.report.log_order.assert_called_with(order_mock2, 2, new_order=new_order_mock) |
403 | new_order_mock.run.assert_called_once_with() | |
c682bdf4 IB |
404 | |
405 | @mock.patch.object(market.BalanceStore, "fetch_balances") | |
406 | def test_move_balance(self, fetch_balances): | |
407 | for debug in [True, False]: | |
408 | with self.subTest(debug=debug),\ | |
409 | mock.patch("market.ReportStore"): | |
410 | m = market.Market(self.ccxt, self.market_args(debug=debug)) | |
411 | ||
412 | value_from = portfolio.Amount("BTC", "1.0") | |
413 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
414 | value_to = portfolio.Amount("BTC", "10.0") | |
415 | trade1 = portfolio.Trade(value_from, value_to, "ETH", m) | |
416 | ||
417 | value_from = portfolio.Amount("BTC", "0.0") | |
418 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
419 | value_to = portfolio.Amount("BTC", "-3.0") | |
420 | trade2 = portfolio.Trade(value_from, value_to, "ETH", m) | |
421 | ||
422 | value_from = portfolio.Amount("USDT", "0.0") | |
423 | value_from.linked_to = portfolio.Amount("XVG", "0.0") | |
424 | value_to = portfolio.Amount("USDT", "-50.0") | |
425 | trade3 = portfolio.Trade(value_from, value_to, "XVG", m) | |
426 | ||
427 | m.trades.all = [trade1, trade2, trade3] | |
428 | balance1 = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
429 | balance2 = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }) | |
430 | balance3 = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }) | |
431 | m.balances.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3} | |
432 | ||
433 | m.move_balances() | |
434 | ||
435 | fetch_balances.assert_called_with() | |
436 | m.report.log_move_balances.assert_called_once() | |
437 | ||
438 | if debug: | |
439 | m.report.log_debug_action.assert_called() | |
440 | self.assertEqual(3, m.report.log_debug_action.call_count) | |
441 | else: | |
442 | self.ccxt.transfer_balance.assert_any_call("BTC", 3, "exchange", "margin") | |
443 | self.ccxt.transfer_balance.assert_any_call("USDT", 100, "exchange", "margin") | |
444 | self.ccxt.transfer_balance.assert_any_call("ETC", 5, "margin", "exchange") | |
445 | ||
446 | m.report.reset_mock() | |
447 | fetch_balances.reset_mock() | |
448 | with self.subTest(retry=True): | |
449 | with mock.patch("market.ReportStore"): | |
450 | m = market.Market(self.ccxt, self.market_args()) | |
451 | ||
452 | value_from = portfolio.Amount("BTC", "0.0") | |
453 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
454 | value_to = portfolio.Amount("BTC", "-3.0") | |
455 | trade = portfolio.Trade(value_from, value_to, "ETH", m) | |
456 | ||
457 | m.trades.all = [trade] | |
458 | balance = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
459 | m.balances.all = {"BTC": balance} | |
460 | ||
461 | m.ccxt.transfer_balance.side_effect = [ | |
462 | market.ccxt.RequestTimeout, | |
463 | market.ccxt.InvalidNonce, | |
464 | True | |
465 | ] | |
466 | m.move_balances() | |
467 | self.ccxt.transfer_balance.assert_has_calls([ | |
468 | mock.call("BTC", 3, "exchange", "margin"), | |
469 | mock.call("BTC", 3, "exchange", "margin"), | |
470 | mock.call("BTC", 3, "exchange", "margin") | |
471 | ]) | |
472 | self.assertEqual(3, fetch_balances.call_count) | |
473 | m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) | |
474 | self.assertEqual(3, m.report.log_move_balances.call_count) | |
475 | ||
476 | self.ccxt.transfer_balance.reset_mock() | |
477 | m.report.reset_mock() | |
478 | fetch_balances.reset_mock() | |
479 | with self.subTest(retry=True, too_much=True): | |
480 | with mock.patch("market.ReportStore"): | |
481 | m = market.Market(self.ccxt, self.market_args()) | |
482 | ||
483 | value_from = portfolio.Amount("BTC", "0.0") | |
484 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
485 | value_to = portfolio.Amount("BTC", "-3.0") | |
486 | trade = portfolio.Trade(value_from, value_to, "ETH", m) | |
487 | ||
488 | m.trades.all = [trade] | |
489 | balance = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
490 | m.balances.all = {"BTC": balance} | |
491 | ||
492 | m.ccxt.transfer_balance.side_effect = [ | |
493 | market.ccxt.RequestTimeout, | |
494 | market.ccxt.RequestTimeout, | |
495 | market.ccxt.RequestTimeout, | |
496 | market.ccxt.RequestTimeout, | |
497 | market.ccxt.RequestTimeout, | |
498 | ] | |
499 | with self.assertRaises(market.ccxt.RequestTimeout): | |
500 | m.move_balances() | |
501 | ||
502 | self.ccxt.transfer_balance.reset_mock() | |
503 | m.report.reset_mock() | |
504 | fetch_balances.reset_mock() | |
505 | with self.subTest(retry=True, partial_result=True): | |
506 | with mock.patch("market.ReportStore"): | |
507 | m = market.Market(self.ccxt, self.market_args()) | |
508 | ||
509 | value_from = portfolio.Amount("BTC", "1.0") | |
510 | value_from.linked_to = portfolio.Amount("ETH", "10.0") | |
511 | value_to = portfolio.Amount("BTC", "10.0") | |
512 | trade1 = portfolio.Trade(value_from, value_to, "ETH", m) | |
513 | ||
514 | value_from = portfolio.Amount("BTC", "0.0") | |
515 | value_from.linked_to = portfolio.Amount("ETH", "0.0") | |
516 | value_to = portfolio.Amount("BTC", "-3.0") | |
517 | trade2 = portfolio.Trade(value_from, value_to, "ETH", m) | |
518 | ||
519 | value_from = portfolio.Amount("USDT", "0.0") | |
520 | value_from.linked_to = portfolio.Amount("XVG", "0.0") | |
521 | value_to = portfolio.Amount("USDT", "-50.0") | |
522 | trade3 = portfolio.Trade(value_from, value_to, "XVG", m) | |
523 | ||
524 | m.trades.all = [trade1, trade2, trade3] | |
525 | balance1 = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) | |
526 | balance2 = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }) | |
527 | balance3 = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }) | |
528 | m.balances.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3} | |
529 | ||
530 | call_counts = { "BTC": 0, "USDT": 0, "ETC": 0 } | |
531 | def _transfer_balance(currency, amount, from_, to_): | |
532 | call_counts[currency] += 1 | |
533 | if currency == "BTC": | |
534 | m.balances.all["BTC"] = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "3" }) | |
535 | if currency == "USDT": | |
536 | if call_counts["USDT"] == 1: | |
537 | raise market.ccxt.RequestTimeout | |
538 | else: | |
539 | m.balances.all["USDT"] = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "150" }) | |
540 | if currency == "ETC": | |
541 | m.balances.all["ETC"] = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "10" }) | |
542 | ||
543 | ||
544 | m.ccxt.transfer_balance.side_effect = _transfer_balance | |
545 | ||
546 | m.move_balances() | |
547 | self.ccxt.transfer_balance.assert_has_calls([ | |
548 | mock.call("BTC", 3, "exchange", "margin"), | |
549 | mock.call('USDT', 100, 'exchange', 'margin'), | |
550 | mock.call('USDT', 100, 'exchange', 'margin'), | |
551 | mock.call("ETC", 5, "margin", "exchange") | |
552 | ]) | |
553 | self.assertEqual(2, fetch_balances.call_count) | |
554 | m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) | |
555 | self.assertEqual(2, m.report.log_move_balances.call_count) | |
556 | m.report.log_move_balances.asser_has_calls([ | |
557 | mock.call( | |
558 | { | |
559 | 'BTC': portfolio.Amount("BTC", "3"), | |
560 | 'USDT': portfolio.Amount("USDT", "150"), | |
561 | 'ETC': portfolio.Amount("ETC", "10"), | |
562 | }, | |
563 | { | |
564 | 'BTC': portfolio.Amount("BTC", "3"), | |
565 | 'USDT': portfolio.Amount("USDT", "100"), | |
566 | }), | |
567 | mock.call( | |
568 | { | |
569 | 'BTC': portfolio.Amount("BTC", "3"), | |
570 | 'USDT': portfolio.Amount("USDT", "150"), | |
571 | 'ETC': portfolio.Amount("ETC", "10"), | |
572 | }, | |
573 | { | |
574 | 'BTC': portfolio.Amount("BTC", "0"), | |
575 | 'USDT': portfolio.Amount("USDT", "100"), | |
576 | 'ETC': portfolio.Amount("ETC", "-5"), | |
577 | }), | |
578 | ]) | |
579 | ||
580 | ||
581 | def test_store_file_report(self): | |
582 | file_open = mock.mock_open() | |
583 | m = market.Market(self.ccxt, | |
584 | self.market_args(report_path="present"), user_id=1) | |
585 | with self.subTest(file="present"),\ | |
586 | mock.patch("market.open", file_open),\ | |
587 | mock.patch.object(m, "report") as report,\ | |
588 | mock.patch.object(market, "datetime") as time_mock: | |
589 | ||
590 | report.print_logs = [[time_mock.now(), "Foo"], [time_mock.now(), "Bar"]] | |
591 | report.to_json.return_value = "json_content" | |
592 | ||
593 | m.store_file_report(datetime.datetime(2018, 2, 25)) | |
594 | ||
595 | file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") | |
596 | file_open.assert_any_call("present/2018-02-25T00:00:00_1.log", "w") | |
597 | file_open().write.assert_any_call("json_content") | |
598 | file_open().write.assert_any_call("Foo\nBar") | |
599 | m.report.to_json.assert_called_once_with() | |
600 | ||
601 | m = market.Market(self.ccxt, self.market_args(report_path="error"), user_id=1) | |
602 | with self.subTest(file="error"),\ | |
603 | mock.patch("market.open") as file_open,\ | |
604 | mock.patch.object(m, "report") as report,\ | |
605 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
606 | file_open.side_effect = FileNotFoundError | |
607 | ||
608 | m.store_file_report(datetime.datetime(2018, 2, 25)) | |
609 | ||
610 | self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") | |
611 | ||
30700830 IB |
612 | @mock.patch.object(dbs, "psql") |
613 | def test_store_database_report(self, psql): | |
c682bdf4 IB |
614 | cursor_mock = mock.MagicMock() |
615 | ||
30700830 | 616 | psql.cursor.return_value = cursor_mock |
c682bdf4 IB |
617 | m = market.Market(self.ccxt, self.market_args(), |
618 | pg_config={"config": "pg_config"}, user_id=1) | |
619 | cursor_mock.fetchone.return_value = [42] | |
620 | ||
621 | with self.subTest(error=False),\ | |
622 | mock.patch.object(m, "report") as report: | |
623 | report.to_json_array.return_value = [ | |
624 | ("date1", "type1", "payload1"), | |
625 | ("date2", "type2", "payload2"), | |
626 | ] | |
627 | m.store_database_report(datetime.datetime(2018, 3, 24)) | |
30700830 | 628 | psql.assert_has_calls([ |
c682bdf4 IB |
629 | mock.call.cursor(), |
630 | 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)), | |
631 | mock.call.cursor().fetchone(), | |
632 | mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date1', 42, 'type1', 'payload1')), | |
633 | mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')), | |
634 | mock.call.commit(), | |
635 | mock.call.cursor().close(), | |
c682bdf4 IB |
636 | ]) |
637 | ||
c682bdf4 IB |
638 | with self.subTest(error=True),\ |
639 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
30700830 | 640 | psql.cursor.side_effect = Exception("Bouh") |
c682bdf4 IB |
641 | m.store_database_report(datetime.datetime(2018, 3, 24)) |
642 | self.assertEqual(stdout_mock.getvalue(), "impossible to store report to database: Exception; Bouh\n") | |
643 | ||
30700830 | 644 | @mock.patch.object(dbs, "redis") |
1593c7a9 | 645 | def test_store_redis_report(self, redis): |
1593c7a9 IB |
646 | m = market.Market(self.ccxt, self.market_args(), |
647 | redis_config={"config": "redis_config"}, market_id=1) | |
648 | ||
649 | with self.subTest(error=False),\ | |
650 | mock.patch.object(m, "report") as report: | |
651 | report.to_json_redis.return_value = [ | |
652 | ("type1", "payload1"), | |
653 | ("type2", "payload2"), | |
654 | ] | |
655 | m.store_redis_report(datetime.datetime(2018, 3, 24)) | |
30700830 | 656 | redis.assert_has_calls([ |
1593c7a9 IB |
657 | mock.call.set("/cryptoportfolio/1/2018-03-24T00:00:00/type1", "payload1", ex=31*24*60*60), |
658 | mock.call.set("/cryptoportfolio/1/latest/type1", "payload1"), | |
659 | mock.call.set("/cryptoportfolio/1/2018-03-24T00:00:00/type2", "payload2", ex=31*24*60*60), | |
660 | mock.call.set("/cryptoportfolio/1/latest/type2", "payload2"), | |
17fd3f75 | 661 | mock.call.set("/cryptoportfolio/1/latest/date", "2018-03-24T00:00:00"), |
1593c7a9 IB |
662 | ]) |
663 | ||
30700830 | 664 | redis.reset_mock() |
1593c7a9 IB |
665 | with self.subTest(error=True),\ |
666 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
30700830 | 667 | redis.set.side_effect = Exception("Bouh") |
1593c7a9 IB |
668 | m.store_redis_report(datetime.datetime(2018, 3, 24)) |
669 | self.assertEqual(stdout_mock.getvalue(), "impossible to store report to redis: Exception; Bouh\n") | |
670 | ||
c682bdf4 IB |
671 | def test_store_report(self): |
672 | m = market.Market(self.ccxt, self.market_args(report_db=False), user_id=1) | |
30700830 IB |
673 | with self.subTest(file=None, pg_connected=None),\ |
674 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
675 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
c682bdf4 IB |
676 | mock.patch.object(m, "report") as report,\ |
677 | mock.patch.object(m, "store_database_report") as db_report,\ | |
1593c7a9 | 678 | mock.patch.object(m, "store_redis_report") as redis_report,\ |
c682bdf4 | 679 | mock.patch.object(m, "store_file_report") as file_report: |
30700830 IB |
680 | psql.return_value = False |
681 | redis.return_value = False | |
c682bdf4 IB |
682 | m.store_report() |
683 | report.merge.assert_called_with(store.Portfolio.report) | |
684 | ||
685 | file_report.assert_not_called() | |
686 | db_report.assert_not_called() | |
1593c7a9 | 687 | redis_report.assert_not_called() |
c682bdf4 IB |
688 | |
689 | report.reset_mock() | |
690 | m = market.Market(self.ccxt, self.market_args(report_db=False, report_path="present"), user_id=1) | |
30700830 IB |
691 | with self.subTest(file="present", pg_connected=None),\ |
692 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
693 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
c682bdf4 IB |
694 | mock.patch.object(m, "report") as report,\ |
695 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1593c7a9 | 696 | mock.patch.object(m, "store_redis_report") as redis_report,\ |
c682bdf4 | 697 | mock.patch.object(m, "store_database_report") as db_report,\ |
e7d7c0e5 | 698 | mock.patch.object(market.datetime, "datetime") as time_mock: |
30700830 IB |
699 | psql.return_value = False |
700 | redis.return_value = False | |
c682bdf4 IB |
701 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
702 | ||
703 | m.store_report() | |
704 | ||
705 | report.merge.assert_called_with(store.Portfolio.report) | |
706 | file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
707 | db_report.assert_not_called() | |
1593c7a9 | 708 | redis_report.assert_not_called() |
c682bdf4 IB |
709 | |
710 | report.reset_mock() | |
711 | m = market.Market(self.ccxt, self.market_args(report_db=True, report_path="present"), user_id=1) | |
30700830 IB |
712 | with self.subTest(file="present", pg_connected=None, report_db=True),\ |
713 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
714 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
c682bdf4 IB |
715 | mock.patch.object(m, "report") as report,\ |
716 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1593c7a9 | 717 | mock.patch.object(m, "store_redis_report") as redis_report,\ |
c682bdf4 | 718 | mock.patch.object(m, "store_database_report") as db_report,\ |
e7d7c0e5 | 719 | mock.patch.object(market.datetime, "datetime") as time_mock: |
30700830 IB |
720 | psql.return_value = False |
721 | redis.return_value = False | |
c682bdf4 IB |
722 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
723 | ||
724 | m.store_report() | |
725 | ||
726 | report.merge.assert_called_with(store.Portfolio.report) | |
727 | file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
728 | db_report.assert_not_called() | |
1593c7a9 | 729 | redis_report.assert_not_called() |
c682bdf4 IB |
730 | |
731 | report.reset_mock() | |
30700830 IB |
732 | m = market.Market(self.ccxt, self.market_args(report_db=True), user_id=1) |
733 | with self.subTest(file=None, pg_connected=True),\ | |
734 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
735 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
c682bdf4 IB |
736 | mock.patch.object(m, "report") as report,\ |
737 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1593c7a9 | 738 | mock.patch.object(m, "store_redis_report") as redis_report,\ |
c682bdf4 | 739 | mock.patch.object(m, "store_database_report") as db_report,\ |
e7d7c0e5 | 740 | mock.patch.object(market.datetime, "datetime") as time_mock: |
30700830 IB |
741 | psql.return_value = True |
742 | redis.return_value = False | |
c682bdf4 IB |
743 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
744 | ||
745 | m.store_report() | |
746 | ||
747 | report.merge.assert_called_with(store.Portfolio.report) | |
748 | file_report.assert_not_called() | |
749 | db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
1593c7a9 | 750 | redis_report.assert_not_called() |
c682bdf4 IB |
751 | |
752 | report.reset_mock() | |
753 | m = market.Market(self.ccxt, self.market_args(report_db=True, report_path="present"), | |
30700830 IB |
754 | user_id=1) |
755 | with self.subTest(file="present", pg_connected=True),\ | |
756 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
757 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
c682bdf4 IB |
758 | mock.patch.object(m, "report") as report,\ |
759 | mock.patch.object(m, "store_file_report") as file_report,\ | |
1593c7a9 | 760 | mock.patch.object(m, "store_redis_report") as redis_report,\ |
c682bdf4 | 761 | mock.patch.object(m, "store_database_report") as db_report,\ |
e7d7c0e5 | 762 | mock.patch.object(market.datetime, "datetime") as time_mock: |
30700830 IB |
763 | psql.return_value = True |
764 | redis.return_value = False | |
c682bdf4 IB |
765 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
766 | ||
767 | m.store_report() | |
768 | ||
769 | report.merge.assert_called_with(store.Portfolio.report) | |
770 | file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
771 | db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
1593c7a9 IB |
772 | redis_report.assert_not_called() |
773 | ||
774 | report.reset_mock() | |
775 | m = market.Market(self.ccxt, self.market_args(report_redis=False), | |
30700830 IB |
776 | user_id=1) |
777 | with self.subTest(redis_connected=True, report_redis=False),\ | |
778 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
779 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
1593c7a9 IB |
780 | mock.patch.object(m, "report") as report,\ |
781 | mock.patch.object(m, "store_file_report") as file_report,\ | |
782 | mock.patch.object(m, "store_redis_report") as redis_report,\ | |
783 | mock.patch.object(m, "store_database_report") as db_report,\ | |
784 | mock.patch.object(market.datetime, "datetime") as time_mock: | |
30700830 IB |
785 | psql.return_value = False |
786 | redis.return_value = True | |
1593c7a9 IB |
787 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
788 | ||
789 | m.store_report() | |
790 | redis_report.assert_not_called() | |
791 | ||
792 | report.reset_mock() | |
793 | m = market.Market(self.ccxt, self.market_args(report_redis=True), | |
794 | user_id=1) | |
30700830 IB |
795 | with self.subTest(redis_connected=False, report_redis=True),\ |
796 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
797 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
1593c7a9 IB |
798 | mock.patch.object(m, "report") as report,\ |
799 | mock.patch.object(m, "store_file_report") as file_report,\ | |
800 | mock.patch.object(m, "store_redis_report") as redis_report,\ | |
801 | mock.patch.object(m, "store_database_report") as db_report,\ | |
802 | mock.patch.object(market.datetime, "datetime") as time_mock: | |
30700830 IB |
803 | psql.return_value = False |
804 | redis.return_value = False | |
1593c7a9 IB |
805 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
806 | ||
807 | m.store_report() | |
808 | redis_report.assert_not_called() | |
809 | ||
810 | report.reset_mock() | |
811 | m = market.Market(self.ccxt, self.market_args(report_redis=True), | |
30700830 IB |
812 | user_id=1) |
813 | with self.subTest(redis_connected=True, report_redis=True),\ | |
814 | mock.patch.object(dbs, "psql_connected") as psql,\ | |
815 | mock.patch.object(dbs, "redis_connected") as redis,\ | |
1593c7a9 IB |
816 | mock.patch.object(m, "report") as report,\ |
817 | mock.patch.object(m, "store_file_report") as file_report,\ | |
818 | mock.patch.object(m, "store_redis_report") as redis_report,\ | |
819 | mock.patch.object(m, "store_database_report") as db_report,\ | |
820 | mock.patch.object(market.datetime, "datetime") as time_mock: | |
30700830 IB |
821 | psql.return_value = False |
822 | redis.return_value = True | |
1593c7a9 IB |
823 | time_mock.now.return_value = datetime.datetime(2018, 2, 25) |
824 | ||
825 | m.store_report() | |
826 | redis_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) | |
c682bdf4 | 827 | |
ceb7fc4c | 828 | def test_print_tickers(self): |
c682bdf4 IB |
829 | m = market.Market(self.ccxt, self.market_args()) |
830 | ||
831 | with mock.patch.object(m.balances, "in_currency") as in_currency,\ | |
832 | mock.patch.object(m.report, "log_stage") as log_stage,\ | |
833 | mock.patch.object(m.balances, "fetch_balances") as fetch_balances,\ | |
834 | mock.patch.object(m.report, "print_log") as print_log: | |
835 | ||
836 | in_currency.return_value = { | |
837 | "BTC": portfolio.Amount("BTC", "0.65"), | |
838 | "ETH": portfolio.Amount("BTC", "0.3"), | |
839 | } | |
840 | ||
ceb7fc4c | 841 | m.print_tickers() |
c682bdf4 | 842 | |
c682bdf4 IB |
843 | print_log.assert_has_calls([ |
844 | mock.call("total:"), | |
845 | mock.call(portfolio.Amount("BTC", "0.95")), | |
846 | ]) | |
847 | ||
848 | @mock.patch("market.Processor.process") | |
849 | @mock.patch("market.ReportStore.log_error") | |
850 | @mock.patch("market.Market.store_report") | |
851 | def test_process(self, store_report, log_error, process): | |
852 | m = market.Market(self.ccxt, self.market_args()) | |
ceb7fc4c IB |
853 | with self.subTest(actions=[], before=False, after=False): |
854 | m.process([]) | |
c682bdf4 IB |
855 | |
856 | process.assert_not_called() | |
857 | store_report.assert_called_once() | |
858 | log_error.assert_not_called() | |
859 | ||
860 | process.reset_mock() | |
861 | log_error.reset_mock() | |
862 | store_report.reset_mock() | |
863 | with self.subTest(before=True, after=False): | |
ceb7fc4c | 864 | m.process(["foo"], before=True) |
c682bdf4 | 865 | |
ceb7fc4c | 866 | process.assert_called_once_with("foo", steps="before") |
c682bdf4 IB |
867 | store_report.assert_called_once() |
868 | log_error.assert_not_called() | |
869 | ||
870 | process.reset_mock() | |
871 | log_error.reset_mock() | |
872 | store_report.reset_mock() | |
873 | with self.subTest(before=False, after=True): | |
ceb7fc4c | 874 | m.process(["sell_all"], after=True) |
c682bdf4 IB |
875 | |
876 | process.assert_called_once_with("sell_all", steps="after") | |
877 | store_report.assert_called_once() | |
878 | log_error.assert_not_called() | |
879 | ||
880 | process.reset_mock() | |
881 | log_error.reset_mock() | |
882 | store_report.reset_mock() | |
ceb7fc4c IB |
883 | with self.subTest(before=False, after=False): |
884 | m.process(["foo"]) | |
c682bdf4 | 885 | |
ceb7fc4c | 886 | process.assert_called_once_with("foo", steps="all") |
c682bdf4 IB |
887 | store_report.assert_called_once() |
888 | log_error.assert_not_called() | |
889 | ||
890 | process.reset_mock() | |
891 | log_error.reset_mock() | |
892 | store_report.reset_mock() | |
ceb7fc4c IB |
893 | with self.subTest(before=True, after=True): |
894 | m.process(["sell_all"], before=True, after=True) | |
c682bdf4 | 895 | |
ceb7fc4c | 896 | process.assert_called_once_with("sell_all", steps="all") |
c682bdf4 | 897 | store_report.assert_called_once() |
c682bdf4 | 898 | log_error.assert_not_called() |
c682bdf4 | 899 | |
5321200c IB |
900 | process.reset_mock() |
901 | log_error.reset_mock() | |
902 | store_report.reset_mock() | |
903 | with self.subTest(authentication_error=True): | |
904 | m.ccxt.check_required_credentials.side_effect = market.ccxt.AuthenticationError | |
905 | ||
906 | m.process(["some_action"], before=True) | |
907 | log_error.assert_called_with("market_authentication", message="Impossible to authenticate to market") | |
908 | store_report.assert_called_once() | |
909 | ||
910 | m.ccxt.check_required_credentials.side_effect = True | |
ceb7fc4c | 911 | process.reset_mock() |
c682bdf4 IB |
912 | log_error.reset_mock() |
913 | store_report.reset_mock() | |
914 | with self.subTest(unhandled_exception=True): | |
915 | process.side_effect = Exception("bouh") | |
916 | ||
ceb7fc4c | 917 | m.process(["some_action"], before=True) |
4ae84fb7 | 918 | log_error.assert_called_with("market_process", exception=mock.ANY, message=mock.ANY) |
c682bdf4 IB |
919 | store_report.assert_called_once() |
920 | ||
921 | ||
3080f31d | 922 | @unittest.skipUnless("unit" in limits, "Unit skipped") |
c682bdf4 IB |
923 | class ProcessorTest(WebMockTestCase): |
924 | def test_values(self): | |
925 | processor = market.Processor(self.m) | |
926 | ||
927 | self.assertEqual(self.m, processor.market) | |
928 | ||
929 | def test_run_action(self): | |
930 | processor = market.Processor(self.m) | |
931 | ||
932 | with mock.patch.object(processor, "parse_args") as parse_args: | |
933 | method_mock = mock.Mock() | |
934 | parse_args.return_value = [method_mock, { "foo": "bar" }] | |
935 | ||
936 | processor.run_action("foo", "bar", "baz") | |
937 | ||
938 | parse_args.assert_called_with("foo", "bar", "baz") | |
939 | ||
940 | method_mock.assert_called_with(foo="bar") | |
941 | ||
942 | processor.run_action("wait_for_recent", "bar", "baz") | |
943 | ||
944 | method_mock.assert_called_with(foo="bar") | |
945 | ||
946 | def test_select_step(self): | |
947 | processor = market.Processor(self.m) | |
948 | ||
949 | scenario = processor.scenarios["sell_all"] | |
950 | ||
951 | self.assertEqual(scenario, processor.select_steps(scenario, "all")) | |
952 | self.assertEqual(["all_sell"], list(map(lambda x: x["name"], processor.select_steps(scenario, "before")))) | |
953 | self.assertEqual(["wait", "all_buy"], list(map(lambda x: x["name"], processor.select_steps(scenario, "after")))) | |
954 | self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, 2)))) | |
955 | self.assertEqual(["wait"], list(map(lambda x: x["name"], processor.select_steps(scenario, "wait")))) | |
956 | ||
957 | with self.assertRaises(TypeError): | |
958 | processor.select_steps(scenario, ["wait"]) | |
959 | ||
ceb7fc4c IB |
960 | def test_can_process(self): |
961 | processor = market.Processor(self.m) | |
962 | ||
963 | with self.subTest(True): | |
964 | self.assertTrue(processor.can_process("sell_all")) | |
965 | ||
966 | with self.subTest(False): | |
967 | self.assertFalse(processor.can_process("unknown_action")) | |
968 | ||
c682bdf4 IB |
969 | @mock.patch("market.Processor.process_step") |
970 | def test_process(self, process_step): | |
ceb7fc4c IB |
971 | with self.subTest("unknown action"): |
972 | processor = market.Processor(self.m) | |
973 | with self.assertRaises(TypeError): | |
974 | processor.process("unknown_action") | |
975 | ||
976 | with self.subTest("nominal case"): | |
977 | processor = market.Processor(self.m) | |
c682bdf4 | 978 | |
ceb7fc4c IB |
979 | processor.process("sell_all", foo="bar") |
980 | self.assertEqual(3, process_step.call_count) | |
c682bdf4 | 981 | |
ceb7fc4c IB |
982 | steps = list(map(lambda x: x[1][1]["name"], process_step.mock_calls)) |
983 | scenario_names = list(map(lambda x: x[1][0], process_step.mock_calls)) | |
984 | kwargs = list(map(lambda x: x[1][2], process_step.mock_calls)) | |
985 | self.assertEqual(["all_sell", "wait", "all_buy"], steps) | |
986 | self.assertEqual(["sell_all", "sell_all", "sell_all"], scenario_names) | |
987 | self.assertEqual([{"foo":"bar"}, {"foo":"bar"}, {"foo":"bar"}], kwargs) | |
c682bdf4 | 988 | |
ceb7fc4c | 989 | process_step.reset_mock() |
c682bdf4 | 990 | |
ceb7fc4c | 991 | processor.process("sell_needed", steps=["before", "after"]) |
bb127bc8 | 992 | self.assertEqual(4, process_step.call_count) |
c682bdf4 IB |
993 | |
994 | def test_method_arguments(self): | |
995 | ccxt = mock.Mock(spec=market.ccxt.poloniexE) | |
996 | m = market.Market(ccxt, self.market_args()) | |
997 | ||
998 | processor = market.Processor(m) | |
999 | ||
1000 | method, arguments = processor.method_arguments("wait_for_recent") | |
1001 | self.assertEqual(market.Portfolio.wait_for_recent, method) | |
1002 | self.assertEqual(["delta", "poll"], arguments) | |
1003 | ||
1004 | method, arguments = processor.method_arguments("prepare_trades") | |
1005 | self.assertEqual(m.prepare_trades, method) | |
96959cea | 1006 | self.assertEqual(['base_currency', 'liquidity', 'compute_value', 'repartition', 'only', 'available_balance_only'], arguments) |
c682bdf4 IB |
1007 | |
1008 | method, arguments = processor.method_arguments("prepare_orders") | |
1009 | self.assertEqual(m.trades.prepare_orders, method) | |
1010 | ||
1011 | method, arguments = processor.method_arguments("move_balances") | |
1012 | self.assertEqual(m.move_balances, method) | |
1013 | ||
1014 | method, arguments = processor.method_arguments("run_orders") | |
1015 | self.assertEqual(m.trades.run_orders, method) | |
1016 | ||
1017 | method, arguments = processor.method_arguments("follow_orders") | |
1018 | self.assertEqual(m.follow_orders, method) | |
1019 | ||
1020 | method, arguments = processor.method_arguments("close_trades") | |
1021 | self.assertEqual(m.trades.close_trades, method) | |
1022 | ||
ceb7fc4c IB |
1023 | method, arguments = processor.method_arguments("print_tickers") |
1024 | self.assertEqual(m.print_tickers, method) | |
1025 | ||
c682bdf4 IB |
1026 | def test_process_step(self): |
1027 | processor = market.Processor(self.m) | |
1028 | ||
1029 | with mock.patch.object(processor, "run_action") as run_action: | |
bb127bc8 | 1030 | step = processor.scenarios["sell_needed"][2] |
c682bdf4 IB |
1031 | |
1032 | processor.process_step("foo", step, {"foo":"bar"}) | |
1033 | ||
1034 | self.m.report.log_stage.assert_has_calls([ | |
bb127bc8 IB |
1035 | mock.call("process_foo__2_sell_begin"), |
1036 | mock.call("process_foo__2_sell_end"), | |
c682bdf4 IB |
1037 | ]) |
1038 | self.m.balances.fetch_balances.assert_has_calls([ | |
bb127bc8 IB |
1039 | mock.call(tag="process_foo__2_sell_begin"), |
1040 | mock.call(tag="process_foo__2_sell_end"), | |
c682bdf4 IB |
1041 | ]) |
1042 | ||
1043 | self.assertEqual(5, run_action.call_count) | |
1044 | ||
1045 | run_action.assert_has_calls([ | |
1046 | mock.call('prepare_trades', {}, {'foo': 'bar'}), | |
1047 | mock.call('prepare_orders', {'only': 'dispose', 'compute_value': 'average'}, {'foo': 'bar'}), | |
1048 | mock.call('run_orders', {}, {'foo': 'bar'}), | |
1049 | mock.call('follow_orders', {}, {'foo': 'bar'}), | |
1050 | mock.call('close_trades', {}, {'foo': 'bar'}), | |
1051 | ]) | |
1052 | ||
1053 | self.m.reset_mock() | |
1054 | with mock.patch.object(processor, "run_action") as run_action: | |
1055 | step = processor.scenarios["sell_needed"][0] | |
1056 | ||
bb127bc8 IB |
1057 | processor.process_step("foo", step, {"foo":"bar"}) |
1058 | ||
1059 | self.m.report.log_stage.assert_has_calls([ | |
1060 | mock.call("process_foo__0_print_balances_begin"), | |
1061 | mock.call("process_foo__0_print_balances_end"), | |
1062 | ]) | |
1063 | self.m.balances.fetch_balances.assert_has_calls([ | |
1064 | mock.call(add_portfolio=True, checkpoint='end', | |
1065 | log_tickers=True, | |
3a15ffc7 | 1066 | add_usdt=True, |
bb127bc8 IB |
1067 | tag='process_foo__0_print_balances_begin') |
1068 | ]) | |
1069 | ||
1070 | self.assertEqual(0, run_action.call_count) | |
1071 | ||
1072 | self.m.reset_mock() | |
1073 | with mock.patch.object(processor, "run_action") as run_action: | |
1074 | step = processor.scenarios["sell_needed"][1] | |
1075 | ||
c682bdf4 IB |
1076 | processor.process_step("foo", step, {"foo":"bar"}) |
1077 | self.m.balances.fetch_balances.assert_not_called() | |
1078 | ||
9b697863 IB |
1079 | self.m.reset_mock() |
1080 | with mock.patch.object(processor, "run_action") as run_action: | |
1081 | step = processor.scenarios["print_balances"][0] | |
1082 | ||
1083 | processor.process_step("foo", step, {"foo":"bar"}) | |
1084 | self.m.balances.fetch_balances.assert_called_once_with( | |
3a15ffc7 | 1085 | add_portfolio=True, add_usdt=True, log_tickers=True, |
9b697863 IB |
1086 | tag='process_foo__1_print_balances_begin') |
1087 | ||
c682bdf4 IB |
1088 | def test_parse_args(self): |
1089 | processor = market.Processor(self.m) | |
1090 | ||
1091 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
1092 | method_mock = mock.Mock() | |
1093 | method_arguments.return_value = [ | |
1094 | method_mock, | |
1095 | ["foo2", "foo"] | |
1096 | ] | |
1097 | method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"}) | |
1098 | ||
1099 | self.assertEqual(method_mock, method) | |
1100 | self.assertEqual({"foo": "bar2", "foo2": "bar"}, args) | |
1101 | ||
1102 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
1103 | method_mock = mock.Mock() | |
1104 | method_arguments.return_value = [ | |
1105 | method_mock, | |
1106 | ["repartition"] | |
1107 | ] | |
1108 | method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {}) | |
1109 | ||
1110 | self.assertEqual(1, len(args["repartition"])) | |
1111 | self.assertIn("BTC", args["repartition"]) | |
1112 | ||
1113 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
1114 | method_mock = mock.Mock() | |
1115 | method_arguments.return_value = [ | |
1116 | method_mock, | |
1117 | ["repartition", "base_currency"] | |
1118 | ] | |
1119 | method, args = processor.parse_args("action", {"repartition": { "base_currency": 1 }}, {"base_currency": "USDT"}) | |
1120 | ||
1121 | self.assertEqual(1, len(args["repartition"])) | |
1122 | self.assertIn("USDT", args["repartition"]) | |
1123 | ||
1124 | with mock.patch.object(processor, "method_arguments") as method_arguments: | |
1125 | method_mock = mock.Mock() | |
1126 | method_arguments.return_value = [ | |
1127 | method_mock, | |
1128 | ["repartition", "base_currency"] | |
1129 | ] | |
1130 | method, args = processor.parse_args("action", {"repartition": { "ETH": 1 }}, {"base_currency": "USDT"}) | |
1131 | ||
1132 | self.assertEqual(1, len(args["repartition"])) | |
1133 | self.assertIn("ETH", args["repartition"]) | |
1134 | ||
1135 |