]>
Commit | Line | Data |
---|---|---|
1 | from .helper import * | |
2 | import requests | |
3 | import datetime | |
4 | import threading | |
5 | import market, portfolio, store | |
6 | ||
7 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
8 | class NoopLockTest(unittest.TestCase): | |
9 | def test_with(self): | |
10 | noop_lock = store.NoopLock() | |
11 | with noop_lock: | |
12 | self.assertTrue(True) | |
13 | ||
14 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
15 | class LockedVarTest(unittest.TestCase): | |
16 | ||
17 | def test_values(self): | |
18 | locked_var = store.LockedVar("Foo") | |
19 | self.assertIsInstance(locked_var.lock, store.NoopLock) | |
20 | self.assertEqual("Foo", locked_var.val) | |
21 | ||
22 | def test_get(self): | |
23 | with self.subTest(desc="Normal case"): | |
24 | locked_var = store.LockedVar("Foo") | |
25 | self.assertEqual("Foo", locked_var.get()) | |
26 | with self.subTest(desc="Dict"): | |
27 | locked_var = store.LockedVar({"foo": "bar"}) | |
28 | self.assertEqual({"foo": "bar"}, locked_var.get()) | |
29 | self.assertEqual("bar", locked_var.get("foo")) | |
30 | self.assertIsNone(locked_var.get("other")) | |
31 | ||
32 | def test_set(self): | |
33 | locked_var = store.LockedVar("Foo") | |
34 | locked_var.set("Bar") | |
35 | self.assertEqual("Bar", locked_var.get()) | |
36 | ||
37 | def test__getattr(self): | |
38 | dummy = type('Dummy', (object,), {})() | |
39 | dummy.attribute = "Hey" | |
40 | ||
41 | locked_var = store.LockedVar(dummy) | |
42 | self.assertEqual("Hey", locked_var.attribute) | |
43 | with self.assertRaises(AttributeError): | |
44 | locked_var.other | |
45 | ||
46 | def test_start_lock(self): | |
47 | locked_var = store.LockedVar("Foo") | |
48 | locked_var.start_lock() | |
49 | self.assertEqual("lock", locked_var.lock.__class__.__name__) | |
50 | ||
51 | thread1 = threading.Thread(target=locked_var.set, args=["Bar1"]) | |
52 | thread2 = threading.Thread(target=locked_var.set, args=["Bar2"]) | |
53 | thread3 = threading.Thread(target=locked_var.set, args=["Bar3"]) | |
54 | ||
55 | with locked_var.lock: | |
56 | thread1.start() | |
57 | thread2.start() | |
58 | thread3.start() | |
59 | ||
60 | self.assertEqual("Foo", locked_var.val) | |
61 | thread1.join() | |
62 | thread2.join() | |
63 | thread3.join() | |
64 | self.assertEqual("Bar", locked_var.get()[0:3]) | |
65 | ||
66 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
67 | class TradeStoreTest(WebMockTestCase): | |
68 | def test_compute_trades(self): | |
69 | self.m.balances.currencies.return_value = ["XMR", "DASH", "XVG", "BTC", "ETH"] | |
70 | ||
71 | values_in_base = { | |
72 | "XMR": portfolio.Amount("BTC", D("0.9")), | |
73 | "DASH": portfolio.Amount("BTC", D("0.4")), | |
74 | "XVG": portfolio.Amount("BTC", D("-0.5")), | |
75 | "BTC": portfolio.Amount("BTC", D("0.5")), | |
76 | } | |
77 | new_repartition = { | |
78 | "DASH": portfolio.Amount("BTC", D("0.5")), | |
79 | "XVG": portfolio.Amount("BTC", D("0.1")), | |
80 | "BTC": portfolio.Amount("BTC", D("0.4")), | |
81 | "ETH": portfolio.Amount("BTC", D("0.3")), | |
82 | } | |
83 | side_effect = [ | |
84 | (True, 1), | |
85 | (False, 2), | |
86 | (False, 3), | |
87 | (True, 4), | |
88 | (True, 5) | |
89 | ] | |
90 | ||
91 | with mock.patch.object(market.TradeStore, "trade_if_matching") as trade_if_matching: | |
92 | trade_store = market.TradeStore(self.m) | |
93 | trade_if_matching.side_effect = side_effect | |
94 | ||
95 | trade_store.compute_trades(values_in_base, | |
96 | new_repartition, only="only") | |
97 | ||
98 | self.assertEqual(5, trade_if_matching.call_count) | |
99 | self.assertEqual(3, len(trade_store.all)) | |
100 | self.assertEqual([1, 4, 5], trade_store.all) | |
101 | self.m.report.log_trades.assert_called_with(side_effect, "only") | |
102 | ||
103 | def test_trade_if_matching(self): | |
104 | ||
105 | with self.subTest(only="nope"): | |
106 | trade_store = market.TradeStore(self.m) | |
107 | result = trade_store.trade_if_matching( | |
108 | portfolio.Amount("BTC", D("0")), | |
109 | portfolio.Amount("BTC", D("0.3")), | |
110 | "ETH", only="nope") | |
111 | self.assertEqual(False, result[0]) | |
112 | self.assertIsInstance(result[1], portfolio.Trade) | |
113 | ||
114 | with self.subTest(only=None): | |
115 | trade_store = market.TradeStore(self.m) | |
116 | result = trade_store.trade_if_matching( | |
117 | portfolio.Amount("BTC", D("0")), | |
118 | portfolio.Amount("BTC", D("0.3")), | |
119 | "ETH", only=None) | |
120 | self.assertEqual(True, result[0]) | |
121 | ||
122 | with self.subTest(only="acquire"): | |
123 | trade_store = market.TradeStore(self.m) | |
124 | result = trade_store.trade_if_matching( | |
125 | portfolio.Amount("BTC", D("0")), | |
126 | portfolio.Amount("BTC", D("0.3")), | |
127 | "ETH", only="acquire") | |
128 | self.assertEqual(True, result[0]) | |
129 | ||
130 | with self.subTest(only="dispose"): | |
131 | trade_store = market.TradeStore(self.m) | |
132 | result = trade_store.trade_if_matching( | |
133 | portfolio.Amount("BTC", D("0")), | |
134 | portfolio.Amount("BTC", D("0.3")), | |
135 | "ETH", only="dispose") | |
136 | self.assertEqual(False, result[0]) | |
137 | ||
138 | def test_prepare_orders(self): | |
139 | trade_store = market.TradeStore(self.m) | |
140 | ||
141 | trade_mock1 = mock.Mock() | |
142 | trade_mock2 = mock.Mock() | |
143 | trade_mock3 = mock.Mock() | |
144 | ||
145 | trade_mock1.prepare_order.return_value = 1 | |
146 | trade_mock2.prepare_order.return_value = 2 | |
147 | trade_mock3.prepare_order.return_value = 3 | |
148 | ||
149 | trade_mock1.pending = True | |
150 | trade_mock2.pending = True | |
151 | trade_mock3.pending = False | |
152 | ||
153 | trade_store.all.append(trade_mock1) | |
154 | trade_store.all.append(trade_mock2) | |
155 | trade_store.all.append(trade_mock3) | |
156 | ||
157 | trade_store.prepare_orders() | |
158 | trade_mock1.prepare_order.assert_called_with(compute_value="default") | |
159 | trade_mock2.prepare_order.assert_called_with(compute_value="default") | |
160 | trade_mock3.prepare_order.assert_not_called() | |
161 | self.m.report.log_orders.assert_called_once_with([1, 2], None, "default") | |
162 | ||
163 | self.m.report.log_orders.reset_mock() | |
164 | ||
165 | trade_store.prepare_orders(compute_value="bla") | |
166 | trade_mock1.prepare_order.assert_called_with(compute_value="bla") | |
167 | trade_mock2.prepare_order.assert_called_with(compute_value="bla") | |
168 | self.m.report.log_orders.assert_called_once_with([1, 2], None, "bla") | |
169 | ||
170 | trade_mock1.prepare_order.reset_mock() | |
171 | trade_mock2.prepare_order.reset_mock() | |
172 | self.m.report.log_orders.reset_mock() | |
173 | ||
174 | trade_mock1.action = "foo" | |
175 | trade_mock2.action = "bar" | |
176 | trade_store.prepare_orders(only="bar") | |
177 | trade_mock1.prepare_order.assert_not_called() | |
178 | trade_mock2.prepare_order.assert_called_with(compute_value="default") | |
179 | self.m.report.log_orders.assert_called_once_with([2], "bar", "default") | |
180 | ||
181 | def test_print_all_with_order(self): | |
182 | trade_mock1 = mock.Mock() | |
183 | trade_mock2 = mock.Mock() | |
184 | trade_mock3 = mock.Mock() | |
185 | trade_store = market.TradeStore(self.m) | |
186 | trade_store.all = [trade_mock1, trade_mock2, trade_mock3] | |
187 | ||
188 | trade_store.print_all_with_order() | |
189 | ||
190 | trade_mock1.print_with_order.assert_called() | |
191 | trade_mock2.print_with_order.assert_called() | |
192 | trade_mock3.print_with_order.assert_called() | |
193 | ||
194 | def test_run_orders(self): | |
195 | with mock.patch.object(market.TradeStore, "all_orders") as all_orders: | |
196 | order_mock1 = mock.Mock() | |
197 | order_mock2 = mock.Mock() | |
198 | order_mock3 = mock.Mock() | |
199 | trade_store = market.TradeStore(self.m) | |
200 | ||
201 | all_orders.return_value = [order_mock1, order_mock2, order_mock3] | |
202 | ||
203 | trade_store.run_orders() | |
204 | ||
205 | all_orders.assert_called_with(state="pending") | |
206 | ||
207 | order_mock1.run.assert_called() | |
208 | order_mock2.run.assert_called() | |
209 | order_mock3.run.assert_called() | |
210 | ||
211 | self.m.report.log_stage.assert_called_with("run_orders") | |
212 | self.m.report.log_orders.assert_called_with([order_mock1, order_mock2, | |
213 | order_mock3]) | |
214 | ||
215 | def test_all_orders(self): | |
216 | trade_mock1 = mock.Mock() | |
217 | trade_mock2 = mock.Mock() | |
218 | ||
219 | order_mock1 = mock.Mock() | |
220 | order_mock2 = mock.Mock() | |
221 | order_mock3 = mock.Mock() | |
222 | ||
223 | trade_mock1.orders = [order_mock1, order_mock2] | |
224 | trade_mock2.orders = [order_mock3] | |
225 | ||
226 | order_mock1.status = "pending" | |
227 | order_mock2.status = "open" | |
228 | order_mock3.status = "open" | |
229 | ||
230 | trade_store = market.TradeStore(self.m) | |
231 | trade_store.all.append(trade_mock1) | |
232 | trade_store.all.append(trade_mock2) | |
233 | ||
234 | orders = trade_store.all_orders() | |
235 | self.assertEqual(3, len(orders)) | |
236 | ||
237 | open_orders = trade_store.all_orders(state="open") | |
238 | self.assertEqual(2, len(open_orders)) | |
239 | self.assertEqual([order_mock2, order_mock3], open_orders) | |
240 | ||
241 | def test_update_all_orders_status(self): | |
242 | with mock.patch.object(market.TradeStore, "all_orders") as all_orders: | |
243 | order_mock1 = mock.Mock() | |
244 | order_mock2 = mock.Mock() | |
245 | order_mock3 = mock.Mock() | |
246 | ||
247 | all_orders.return_value = [order_mock1, order_mock2, order_mock3] | |
248 | ||
249 | trade_store = market.TradeStore(self.m) | |
250 | ||
251 | trade_store.update_all_orders_status() | |
252 | all_orders.assert_called_with(state="open") | |
253 | ||
254 | order_mock1.get_status.assert_called() | |
255 | order_mock2.get_status.assert_called() | |
256 | order_mock3.get_status.assert_called() | |
257 | ||
258 | def test_close_trades(self): | |
259 | trade_mock1 = mock.Mock() | |
260 | trade_mock2 = mock.Mock() | |
261 | trade_mock3 = mock.Mock() | |
262 | ||
263 | trade_store = market.TradeStore(self.m) | |
264 | ||
265 | trade_store.all.append(trade_mock1) | |
266 | trade_store.all.append(trade_mock2) | |
267 | trade_store.all.append(trade_mock3) | |
268 | ||
269 | trade_store.close_trades() | |
270 | ||
271 | trade_mock1.close.assert_called_once_with() | |
272 | trade_mock2.close.assert_called_once_with() | |
273 | trade_mock3.close.assert_called_once_with() | |
274 | ||
275 | def test_pending(self): | |
276 | trade_mock1 = mock.Mock() | |
277 | trade_mock1.pending = True | |
278 | trade_mock2 = mock.Mock() | |
279 | trade_mock2.pending = True | |
280 | trade_mock3 = mock.Mock() | |
281 | trade_mock3.pending = False | |
282 | ||
283 | trade_store = market.TradeStore(self.m) | |
284 | ||
285 | trade_store.all.append(trade_mock1) | |
286 | trade_store.all.append(trade_mock2) | |
287 | trade_store.all.append(trade_mock3) | |
288 | ||
289 | self.assertEqual([trade_mock1, trade_mock2], trade_store.pending) | |
290 | ||
291 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
292 | class BalanceStoreTest(WebMockTestCase): | |
293 | def setUp(self): | |
294 | super().setUp() | |
295 | ||
296 | self.fetch_balance = { | |
297 | "ETC": { | |
298 | "exchange_free": 0, | |
299 | "exchange_used": 0, | |
300 | "exchange_total": 0, | |
301 | "margin_total": 0, | |
302 | }, | |
303 | "USDT": { | |
304 | "exchange_free": D("6.0"), | |
305 | "exchange_used": D("1.2"), | |
306 | "exchange_total": D("7.2"), | |
307 | "margin_total": 0, | |
308 | }, | |
309 | "XVG": { | |
310 | "exchange_free": 16, | |
311 | "exchange_used": 0, | |
312 | "exchange_total": 16, | |
313 | "margin_total": 0, | |
314 | }, | |
315 | "XMR": { | |
316 | "exchange_free": 0, | |
317 | "exchange_used": 0, | |
318 | "exchange_total": 0, | |
319 | "margin_total": D("-1.0"), | |
320 | "margin_free": 0, | |
321 | }, | |
322 | } | |
323 | ||
324 | def test_in_currency(self): | |
325 | self.m.get_ticker.return_value = { | |
326 | "bid": D("0.09"), | |
327 | "ask": D("0.11"), | |
328 | "average": D("0.1"), | |
329 | } | |
330 | ||
331 | balance_store = market.BalanceStore(self.m) | |
332 | balance_store.all = { | |
333 | "BTC": portfolio.Balance("BTC", { | |
334 | "total": "0.65", | |
335 | "exchange_total":"0.65", | |
336 | "exchange_free": "0.35", | |
337 | "exchange_used": "0.30"}), | |
338 | "ETH": portfolio.Balance("ETH", { | |
339 | "total": 3, | |
340 | "exchange_total": 3, | |
341 | "exchange_free": 3, | |
342 | "exchange_used": 0}), | |
343 | } | |
344 | ||
345 | amounts = balance_store.in_currency("BTC") | |
346 | self.assertEqual("BTC", amounts["ETH"].currency) | |
347 | self.assertEqual(D("0.65"), amounts["BTC"].value) | |
348 | self.assertEqual(D("0.30"), amounts["ETH"].value) | |
349 | self.m.report.log_tickers.assert_called_once_with(amounts, "BTC", | |
350 | "average", "total") | |
351 | self.m.report.log_tickers.reset_mock() | |
352 | ||
353 | amounts = balance_store.in_currency("BTC", compute_value="bid") | |
354 | self.assertEqual(D("0.65"), amounts["BTC"].value) | |
355 | self.assertEqual(D("0.27"), amounts["ETH"].value) | |
356 | self.m.report.log_tickers.assert_called_once_with(amounts, "BTC", | |
357 | "bid", "total") | |
358 | self.m.report.log_tickers.reset_mock() | |
359 | ||
360 | amounts = balance_store.in_currency("BTC", compute_value="bid", type="exchange_used") | |
361 | self.assertEqual(D("0.30"), amounts["BTC"].value) | |
362 | self.assertEqual(0, amounts["ETH"].value) | |
363 | self.m.report.log_tickers.assert_called_once_with(amounts, "BTC", | |
364 | "bid", "exchange_used") | |
365 | self.m.report.log_tickers.reset_mock() | |
366 | ||
367 | def test_fetch_balances(self): | |
368 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance | |
369 | ||
370 | balance_store = market.BalanceStore(self.m) | |
371 | ||
372 | with self.subTest(log_tickers=False): | |
373 | balance_store.fetch_balances() | |
374 | self.assertNotIn("ETC", balance_store.currencies()) | |
375 | self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies())) | |
376 | ||
377 | balance_store.all["ETC"] = portfolio.Balance("ETC", { | |
378 | "exchange_total": "1", "exchange_free": "0", | |
379 | "exchange_used": "1" }) | |
380 | balance_store.fetch_balances(tag="foo") | |
381 | self.assertEqual(0, balance_store.all["ETC"].total) | |
382 | self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies())) | |
383 | self.m.report.log_balances.assert_called_with(tag="foo", checkpoint=None) | |
384 | ||
385 | with self.subTest(log_tickers=True),\ | |
386 | mock.patch.object(balance_store, "in_currency") as in_currency: | |
387 | in_currency.return_value = "tickers" | |
388 | balance_store.fetch_balances(log_tickers=True, ticker_currency="FOO", | |
389 | ticker_compute_value="compute", ticker_type="type") | |
390 | self.m.report.log_balances.assert_called_with(compute_value='compute', | |
391 | tag=None, checkpoint=None, ticker_currency='FOO', tickers='tickers', | |
392 | type='type') | |
393 | ||
394 | balance_store = market.BalanceStore(self.m) | |
395 | with self.subTest(add_portfolio=True),\ | |
396 | mock.patch.object(market.Portfolio, "repartition") as repartition: | |
397 | repartition.return_value = { | |
398 | "DOGE": D("0.5"), | |
399 | "USDT": D("0.5"), | |
400 | } | |
401 | balance_store.fetch_balances(add_portfolio=True) | |
402 | self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies())) | |
403 | ||
404 | self.m.ccxt.fetch_all_balances.return_value = { | |
405 | "ETC": { | |
406 | "exchange_free": 0, | |
407 | "exchange_used": 0, | |
408 | "exchange_total": 0, | |
409 | "margin_total": 0, | |
410 | }, | |
411 | "XVG": { | |
412 | "exchange_free": 16, | |
413 | "exchange_used": 0, | |
414 | "exchange_total": 16, | |
415 | "margin_total": 0, | |
416 | }, | |
417 | "XMR": { | |
418 | "exchange_free": 0, | |
419 | "exchange_used": 0, | |
420 | "exchange_total": 0, | |
421 | "margin_total": D("-1.0"), | |
422 | "margin_free": 0, | |
423 | }, | |
424 | } | |
425 | ||
426 | balance_store = market.BalanceStore(self.m) | |
427 | with self.subTest(add_usdt=True),\ | |
428 | mock.patch.object(market.Portfolio, "repartition") as repartition: | |
429 | repartition.return_value = { | |
430 | "DOGE": D("0.5"), | |
431 | "ETH": D("0.5"), | |
432 | } | |
433 | balance_store.fetch_balances(add_usdt=True) | |
434 | self.assertListEqual(["XVG", "XMR", "USDT"], list(balance_store.currencies())) | |
435 | ||
436 | @mock.patch.object(market.Portfolio, "repartition") | |
437 | def test_dispatch_assets(self, repartition): | |
438 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance | |
439 | ||
440 | balance_store = market.BalanceStore(self.m) | |
441 | balance_store.fetch_balances() | |
442 | ||
443 | self.assertNotIn("XEM", balance_store.currencies()) | |
444 | ||
445 | repartition_hash = { | |
446 | "XEM": (D("0.75"), "long"), | |
447 | "BTC": (D("0.26"), "long"), | |
448 | "DASH": (D("0.10"), "short"), | |
449 | } | |
450 | repartition.return_value = repartition_hash | |
451 | ||
452 | amounts = balance_store.dispatch_assets(portfolio.Amount("BTC", "11.1")) | |
453 | repartition.assert_called_with(liquidity="medium") | |
454 | self.assertIn("XEM", balance_store.currencies()) | |
455 | self.assertEqual(D("2.6"), amounts["BTC"].value) | |
456 | self.assertEqual(D("7.5"), amounts["XEM"].value) | |
457 | self.assertEqual(D("-1.0"), amounts["DASH"].value) | |
458 | self.m.report.log_balances.assert_called_with(tag=None, checkpoint=None) | |
459 | self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC", | |
460 | "11.1"), amounts, "medium", repartition_hash) | |
461 | ||
462 | def test_currencies(self): | |
463 | balance_store = market.BalanceStore(self.m) | |
464 | ||
465 | balance_store.all = { | |
466 | "BTC": portfolio.Balance("BTC", { | |
467 | "total": "0.65", | |
468 | "exchange_total":"0.65", | |
469 | "exchange_free": "0.35", | |
470 | "exchange_used": "0.30"}), | |
471 | "ETH": portfolio.Balance("ETH", { | |
472 | "total": 3, | |
473 | "exchange_total": 3, | |
474 | "exchange_free": 3, | |
475 | "exchange_used": 0}), | |
476 | } | |
477 | self.assertListEqual(["BTC", "ETH"], list(balance_store.currencies())) | |
478 | ||
479 | def test_as_json(self): | |
480 | balance_mock1 = mock.Mock() | |
481 | balance_mock1.as_json.return_value = 1 | |
482 | ||
483 | balance_mock2 = mock.Mock() | |
484 | balance_mock2.as_json.return_value = 2 | |
485 | ||
486 | balance_store = market.BalanceStore(self.m) | |
487 | balance_store.all = { | |
488 | "BTC": balance_mock1, | |
489 | "ETH": balance_mock2, | |
490 | } | |
491 | ||
492 | as_json = balance_store.as_json() | |
493 | self.assertEqual(1, as_json["BTC"]) | |
494 | self.assertEqual(2, as_json["ETH"]) | |
495 | ||
496 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
497 | class ReportStoreTest(WebMockTestCase): | |
498 | def test_add_log(self): | |
499 | with self.subTest(market=self.m): | |
500 | self.m.user_id = 1 | |
501 | self.m.market_id = 3 | |
502 | report_store = market.ReportStore(self.m) | |
503 | result = report_store.add_log({"foo": "bar"}) | |
504 | ||
505 | self.assertEqual({"foo": "bar", "date": mock.ANY, "user_id": 1, "market_id": 3}, result) | |
506 | self.assertEqual(result, report_store.logs[0]) | |
507 | ||
508 | with self.subTest(market=None): | |
509 | report_store = market.ReportStore(None) | |
510 | result = report_store.add_log({"foo": "bar"}) | |
511 | ||
512 | self.assertEqual({"foo": "bar", "date": mock.ANY, "user_id": None, "market_id": None}, result) | |
513 | ||
514 | def test_add_redis_status(self): | |
515 | report_store = market.ReportStore(self.m) | |
516 | result = report_store.add_redis_status({"foo": "bar"}) | |
517 | ||
518 | self.assertEqual({"foo": "bar"}, result) | |
519 | self.assertEqual(result, report_store.redis_status[0]) | |
520 | ||
521 | def test_set_verbose(self): | |
522 | report_store = market.ReportStore(self.m) | |
523 | with self.subTest(verbose=True): | |
524 | report_store.set_verbose(True) | |
525 | self.assertTrue(report_store.verbose_print) | |
526 | ||
527 | with self.subTest(verbose=False): | |
528 | report_store.set_verbose(False) | |
529 | self.assertFalse(report_store.verbose_print) | |
530 | ||
531 | def test_merge(self): | |
532 | self.m.user_id = 1 | |
533 | self.m.market_id = 3 | |
534 | report_store1 = market.ReportStore(self.m, verbose_print=False) | |
535 | report_store2 = market.ReportStore(None, verbose_print=False) | |
536 | ||
537 | report_store2.log_stage("1") | |
538 | report_store1.log_stage("2") | |
539 | report_store2.log_stage("3") | |
540 | ||
541 | report_store1.merge(report_store2) | |
542 | ||
543 | self.assertEqual(3, len(report_store1.logs)) | |
544 | self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs))) | |
545 | self.assertEqual(6, len(report_store1.print_logs)) | |
546 | ||
547 | def test_print_log(self): | |
548 | report_store = market.ReportStore(self.m) | |
549 | with self.subTest(verbose=True),\ | |
550 | mock.patch.object(store, "datetime") as time_mock,\ | |
551 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
552 | time_mock.datetime.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10) | |
553 | report_store.set_verbose(True) | |
554 | report_store.print_log("Coucou") | |
555 | report_store.print_log(portfolio.Amount("BTC", 1)) | |
556 | self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n") | |
557 | ||
558 | with self.subTest(verbose=False),\ | |
559 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | |
560 | report_store.set_verbose(False) | |
561 | report_store.print_log("Coucou") | |
562 | report_store.print_log(portfolio.Amount("BTC", 1)) | |
563 | self.assertEqual(stdout_mock.getvalue(), "") | |
564 | ||
565 | def test_default_json_serial(self): | |
566 | report_store = market.ReportStore(self.m) | |
567 | ||
568 | self.assertEqual("2018-02-24T00:00:00", | |
569 | report_store.default_json_serial(portfolio.datetime.datetime(2018, 2, 24))) | |
570 | self.assertEqual("1.00000000 BTC", | |
571 | report_store.default_json_serial(portfolio.Amount("BTC", 1))) | |
572 | ||
573 | def test_to_json(self): | |
574 | report_store = market.ReportStore(self.m) | |
575 | report_store.logs.append({"foo": "bar"}) | |
576 | self.assertEqual('[\n {\n "foo": "bar"\n }\n]', report_store.to_json()) | |
577 | report_store.logs.append({"date": portfolio.datetime.datetime(2018, 2, 24)}) | |
578 | self.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n }\n]', report_store.to_json()) | |
579 | report_store.logs.append({"amount": portfolio.Amount("BTC", 1)}) | |
580 | self.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n },\n {\n "amount": "1.00000000 BTC"\n }\n]', report_store.to_json()) | |
581 | ||
582 | def test_to_json_array(self): | |
583 | report_store = market.ReportStore(self.m) | |
584 | report_store.logs.append({ | |
585 | "date": "date1", "type": "type1", "foo": "bar", "bla": "bla" | |
586 | }) | |
587 | report_store.logs.append({ | |
588 | "date": "date2", "type": "type2", "foo": "bar", "bla": "bla" | |
589 | }) | |
590 | logs = list(report_store.to_json_array()) | |
591 | ||
592 | self.assertEqual(2, len(logs)) | |
593 | self.assertEqual(("date1", "type1", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[0]) | |
594 | self.assertEqual(("date2", "type2", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[1]) | |
595 | ||
596 | def test_to_json_redis(self): | |
597 | report_store = market.ReportStore(self.m) | |
598 | report_store.redis_status.append({ | |
599 | "type": "type1", "foo": "bar", "bla": "bla" | |
600 | }) | |
601 | report_store.redis_status.append({ | |
602 | "type": "type2", "foo": "bar", "bla": "bla" | |
603 | }) | |
604 | logs = list(report_store.to_json_redis()) | |
605 | ||
606 | self.assertEqual(2, len(logs)) | |
607 | self.assertEqual(("type1", '{"foo": "bar", "bla": "bla"}'), logs[0]) | |
608 | self.assertEqual(("type2", '{"foo": "bar", "bla": "bla"}'), logs[1]) | |
609 | ||
610 | @mock.patch.object(market.ReportStore, "print_log") | |
611 | @mock.patch.object(market.ReportStore, "add_log") | |
612 | def test_log_stage(self, add_log, print_log): | |
613 | report_store = market.ReportStore(self.m) | |
614 | c = lambda x: x | |
615 | report_store.log_stage("foo", bar="baz", c=c, d=portfolio.Amount("BTC", 1)) | |
616 | print_log.assert_has_calls([ | |
617 | mock.call("-----------"), | |
618 | mock.call("[Stage] foo bar=baz, c=c = lambda x: x, d={'currency': 'BTC', 'value': Decimal('1')}"), | |
619 | ]) | |
620 | add_log.assert_called_once_with({ | |
621 | 'type': 'stage', | |
622 | 'stage': 'foo', | |
623 | 'args': { | |
624 | 'bar': 'baz', | |
625 | 'c': 'c = lambda x: x', | |
626 | 'd': { | |
627 | 'currency': 'BTC', | |
628 | 'value': D('1') | |
629 | } | |
630 | } | |
631 | }) | |
632 | ||
633 | @mock.patch.object(market.ReportStore, "print_log") | |
634 | @mock.patch.object(market.ReportStore, "add_log") | |
635 | @mock.patch.object(market.ReportStore, "add_redis_status") | |
636 | def test_log_balances(self, add_redis_status, add_log, print_log): | |
637 | report_store = market.ReportStore(self.m) | |
638 | self.m.balances.as_json.return_value = "json" | |
639 | self.m.balances.all = { "FOO": "bar", "BAR": "baz" } | |
640 | ||
641 | with self.subTest(tickers=None): | |
642 | report_store.log_balances(tag="tag") | |
643 | print_log.assert_has_calls([ | |
644 | mock.call("[Balance]"), | |
645 | mock.call("\tbar"), | |
646 | mock.call("\tbaz"), | |
647 | ]) | |
648 | add_log.assert_called_once_with({ | |
649 | 'type': 'balance', | |
650 | 'checkpoint': None, | |
651 | 'balances': 'json', | |
652 | 'tag': 'tag' | |
653 | }) | |
654 | add_redis_status.assert_called_once_with({ | |
655 | 'type': 'balance', | |
656 | 'balances': 'json', | |
657 | 'checkpoint': None, | |
658 | 'tag': 'tag' | |
659 | }) | |
660 | add_log.reset_mock() | |
661 | add_redis_status.reset_mock() | |
662 | with self.subTest(tickers="present"): | |
663 | amounts = { | |
664 | "BTC": portfolio.Amount("BTC", 10), | |
665 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
666 | } | |
667 | amounts["ETH"].rate = D("0.1") | |
668 | ||
669 | report_store.log_balances(tag="tag", tickers=amounts, | |
670 | ticker_currency="BTC", compute_value="default", | |
671 | type="total") | |
672 | add_log.assert_called_once_with({ | |
673 | 'type': 'balance', | |
674 | 'checkpoint': None, | |
675 | 'balances': 'json', | |
676 | 'tag': 'tag', | |
677 | 'tickers': { | |
678 | 'compute_value': 'default', | |
679 | 'balance_type': 'total', | |
680 | 'currency': 'BTC', | |
681 | 'balances': { | |
682 | 'BTC': D('10'), | |
683 | 'ETH': D('0.3') | |
684 | }, | |
685 | 'rates': { | |
686 | 'BTC': None, | |
687 | 'ETH': D('0.1') | |
688 | }, | |
689 | 'total': D('10.3') | |
690 | }, | |
691 | }) | |
692 | add_redis_status.assert_called_once_with({ | |
693 | 'type': 'balance', | |
694 | 'checkpoint': None, | |
695 | 'balances': 'json', | |
696 | 'tag': 'tag', | |
697 | 'tickers': { | |
698 | 'compute_value': 'default', | |
699 | 'balance_type': 'total', | |
700 | 'currency': 'BTC', | |
701 | 'balances': { | |
702 | 'BTC': D('10'), | |
703 | 'ETH': D('0.3') | |
704 | }, | |
705 | 'rates': { | |
706 | 'BTC': None, | |
707 | 'ETH': D('0.1') | |
708 | }, | |
709 | 'total': D('10.3') | |
710 | }, | |
711 | }) | |
712 | ||
713 | @mock.patch.object(market.ReportStore, "print_log") | |
714 | @mock.patch.object(market.ReportStore, "add_log") | |
715 | def test_log_tickers(self, add_log, print_log): | |
716 | report_store = market.ReportStore(self.m) | |
717 | amounts = { | |
718 | "BTC": portfolio.Amount("BTC", 10), | |
719 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
720 | } | |
721 | amounts["ETH"].rate = D("0.1") | |
722 | ||
723 | report_store.log_tickers(amounts, "BTC", "default", "total") | |
724 | print_log.assert_not_called() | |
725 | add_log.assert_called_once_with({ | |
726 | 'type': 'tickers', | |
727 | 'compute_value': 'default', | |
728 | 'balance_type': 'total', | |
729 | 'currency': 'BTC', | |
730 | 'balances': { | |
731 | 'BTC': D('10'), | |
732 | 'ETH': D('0.3') | |
733 | }, | |
734 | 'rates': { | |
735 | 'BTC': None, | |
736 | 'ETH': D('0.1') | |
737 | }, | |
738 | 'total': D('10.3') | |
739 | }) | |
740 | ||
741 | add_log.reset_mock() | |
742 | compute_value = lambda x: x["bid"] | |
743 | report_store.log_tickers(amounts, "BTC", compute_value, "total") | |
744 | add_log.assert_called_once_with({ | |
745 | 'type': 'tickers', | |
746 | 'compute_value': 'compute_value = lambda x: x["bid"]', | |
747 | 'balance_type': 'total', | |
748 | 'currency': 'BTC', | |
749 | 'balances': { | |
750 | 'BTC': D('10'), | |
751 | 'ETH': D('0.3') | |
752 | }, | |
753 | 'rates': { | |
754 | 'BTC': None, | |
755 | 'ETH': D('0.1') | |
756 | }, | |
757 | 'total': D('10.3') | |
758 | }) | |
759 | ||
760 | @mock.patch.object(market.ReportStore, "print_log") | |
761 | @mock.patch.object(market.ReportStore, "add_log") | |
762 | def test_log_dispatch(self, add_log, print_log): | |
763 | report_store = market.ReportStore(self.m) | |
764 | amount = portfolio.Amount("BTC", "10.3") | |
765 | amounts = { | |
766 | "BTC": portfolio.Amount("BTC", 10), | |
767 | "ETH": portfolio.Amount("BTC", D("0.3")) | |
768 | } | |
769 | report_store.log_dispatch(amount, amounts, "medium", "repartition") | |
770 | print_log.assert_not_called() | |
771 | add_log.assert_called_once_with({ | |
772 | 'type': 'dispatch', | |
773 | 'liquidity': 'medium', | |
774 | 'repartition_ratio': 'repartition', | |
775 | 'total_amount': { | |
776 | 'currency': 'BTC', | |
777 | 'value': D('10.3') | |
778 | }, | |
779 | 'repartition': { | |
780 | 'BTC': D('10'), | |
781 | 'ETH': D('0.3') | |
782 | } | |
783 | }) | |
784 | ||
785 | @mock.patch.object(market.ReportStore, "print_log") | |
786 | @mock.patch.object(market.ReportStore, "add_log") | |
787 | def test_log_trades(self, add_log, print_log): | |
788 | report_store = market.ReportStore(self.m) | |
789 | trade_mock1 = mock.Mock() | |
790 | trade_mock2 = mock.Mock() | |
791 | trade_mock1.as_json.return_value = { "trade": "1" } | |
792 | trade_mock2.as_json.return_value = { "trade": "2" } | |
793 | ||
794 | matching_and_trades = [ | |
795 | (True, trade_mock1), | |
796 | (False, trade_mock2), | |
797 | ] | |
798 | report_store.log_trades(matching_and_trades, "only") | |
799 | ||
800 | print_log.assert_not_called() | |
801 | add_log.assert_called_with({ | |
802 | 'type': 'trades', | |
803 | 'only': 'only', | |
804 | 'debug': False, | |
805 | 'trades': [ | |
806 | {'trade': '1', 'skipped': False}, | |
807 | {'trade': '2', 'skipped': True} | |
808 | ] | |
809 | }) | |
810 | ||
811 | @mock.patch.object(market.ReportStore, "print_log") | |
812 | @mock.patch.object(market.ReportStore, "add_log") | |
813 | def test_log_orders(self, add_log, print_log): | |
814 | report_store = market.ReportStore(self.m) | |
815 | ||
816 | order_mock1 = mock.Mock() | |
817 | order_mock2 = mock.Mock() | |
818 | ||
819 | order_mock1.as_json.return_value = "order1" | |
820 | order_mock2.as_json.return_value = "order2" | |
821 | ||
822 | orders = [order_mock1, order_mock2] | |
823 | ||
824 | report_store.log_orders(orders, tick="tick", | |
825 | only="only", compute_value="compute_value") | |
826 | ||
827 | print_log.assert_called_once_with("[Orders]") | |
828 | self.m.trades.print_all_with_order.assert_called_once_with(ind="\t") | |
829 | ||
830 | add_log.assert_called_with({ | |
831 | 'type': 'orders', | |
832 | 'only': 'only', | |
833 | 'compute_value': 'compute_value', | |
834 | 'tick': 'tick', | |
835 | 'orders': ['order1', 'order2'] | |
836 | }) | |
837 | ||
838 | add_log.reset_mock() | |
839 | def compute_value(x, y): | |
840 | return x[y] | |
841 | report_store.log_orders(orders, tick="tick", | |
842 | only="only", compute_value=compute_value) | |
843 | add_log.assert_called_with({ | |
844 | 'type': 'orders', | |
845 | 'only': 'only', | |
846 | 'compute_value': 'def compute_value(x, y):\n return x[y]', | |
847 | 'tick': 'tick', | |
848 | 'orders': ['order1', 'order2'] | |
849 | }) | |
850 | ||
851 | ||
852 | @mock.patch.object(market.ReportStore, "print_log") | |
853 | @mock.patch.object(market.ReportStore, "add_log") | |
854 | def test_log_order(self, add_log, print_log): | |
855 | report_store = market.ReportStore(self.m) | |
856 | order_mock = mock.Mock() | |
857 | order_mock.as_json.return_value = "order" | |
858 | new_order_mock = mock.Mock() | |
859 | new_order_mock.as_json.return_value = "new_order" | |
860 | order_mock.__repr__ = mock.Mock() | |
861 | order_mock.__repr__.return_value = "Order Mock" | |
862 | new_order_mock.__repr__ = mock.Mock() | |
863 | new_order_mock.__repr__.return_value = "New order Mock" | |
864 | ||
865 | with self.subTest(finished=True): | |
866 | report_store.log_order(order_mock, 1, finished=True) | |
867 | print_log.assert_called_once_with("[Order] Finished Order Mock") | |
868 | add_log.assert_called_once_with({ | |
869 | 'type': 'order', | |
870 | 'tick': 1, | |
871 | 'update': None, | |
872 | 'order': 'order', | |
873 | 'compute_value': None, | |
874 | 'new_order': None | |
875 | }) | |
876 | ||
877 | add_log.reset_mock() | |
878 | print_log.reset_mock() | |
879 | ||
880 | with self.subTest(update="waiting"): | |
881 | report_store.log_order(order_mock, 1, update="waiting") | |
882 | print_log.assert_called_once_with("[Order] Order Mock, tick 1, waiting") | |
883 | add_log.assert_called_once_with({ | |
884 | 'type': 'order', | |
885 | 'tick': 1, | |
886 | 'update': 'waiting', | |
887 | 'order': 'order', | |
888 | 'compute_value': None, | |
889 | 'new_order': None | |
890 | }) | |
891 | ||
892 | add_log.reset_mock() | |
893 | print_log.reset_mock() | |
894 | with self.subTest(update="adjusting"): | |
895 | compute_value = lambda x: (x["bid"] + x["ask"]*2)/3 | |
896 | report_store.log_order(order_mock, 3, | |
897 | update="adjusting", new_order=new_order_mock, | |
898 | compute_value=compute_value) | |
899 | print_log.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock") | |
900 | add_log.assert_called_once_with({ | |
901 | 'type': 'order', | |
902 | 'tick': 3, | |
903 | 'update': 'adjusting', | |
904 | 'order': 'order', | |
905 | 'compute_value': 'compute_value = lambda x: (x["bid"] + x["ask"]*2)/3', | |
906 | 'new_order': 'new_order' | |
907 | }) | |
908 | ||
909 | add_log.reset_mock() | |
910 | print_log.reset_mock() | |
911 | with self.subTest(update="market_fallback"): | |
912 | report_store.log_order(order_mock, 7, | |
913 | update="market_fallback", new_order=new_order_mock) | |
914 | print_log.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value") | |
915 | add_log.assert_called_once_with({ | |
916 | 'type': 'order', | |
917 | 'tick': 7, | |
918 | 'update': 'market_fallback', | |
919 | 'order': 'order', | |
920 | 'compute_value': None, | |
921 | 'new_order': 'new_order' | |
922 | }) | |
923 | ||
924 | add_log.reset_mock() | |
925 | print_log.reset_mock() | |
926 | with self.subTest(update="market_adjusting"): | |
927 | report_store.log_order(order_mock, 17, | |
928 | update="market_adjust", new_order=new_order_mock) | |
929 | print_log.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock") | |
930 | add_log.assert_called_once_with({ | |
931 | 'type': 'order', | |
932 | 'tick': 17, | |
933 | 'update': 'market_adjust', | |
934 | 'order': 'order', | |
935 | 'compute_value': None, | |
936 | 'new_order': 'new_order' | |
937 | }) | |
938 | ||
939 | @mock.patch.object(market.ReportStore, "print_log") | |
940 | @mock.patch.object(market.ReportStore, "add_log") | |
941 | def test_log_move_balances(self, add_log, print_log): | |
942 | report_store = market.ReportStore(self.m) | |
943 | needed = { | |
944 | "BTC": portfolio.Amount("BTC", 10), | |
945 | "USDT": 1 | |
946 | } | |
947 | moving = { | |
948 | "BTC": portfolio.Amount("BTC", 3), | |
949 | "USDT": -2 | |
950 | } | |
951 | report_store.log_move_balances(needed, moving) | |
952 | print_log.assert_not_called() | |
953 | add_log.assert_called_once_with({ | |
954 | 'type': 'move_balances', | |
955 | 'debug': False, | |
956 | 'needed': { | |
957 | 'BTC': D('10'), | |
958 | 'USDT': 1 | |
959 | }, | |
960 | 'moving': { | |
961 | 'BTC': D('3'), | |
962 | 'USDT': -2 | |
963 | } | |
964 | }) | |
965 | ||
966 | def test_log_http_request(self): | |
967 | with mock.patch.object(market.ReportStore, "add_log") as add_log: | |
968 | report_store = market.ReportStore(self.m) | |
969 | response = mock.Mock() | |
970 | response.status_code = 200 | |
971 | response.text = "Hey" | |
972 | response.elapsed.total_seconds.return_value = 120 | |
973 | ||
974 | report_store.log_http_request("method", "url", "body", | |
975 | "headers", response) | |
976 | add_log.assert_called_once_with({ | |
977 | 'type': 'http_request', | |
978 | 'method': 'method', | |
979 | 'url': 'url', | |
980 | 'body': 'body', | |
981 | 'headers': 'headers', | |
982 | 'status': 200, | |
983 | 'duration': 120, | |
984 | 'response': 'Hey', | |
985 | 'response_same_as': None, | |
986 | }) | |
987 | ||
988 | add_log.reset_mock() | |
989 | report_store.log_http_request("method", "url", "body", | |
990 | "headers", ValueError("Foo")) | |
991 | add_log.assert_called_once_with({ | |
992 | 'type': 'http_request', | |
993 | 'method': 'method', | |
994 | 'url': 'url', | |
995 | 'body': 'body', | |
996 | 'headers': 'headers', | |
997 | 'status': -1, | |
998 | 'response': None, | |
999 | 'error': 'ValueError', | |
1000 | 'error_message': 'Foo', | |
1001 | }) | |
1002 | ||
1003 | with self.subTest(no_http_dup=True, duplicate=True): | |
1004 | self.m.user_id = 1 | |
1005 | self.m.market_id = 3 | |
1006 | report_store = market.ReportStore(self.m, no_http_dup=True) | |
1007 | original_add_log = report_store.add_log | |
1008 | with mock.patch.object(report_store, "add_log", side_effect=original_add_log) as add_log: | |
1009 | report_store.log_http_request("method", "url", "body", | |
1010 | "headers", response) | |
1011 | report_store.log_http_request("method", "url", "body", | |
1012 | "headers", response) | |
1013 | self.assertEqual(2, add_log.call_count) | |
1014 | self.assertIsNone(add_log.mock_calls[0][1][0]["response_same_as"]) | |
1015 | self.assertIsNone(add_log.mock_calls[1][1][0]["response"]) | |
1016 | self.assertEqual(add_log.mock_calls[0][1][0]["date"], add_log.mock_calls[1][1][0]["response_same_as"]) | |
1017 | with self.subTest(no_http_dup=True, duplicate=False, case="Different call"): | |
1018 | self.m.user_id = 1 | |
1019 | self.m.market_id = 3 | |
1020 | report_store = market.ReportStore(self.m, no_http_dup=True) | |
1021 | original_add_log = report_store.add_log | |
1022 | with mock.patch.object(report_store, "add_log", side_effect=original_add_log) as add_log: | |
1023 | report_store.log_http_request("method", "url", "body", | |
1024 | "headers", response) | |
1025 | report_store.log_http_request("method2", "url", "body", | |
1026 | "headers", response) | |
1027 | self.assertEqual(2, add_log.call_count) | |
1028 | self.assertIsNone(add_log.mock_calls[0][1][0]["response_same_as"]) | |
1029 | self.assertIsNone(add_log.mock_calls[1][1][0]["response_same_as"]) | |
1030 | with self.subTest(no_http_dup=True, duplicate=False, case="Call inbetween"): | |
1031 | self.m.user_id = 1 | |
1032 | self.m.market_id = 3 | |
1033 | report_store = market.ReportStore(self.m, no_http_dup=True) | |
1034 | original_add_log = report_store.add_log | |
1035 | ||
1036 | response2 = mock.Mock() | |
1037 | response2.status_code = 200 | |
1038 | response2.text = "Hey there!" | |
1039 | ||
1040 | with mock.patch.object(report_store, "add_log", side_effect=original_add_log) as add_log: | |
1041 | report_store.log_http_request("method", "url", "body", | |
1042 | "headers", response) | |
1043 | report_store.log_http_request("method", "url", "body", | |
1044 | "headers", response2) | |
1045 | report_store.log_http_request("method", "url", "body", | |
1046 | "headers", response) | |
1047 | self.assertEqual(3, add_log.call_count) | |
1048 | self.assertIsNone(add_log.mock_calls[0][1][0]["response_same_as"]) | |
1049 | self.assertIsNone(add_log.mock_calls[1][1][0]["response_same_as"]) | |
1050 | self.assertIsNone(add_log.mock_calls[2][1][0]["response_same_as"]) | |
1051 | ||
1052 | @mock.patch.object(market.ReportStore, "add_log") | |
1053 | def test_log_market(self, add_log): | |
1054 | report_store = market.ReportStore(self.m) | |
1055 | ||
1056 | report_store.log_market(self.market_args(debug=True, quiet=False)) | |
1057 | add_log.assert_called_once_with({ | |
1058 | "type": "market", | |
1059 | "commit": "$Format:%H$", | |
1060 | "args": { "report_path": None, "debug": True, "quiet": False }, | |
1061 | }) | |
1062 | ||
1063 | @mock.patch.object(market.ReportStore, "print_log") | |
1064 | @mock.patch.object(market.ReportStore, "add_log") | |
1065 | def test_log_error(self, add_log, print_log): | |
1066 | report_store = market.ReportStore(self.m) | |
1067 | with self.subTest(message=None, exception=None): | |
1068 | report_store.log_error("action") | |
1069 | print_log.assert_called_once_with("[Error] action") | |
1070 | add_log.assert_called_once_with({ | |
1071 | 'type': 'error', | |
1072 | 'action': 'action', | |
1073 | 'exception_class': None, | |
1074 | 'exception_message': None, | |
1075 | 'message': None | |
1076 | }) | |
1077 | ||
1078 | print_log.reset_mock() | |
1079 | add_log.reset_mock() | |
1080 | with self.subTest(message="Hey", exception=None): | |
1081 | report_store.log_error("action", message="Hey") | |
1082 | print_log.assert_has_calls([ | |
1083 | mock.call("[Error] action"), | |
1084 | mock.call("\tHey") | |
1085 | ]) | |
1086 | add_log.assert_called_once_with({ | |
1087 | 'type': 'error', | |
1088 | 'action': 'action', | |
1089 | 'exception_class': None, | |
1090 | 'exception_message': None, | |
1091 | 'message': "Hey" | |
1092 | }) | |
1093 | ||
1094 | print_log.reset_mock() | |
1095 | add_log.reset_mock() | |
1096 | with self.subTest(message=None, exception=Exception("bouh")): | |
1097 | report_store.log_error("action", exception=Exception("bouh")) | |
1098 | print_log.assert_has_calls([ | |
1099 | mock.call("[Error] action"), | |
1100 | mock.call("\tException: bouh") | |
1101 | ]) | |
1102 | add_log.assert_called_once_with({ | |
1103 | 'type': 'error', | |
1104 | 'action': 'action', | |
1105 | 'exception_class': "Exception", | |
1106 | 'exception_message': "bouh", | |
1107 | 'message': None | |
1108 | }) | |
1109 | ||
1110 | print_log.reset_mock() | |
1111 | add_log.reset_mock() | |
1112 | with self.subTest(message="Hey", exception=Exception("bouh")): | |
1113 | report_store.log_error("action", message="Hey", exception=Exception("bouh")) | |
1114 | print_log.assert_has_calls([ | |
1115 | mock.call("[Error] action"), | |
1116 | mock.call("\tException: bouh"), | |
1117 | mock.call("\tHey") | |
1118 | ]) | |
1119 | add_log.assert_called_once_with({ | |
1120 | 'type': 'error', | |
1121 | 'action': 'action', | |
1122 | 'exception_class': "Exception", | |
1123 | 'exception_message': "bouh", | |
1124 | 'message': "Hey" | |
1125 | }) | |
1126 | ||
1127 | @mock.patch.object(market.ReportStore, "print_log") | |
1128 | @mock.patch.object(market.ReportStore, "add_log") | |
1129 | def test_log_debug_action(self, add_log, print_log): | |
1130 | report_store = market.ReportStore(self.m) | |
1131 | report_store.log_debug_action("Hey") | |
1132 | ||
1133 | print_log.assert_called_once_with("[Debug] Hey") | |
1134 | add_log.assert_called_once_with({ | |
1135 | 'type': 'debug_action', | |
1136 | 'action': 'Hey' | |
1137 | }) | |
1138 | ||
1139 | @unittest.skipUnless("unit" in limits, "Unit skipped") | |
1140 | class PortfolioTest(WebMockTestCase): | |
1141 | def setUp(self): | |
1142 | super().setUp() | |
1143 | ||
1144 | with open("test_samples/test_portfolio.json") as example: | |
1145 | self.json_response = example.read() | |
1146 | ||
1147 | self.wm.get(market.Portfolio.URL, text=self.json_response) | |
1148 | ||
1149 | @mock.patch.object(market.Portfolio, "parse_cryptoportfolio") | |
1150 | @mock.patch.object(market.Portfolio, "store_cryptoportfolio") | |
1151 | def test_get_cryptoportfolio(self, store_cryptoportfolio, parse_cryptoportfolio): | |
1152 | with self.subTest(parallel=False): | |
1153 | self.wm.get(market.Portfolio.URL, [ | |
1154 | {"text":'{ "foo": "bar" }', "status_code": 200}, | |
1155 | {"text": "System Error", "status_code": 500}, | |
1156 | {"exc": requests.exceptions.ConnectTimeout}, | |
1157 | ]) | |
1158 | market.Portfolio.get_cryptoportfolio() | |
1159 | self.assertIn("foo", market.Portfolio.data.get()) | |
1160 | self.assertEqual("bar", market.Portfolio.data.get()["foo"]) | |
1161 | self.assertTrue(self.wm.called) | |
1162 | self.assertEqual(1, self.wm.call_count) | |
1163 | market.Portfolio.report.log_error.assert_not_called() | |
1164 | market.Portfolio.report.log_http_request.assert_called_once() | |
1165 | parse_cryptoportfolio.assert_called_once_with() | |
1166 | store_cryptoportfolio.assert_called_once_with() | |
1167 | market.Portfolio.report.log_http_request.reset_mock() | |
1168 | parse_cryptoportfolio.reset_mock() | |
1169 | store_cryptoportfolio.reset_mock() | |
1170 | market.Portfolio.data = store.LockedVar(None) | |
1171 | ||
1172 | market.Portfolio.get_cryptoportfolio() | |
1173 | self.assertIsNone(market.Portfolio.data.get()) | |
1174 | self.assertEqual(2, self.wm.call_count) | |
1175 | parse_cryptoportfolio.assert_not_called() | |
1176 | store_cryptoportfolio.assert_not_called() | |
1177 | market.Portfolio.report.log_error.assert_not_called() | |
1178 | market.Portfolio.report.log_http_request.assert_called_once() | |
1179 | market.Portfolio.report.log_http_request.reset_mock() | |
1180 | parse_cryptoportfolio.reset_mock() | |
1181 | store_cryptoportfolio.reset_mock() | |
1182 | ||
1183 | market.Portfolio.data = store.LockedVar("Foo") | |
1184 | market.Portfolio.get_cryptoportfolio() | |
1185 | self.assertEqual(2, self.wm.call_count) | |
1186 | parse_cryptoportfolio.assert_not_called() | |
1187 | store_cryptoportfolio.assert_not_called() | |
1188 | ||
1189 | market.Portfolio.get_cryptoportfolio(refetch=True) | |
1190 | self.assertEqual("Foo", market.Portfolio.data.get()) | |
1191 | self.assertEqual(3, self.wm.call_count) | |
1192 | market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio", | |
1193 | exception=mock.ANY) | |
1194 | market.Portfolio.report.log_http_request.assert_not_called() | |
1195 | with self.subTest(parallel=True): | |
1196 | with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\ | |
1197 | mock.patch.object(market.Portfolio, "notify_and_wait") as notify: | |
1198 | with self.subTest(worker=True): | |
1199 | market.Portfolio.data = store.LockedVar(None) | |
1200 | market.Portfolio.worker = mock.Mock() | |
1201 | is_worker.return_value = True | |
1202 | self.wm.get(market.Portfolio.URL, [ | |
1203 | {"text":'{ "foo": "bar" }', "status_code": 200}, | |
1204 | ]) | |
1205 | market.Portfolio.get_cryptoportfolio() | |
1206 | self.assertIn("foo", market.Portfolio.data.get()) | |
1207 | parse_cryptoportfolio.reset_mock() | |
1208 | store_cryptoportfolio.reset_mock() | |
1209 | with self.subTest(worker=False): | |
1210 | market.Portfolio.data = store.LockedVar(None) | |
1211 | market.Portfolio.worker = mock.Mock() | |
1212 | is_worker.return_value = False | |
1213 | market.Portfolio.get_cryptoportfolio() | |
1214 | notify.assert_called_once_with() | |
1215 | parse_cryptoportfolio.assert_not_called() | |
1216 | store_cryptoportfolio.assert_not_called() | |
1217 | ||
1218 | def test_parse_cryptoportfolio(self): | |
1219 | with self.subTest(description="Normal case"): | |
1220 | market.Portfolio.data = store.LockedVar(store.json.loads( | |
1221 | self.json_response, parse_int=D, parse_float=D)) | |
1222 | market.Portfolio.parse_cryptoportfolio() | |
1223 | ||
1224 | self.assertListEqual( | |
1225 | ["medium", "high"], | |
1226 | list(market.Portfolio.liquidities.get().keys())) | |
1227 | ||
1228 | liquidities = market.Portfolio.liquidities.get() | |
1229 | self.assertEqual(10, len(liquidities["medium"].keys())) | |
1230 | self.assertEqual(10, len(liquidities["high"].keys())) | |
1231 | ||
1232 | expected = { | |
1233 | 'BTC': (D("0.2857"), "long"), | |
1234 | 'DGB': (D("0.1015"), "long"), | |
1235 | 'DOGE': (D("0.1805"), "long"), | |
1236 | 'SC': (D("0.0623"), "long"), | |
1237 | 'ZEC': (D("0.3701"), "long"), | |
1238 | } | |
1239 | date = portfolio.datetime.datetime(2018, 1, 8) | |
1240 | self.assertDictEqual(expected, liquidities["high"][date]) | |
1241 | ||
1242 | expected = { | |
1243 | 'BTC': (D("1.1102e-16"), "long"), | |
1244 | 'ETC': (D("0.1"), "long"), | |
1245 | 'FCT': (D("0.1"), "long"), | |
1246 | 'GAS': (D("0.1"), "long"), | |
1247 | 'NAV': (D("0.1"), "long"), | |
1248 | 'OMG': (D("0.1"), "long"), | |
1249 | 'OMNI': (D("0.1"), "long"), | |
1250 | 'PPC': (D("0.1"), "long"), | |
1251 | 'RIC': (D("0.1"), "long"), | |
1252 | 'VIA': (D("0.1"), "long"), | |
1253 | 'XCP': (D("0.1"), "long"), | |
1254 | } | |
1255 | self.assertDictEqual(expected, liquidities["medium"][date]) | |
1256 | self.assertEqual(portfolio.datetime.datetime(2018, 1, 15), market.Portfolio.last_date.get()) | |
1257 | ||
1258 | with self.subTest(description="Missing weight"): | |
1259 | data = store.json.loads(self.json_response, parse_int=D, parse_float=D) | |
1260 | del(data["portfolio_2"]["weights"]) | |
1261 | market.Portfolio.data = store.LockedVar(data) | |
1262 | ||
1263 | market.Portfolio.parse_cryptoportfolio() | |
1264 | self.assertListEqual( | |
1265 | ["medium", "high"], | |
1266 | list(market.Portfolio.liquidities.get().keys())) | |
1267 | self.assertEqual({}, market.Portfolio.liquidities.get("medium")) | |
1268 | ||
1269 | with self.subTest(description="All missing weights"): | |
1270 | data = store.json.loads(self.json_response, parse_int=D, parse_float=D) | |
1271 | del(data["portfolio_1"]["weights"]) | |
1272 | del(data["portfolio_2"]["weights"]) | |
1273 | market.Portfolio.data = store.LockedVar(data) | |
1274 | ||
1275 | market.Portfolio.parse_cryptoportfolio() | |
1276 | self.assertEqual({}, market.Portfolio.liquidities.get("medium")) | |
1277 | self.assertEqual({}, market.Portfolio.liquidities.get("high")) | |
1278 | self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get()) | |
1279 | ||
1280 | @mock.patch.object(store.dbs, "redis_connected") | |
1281 | @mock.patch.object(store.dbs, "redis") | |
1282 | def test_store_cryptoportfolio(self, redis, redis_connected): | |
1283 | store.Portfolio.liquidities = store.LockedVar({ | |
1284 | "medium": { | |
1285 | datetime.datetime(2018,3,1): "medium_2018-03-01", | |
1286 | datetime.datetime(2018,3,8): "medium_2018-03-08", | |
1287 | }, | |
1288 | "high": { | |
1289 | datetime.datetime(2018,3,1): "high_2018-03-01", | |
1290 | datetime.datetime(2018,3,8): "high_2018-03-08", | |
1291 | } | |
1292 | }) | |
1293 | store.Portfolio.last_date = store.LockedVar(datetime.datetime(2018,3,8)) | |
1294 | ||
1295 | with self.subTest(redis_connected=False): | |
1296 | redis_connected.return_value = False | |
1297 | store.Portfolio.store_cryptoportfolio() | |
1298 | redis.set.assert_not_called() | |
1299 | ||
1300 | with self.subTest(redis_connected=True): | |
1301 | redis_connected.return_value = True | |
1302 | store.Portfolio.store_cryptoportfolio() | |
1303 | redis.set.assert_has_calls([ | |
1304 | mock.call("/cryptoportfolio/repartition/latest", '{"medium": "medium_2018-03-08", "high": "high_2018-03-08"}'), | |
1305 | mock.call("/cryptoportfolio/repartition/date", "2018-03-08"), | |
1306 | ]) | |
1307 | ||
1308 | @mock.patch.object(store.dbs, "redis_connected") | |
1309 | @mock.patch.object(store.dbs, "redis") | |
1310 | def test_retrieve_cryptoportfolio(self, redis, redis_connected): | |
1311 | with self.subTest(redis_connected=False): | |
1312 | redis_connected.return_value = False | |
1313 | store.Portfolio.retrieve_cryptoportfolio() | |
1314 | redis.get.assert_not_called() | |
1315 | self.assertIsNone(store.Portfolio.data.get()) | |
1316 | ||
1317 | with self.subTest(redis_connected=True, value=None): | |
1318 | redis_connected.return_value = True | |
1319 | redis.get.return_value = None | |
1320 | store.Portfolio.retrieve_cryptoportfolio() | |
1321 | self.assertEqual(2, redis.get.call_count) | |
1322 | ||
1323 | redis.reset_mock() | |
1324 | with self.subTest(redis_connected=True, value="present"): | |
1325 | redis_connected.return_value = True | |
1326 | redis.get.side_effect = [ | |
1327 | b'{ "medium": "medium_repartition", "high": "high_repartition" }', | |
1328 | b"2018-03-08" | |
1329 | ] | |
1330 | store.Portfolio.retrieve_cryptoportfolio() | |
1331 | self.assertEqual(2, redis.get.call_count) | |
1332 | self.assertEqual(datetime.datetime(2018,3,8), store.Portfolio.last_date.get()) | |
1333 | self.assertEqual("", store.Portfolio.data.get()) | |
1334 | expected_liquidities = { | |
1335 | 'high': { datetime.datetime(2018, 3, 8): 'high_repartition' }, | |
1336 | 'medium': { datetime.datetime(2018, 3, 8): 'medium_repartition' }, | |
1337 | } | |
1338 | self.assertEqual(expected_liquidities, store.Portfolio.liquidities.get()) | |
1339 | ||
1340 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | |
1341 | @mock.patch.object(market.Portfolio, "retrieve_cryptoportfolio") | |
1342 | def test_repartition(self, retrieve_cryptoportfolio, get_cryptoportfolio): | |
1343 | with self.subTest(from_cache=False): | |
1344 | market.Portfolio.liquidities = store.LockedVar({ | |
1345 | "medium": { | |
1346 | "2018-03-01": "medium_2018-03-01", | |
1347 | "2018-03-08": "medium_2018-03-08", | |
1348 | }, | |
1349 | "high": { | |
1350 | "2018-03-01": "high_2018-03-01", | |
1351 | "2018-03-08": "high_2018-03-08", | |
1352 | } | |
1353 | }) | |
1354 | market.Portfolio.last_date = store.LockedVar("2018-03-08") | |
1355 | ||
1356 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition()) | |
1357 | get_cryptoportfolio.assert_called_once_with() | |
1358 | retrieve_cryptoportfolio.assert_not_called() | |
1359 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) | |
1360 | self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) | |
1361 | ||
1362 | retrieve_cryptoportfolio.reset_mock() | |
1363 | get_cryptoportfolio.reset_mock() | |
1364 | ||
1365 | with self.subTest(from_cache=True): | |
1366 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(from_cache=True)) | |
1367 | get_cryptoportfolio.assert_called_once_with() | |
1368 | retrieve_cryptoportfolio.assert_called_once_with() | |
1369 | ||
1370 | @mock.patch.object(market.time, "sleep") | |
1371 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | |
1372 | def test_wait_for_recent(self, get_cryptoportfolio, sleep): | |
1373 | self.call_count = 0 | |
1374 | def _get(refetch=False): | |
1375 | if self.call_count != 0: | |
1376 | self.assertTrue(refetch) | |
1377 | else: | |
1378 | self.assertFalse(refetch) | |
1379 | self.call_count += 1 | |
1380 | market.Portfolio.last_date = store.LockedVar(store.datetime.datetime.now()\ | |
1381 | - store.datetime.timedelta(10)\ | |
1382 | + store.datetime.timedelta(self.call_count)) | |
1383 | get_cryptoportfolio.side_effect = _get | |
1384 | ||
1385 | market.Portfolio.wait_for_recent() | |
1386 | sleep.assert_called_with(30) | |
1387 | self.assertEqual(6, sleep.call_count) | |
1388 | self.assertEqual(7, get_cryptoportfolio.call_count) | |
1389 | market.Portfolio.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio") | |
1390 | ||
1391 | sleep.reset_mock() | |
1392 | get_cryptoportfolio.reset_mock() | |
1393 | market.Portfolio.last_date = store.LockedVar(None) | |
1394 | self.call_count = 0 | |
1395 | market.Portfolio.wait_for_recent(delta=15) | |
1396 | sleep.assert_not_called() | |
1397 | self.assertEqual(1, get_cryptoportfolio.call_count) | |
1398 | ||
1399 | sleep.reset_mock() | |
1400 | get_cryptoportfolio.reset_mock() | |
1401 | market.Portfolio.last_date = store.LockedVar(None) | |
1402 | self.call_count = 0 | |
1403 | market.Portfolio.wait_for_recent(delta=1) | |
1404 | sleep.assert_called_with(30) | |
1405 | self.assertEqual(9, sleep.call_count) | |
1406 | self.assertEqual(10, get_cryptoportfolio.call_count) | |
1407 | ||
1408 | def test_is_worker_thread(self): | |
1409 | with self.subTest(worker=None): | |
1410 | self.assertFalse(store.Portfolio.is_worker_thread()) | |
1411 | ||
1412 | with self.subTest(worker="not self"),\ | |
1413 | mock.patch("threading.current_thread") as current_thread: | |
1414 | current = mock.Mock() | |
1415 | current_thread.return_value = current | |
1416 | store.Portfolio.worker = mock.Mock() | |
1417 | self.assertFalse(store.Portfolio.is_worker_thread()) | |
1418 | ||
1419 | with self.subTest(worker="self"),\ | |
1420 | mock.patch("threading.current_thread") as current_thread: | |
1421 | current = mock.Mock() | |
1422 | current_thread.return_value = current | |
1423 | store.Portfolio.worker = current | |
1424 | self.assertTrue(store.Portfolio.is_worker_thread()) | |
1425 | ||
1426 | def test_start_worker(self): | |
1427 | with mock.patch.object(store.Portfolio, "wait_for_notification") as notification: | |
1428 | store.Portfolio.start_worker() | |
1429 | notification.assert_called_once_with(poll=30) | |
1430 | ||
1431 | self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__) | |
1432 | self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__) | |
1433 | store.Portfolio.report.start_lock.assert_called_once_with() | |
1434 | ||
1435 | self.assertIsNotNone(store.Portfolio.worker) | |
1436 | self.assertIsNotNone(store.Portfolio.worker_notify) | |
1437 | self.assertIsNotNone(store.Portfolio.callback) | |
1438 | self.assertTrue(store.Portfolio.worker_started) | |
1439 | ||
1440 | self.assertFalse(store.Portfolio.worker.is_alive()) | |
1441 | self.assertEqual(1, threading.active_count()) | |
1442 | ||
1443 | def test_stop_worker(self): | |
1444 | with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ | |
1445 | mock.patch.object(store.Portfolio, "report") as report,\ | |
1446 | mock.patch.object(store.time, "sleep") as sleep: | |
1447 | store.Portfolio.start_worker(poll=3) | |
1448 | store.Portfolio.stop_worker() | |
1449 | store.Portfolio.worker.join() | |
1450 | get.assert_not_called() | |
1451 | report.assert_not_called() | |
1452 | sleep.assert_not_called() | |
1453 | self.assertFalse(store.Portfolio.worker.is_alive()) | |
1454 | ||
1455 | def test_wait_for_notification(self): | |
1456 | with self.assertRaises(RuntimeError): | |
1457 | store.Portfolio.wait_for_notification() | |
1458 | ||
1459 | with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ | |
1460 | mock.patch.object(store.Portfolio, "report") as report,\ | |
1461 | mock.patch.object(store.time, "sleep") as sleep: | |
1462 | store.Portfolio.start_worker(poll=3) | |
1463 | ||
1464 | store.Portfolio.worker_notify.set() | |
1465 | ||
1466 | store.Portfolio.callback.wait() | |
1467 | ||
1468 | report.print_log.assert_called_once_with("[Worker] Fetching cryptoportfolio") | |
1469 | get.assert_called_once_with(refetch=True) | |
1470 | sleep.assert_called_once_with(3) | |
1471 | self.assertFalse(store.Portfolio.worker_notify.is_set()) | |
1472 | self.assertTrue(store.Portfolio.worker.is_alive()) | |
1473 | ||
1474 | store.Portfolio.callback.clear() | |
1475 | store.Portfolio.worker_started = False | |
1476 | store.Portfolio.worker_notify.set() | |
1477 | store.Portfolio.worker.join() | |
1478 | self.assertFalse(store.Portfolio.worker.is_alive()) | |
1479 | ||
1480 | def test_notify_and_wait(self): | |
1481 | with mock.patch.object(store.Portfolio, "callback") as callback,\ | |
1482 | mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: | |
1483 | store.Portfolio.notify_and_wait() | |
1484 | callback.clear.assert_called_once_with() | |
1485 | worker_notify.set.assert_called_once_with() | |
1486 | callback.wait.assert_called_once_with() | |
1487 | ||
1488 |