diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-04-06 21:08:06 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-04-07 19:43:29 +0200 |
commit | c682bdf4a02a45312ef1aadf8aa26136cf308414 (patch) | |
tree | 2fde937445a3e028813aa19dfab256513d6e2edb /tests/test_store.py | |
parent | e6015816224f8f405e9b1c9557f22e73b21246e8 (diff) | |
download | Trader-c682bdf4a02a45312ef1aadf8aa26136cf308414.tar.gz Trader-c682bdf4a02a45312ef1aadf8aa26136cf308414.tar.zst Trader-c682bdf4a02a45312ef1aadf8aa26136cf308414.zip |
Move tests to separate files
Diffstat (limited to 'tests/test_store.py')
-rw-r--r-- | tests/test_store.py | 1203 |
1 files changed, 1203 insertions, 0 deletions
diff --git a/tests/test_store.py b/tests/test_store.py new file mode 100644 index 0000000..c0b1fb9 --- /dev/null +++ b/tests/test_store.py | |||
@@ -0,0 +1,1203 @@ | |||
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 | balance_store.fetch_balances() | ||
373 | self.assertNotIn("ETC", balance_store.currencies()) | ||
374 | self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies())) | ||
375 | |||
376 | balance_store.all["ETC"] = portfolio.Balance("ETC", { | ||
377 | "exchange_total": "1", "exchange_free": "0", | ||
378 | "exchange_used": "1" }) | ||
379 | balance_store.fetch_balances(tag="foo") | ||
380 | self.assertEqual(0, balance_store.all["ETC"].total) | ||
381 | self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies())) | ||
382 | self.m.report.log_balances.assert_called_with(tag="foo") | ||
383 | |||
384 | @mock.patch.object(market.Portfolio, "repartition") | ||
385 | def test_dispatch_assets(self, repartition): | ||
386 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance | ||
387 | |||
388 | balance_store = market.BalanceStore(self.m) | ||
389 | balance_store.fetch_balances() | ||
390 | |||
391 | self.assertNotIn("XEM", balance_store.currencies()) | ||
392 | |||
393 | repartition_hash = { | ||
394 | "XEM": (D("0.75"), "long"), | ||
395 | "BTC": (D("0.26"), "long"), | ||
396 | "DASH": (D("0.10"), "short"), | ||
397 | } | ||
398 | repartition.return_value = repartition_hash | ||
399 | |||
400 | amounts = balance_store.dispatch_assets(portfolio.Amount("BTC", "11.1")) | ||
401 | repartition.assert_called_with(liquidity="medium") | ||
402 | self.assertIn("XEM", balance_store.currencies()) | ||
403 | self.assertEqual(D("2.6"), amounts["BTC"].value) | ||
404 | self.assertEqual(D("7.5"), amounts["XEM"].value) | ||
405 | self.assertEqual(D("-1.0"), amounts["DASH"].value) | ||
406 | self.m.report.log_balances.assert_called_with(tag=None) | ||
407 | self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC", | ||
408 | "11.1"), amounts, "medium", repartition_hash) | ||
409 | |||
410 | def test_currencies(self): | ||
411 | balance_store = market.BalanceStore(self.m) | ||
412 | |||
413 | balance_store.all = { | ||
414 | "BTC": portfolio.Balance("BTC", { | ||
415 | "total": "0.65", | ||
416 | "exchange_total":"0.65", | ||
417 | "exchange_free": "0.35", | ||
418 | "exchange_used": "0.30"}), | ||
419 | "ETH": portfolio.Balance("ETH", { | ||
420 | "total": 3, | ||
421 | "exchange_total": 3, | ||
422 | "exchange_free": 3, | ||
423 | "exchange_used": 0}), | ||
424 | } | ||
425 | self.assertListEqual(["BTC", "ETH"], list(balance_store.currencies())) | ||
426 | |||
427 | def test_as_json(self): | ||
428 | balance_mock1 = mock.Mock() | ||
429 | balance_mock1.as_json.return_value = 1 | ||
430 | |||
431 | balance_mock2 = mock.Mock() | ||
432 | balance_mock2.as_json.return_value = 2 | ||
433 | |||
434 | balance_store = market.BalanceStore(self.m) | ||
435 | balance_store.all = { | ||
436 | "BTC": balance_mock1, | ||
437 | "ETH": balance_mock2, | ||
438 | } | ||
439 | |||
440 | as_json = balance_store.as_json() | ||
441 | self.assertEqual(1, as_json["BTC"]) | ||
442 | self.assertEqual(2, as_json["ETH"]) | ||
443 | |||
444 | @unittest.skipUnless("unit" in limits, "Unit skipped") | ||
445 | class ReportStoreTest(WebMockTestCase): | ||
446 | def test_add_log(self): | ||
447 | report_store = market.ReportStore(self.m) | ||
448 | report_store.add_log({"foo": "bar"}) | ||
449 | |||
450 | self.assertEqual({"foo": "bar", "date": mock.ANY}, report_store.logs[0]) | ||
451 | |||
452 | def test_set_verbose(self): | ||
453 | report_store = market.ReportStore(self.m) | ||
454 | with self.subTest(verbose=True): | ||
455 | report_store.set_verbose(True) | ||
456 | self.assertTrue(report_store.verbose_print) | ||
457 | |||
458 | with self.subTest(verbose=False): | ||
459 | report_store.set_verbose(False) | ||
460 | self.assertFalse(report_store.verbose_print) | ||
461 | |||
462 | def test_merge(self): | ||
463 | report_store1 = market.ReportStore(self.m, verbose_print=False) | ||
464 | report_store2 = market.ReportStore(None, verbose_print=False) | ||
465 | |||
466 | report_store2.log_stage("1") | ||
467 | report_store1.log_stage("2") | ||
468 | report_store2.log_stage("3") | ||
469 | |||
470 | report_store1.merge(report_store2) | ||
471 | |||
472 | self.assertEqual(3, len(report_store1.logs)) | ||
473 | self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs))) | ||
474 | self.assertEqual(6, len(report_store1.print_logs)) | ||
475 | |||
476 | def test_print_log(self): | ||
477 | report_store = market.ReportStore(self.m) | ||
478 | with self.subTest(verbose=True),\ | ||
479 | mock.patch.object(store, "datetime") as time_mock,\ | ||
480 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | ||
481 | time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10) | ||
482 | report_store.set_verbose(True) | ||
483 | report_store.print_log("Coucou") | ||
484 | report_store.print_log(portfolio.Amount("BTC", 1)) | ||
485 | self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n") | ||
486 | |||
487 | with self.subTest(verbose=False),\ | ||
488 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | ||
489 | report_store.set_verbose(False) | ||
490 | report_store.print_log("Coucou") | ||
491 | report_store.print_log(portfolio.Amount("BTC", 1)) | ||
492 | self.assertEqual(stdout_mock.getvalue(), "") | ||
493 | |||
494 | def test_default_json_serial(self): | ||
495 | report_store = market.ReportStore(self.m) | ||
496 | |||
497 | self.assertEqual("2018-02-24T00:00:00", | ||
498 | report_store.default_json_serial(portfolio.datetime(2018, 2, 24))) | ||
499 | self.assertEqual("1.00000000 BTC", | ||
500 | report_store.default_json_serial(portfolio.Amount("BTC", 1))) | ||
501 | |||
502 | def test_to_json(self): | ||
503 | report_store = market.ReportStore(self.m) | ||
504 | report_store.logs.append({"foo": "bar"}) | ||
505 | self.assertEqual('[\n {\n "foo": "bar"\n }\n]', report_store.to_json()) | ||
506 | report_store.logs.append({"date": portfolio.datetime(2018, 2, 24)}) | ||
507 | self.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n }\n]', report_store.to_json()) | ||
508 | report_store.logs.append({"amount": portfolio.Amount("BTC", 1)}) | ||
509 | 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()) | ||
510 | |||
511 | def test_to_json_array(self): | ||
512 | report_store = market.ReportStore(self.m) | ||
513 | report_store.logs.append({ | ||
514 | "date": "date1", "type": "type1", "foo": "bar", "bla": "bla" | ||
515 | }) | ||
516 | report_store.logs.append({ | ||
517 | "date": "date2", "type": "type2", "foo": "bar", "bla": "bla" | ||
518 | }) | ||
519 | logs = list(report_store.to_json_array()) | ||
520 | |||
521 | self.assertEqual(2, len(logs)) | ||
522 | self.assertEqual(("date1", "type1", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[0]) | ||
523 | self.assertEqual(("date2", "type2", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[1]) | ||
524 | |||
525 | @mock.patch.object(market.ReportStore, "print_log") | ||
526 | @mock.patch.object(market.ReportStore, "add_log") | ||
527 | def test_log_stage(self, add_log, print_log): | ||
528 | report_store = market.ReportStore(self.m) | ||
529 | c = lambda x: x | ||
530 | report_store.log_stage("foo", bar="baz", c=c, d=portfolio.Amount("BTC", 1)) | ||
531 | print_log.assert_has_calls([ | ||
532 | mock.call("-----------"), | ||
533 | mock.call("[Stage] foo bar=baz, c=c = lambda x: x, d={'currency': 'BTC', 'value': Decimal('1')}"), | ||
534 | ]) | ||
535 | add_log.assert_called_once_with({ | ||
536 | 'type': 'stage', | ||
537 | 'stage': 'foo', | ||
538 | 'args': { | ||
539 | 'bar': 'baz', | ||
540 | 'c': 'c = lambda x: x', | ||
541 | 'd': { | ||
542 | 'currency': 'BTC', | ||
543 | 'value': D('1') | ||
544 | } | ||
545 | } | ||
546 | }) | ||
547 | |||
548 | @mock.patch.object(market.ReportStore, "print_log") | ||
549 | @mock.patch.object(market.ReportStore, "add_log") | ||
550 | def test_log_balances(self, add_log, print_log): | ||
551 | report_store = market.ReportStore(self.m) | ||
552 | self.m.balances.as_json.return_value = "json" | ||
553 | self.m.balances.all = { "FOO": "bar", "BAR": "baz" } | ||
554 | |||
555 | report_store.log_balances(tag="tag") | ||
556 | print_log.assert_has_calls([ | ||
557 | mock.call("[Balance]"), | ||
558 | mock.call("\tbar"), | ||
559 | mock.call("\tbaz"), | ||
560 | ]) | ||
561 | add_log.assert_called_once_with({ | ||
562 | 'type': 'balance', | ||
563 | 'balances': 'json', | ||
564 | 'tag': 'tag' | ||
565 | }) | ||
566 | |||
567 | @mock.patch.object(market.ReportStore, "print_log") | ||
568 | @mock.patch.object(market.ReportStore, "add_log") | ||
569 | def test_log_tickers(self, add_log, print_log): | ||
570 | report_store = market.ReportStore(self.m) | ||
571 | amounts = { | ||
572 | "BTC": portfolio.Amount("BTC", 10), | ||
573 | "ETH": portfolio.Amount("BTC", D("0.3")) | ||
574 | } | ||
575 | amounts["ETH"].rate = D("0.1") | ||
576 | |||
577 | report_store.log_tickers(amounts, "BTC", "default", "total") | ||
578 | print_log.assert_not_called() | ||
579 | add_log.assert_called_once_with({ | ||
580 | 'type': 'tickers', | ||
581 | 'compute_value': 'default', | ||
582 | 'balance_type': 'total', | ||
583 | 'currency': 'BTC', | ||
584 | 'balances': { | ||
585 | 'BTC': D('10'), | ||
586 | 'ETH': D('0.3') | ||
587 | }, | ||
588 | 'rates': { | ||
589 | 'BTC': None, | ||
590 | 'ETH': D('0.1') | ||
591 | }, | ||
592 | 'total': D('10.3') | ||
593 | }) | ||
594 | |||
595 | add_log.reset_mock() | ||
596 | compute_value = lambda x: x["bid"] | ||
597 | report_store.log_tickers(amounts, "BTC", compute_value, "total") | ||
598 | add_log.assert_called_once_with({ | ||
599 | 'type': 'tickers', | ||
600 | 'compute_value': 'compute_value = lambda x: x["bid"]', | ||
601 | 'balance_type': 'total', | ||
602 | 'currency': 'BTC', | ||
603 | 'balances': { | ||
604 | 'BTC': D('10'), | ||
605 | 'ETH': D('0.3') | ||
606 | }, | ||
607 | 'rates': { | ||
608 | 'BTC': None, | ||
609 | 'ETH': D('0.1') | ||
610 | }, | ||
611 | 'total': D('10.3') | ||
612 | }) | ||
613 | |||
614 | @mock.patch.object(market.ReportStore, "print_log") | ||
615 | @mock.patch.object(market.ReportStore, "add_log") | ||
616 | def test_log_dispatch(self, add_log, print_log): | ||
617 | report_store = market.ReportStore(self.m) | ||
618 | amount = portfolio.Amount("BTC", "10.3") | ||
619 | amounts = { | ||
620 | "BTC": portfolio.Amount("BTC", 10), | ||
621 | "ETH": portfolio.Amount("BTC", D("0.3")) | ||
622 | } | ||
623 | report_store.log_dispatch(amount, amounts, "medium", "repartition") | ||
624 | print_log.assert_not_called() | ||
625 | add_log.assert_called_once_with({ | ||
626 | 'type': 'dispatch', | ||
627 | 'liquidity': 'medium', | ||
628 | 'repartition_ratio': 'repartition', | ||
629 | 'total_amount': { | ||
630 | 'currency': 'BTC', | ||
631 | 'value': D('10.3') | ||
632 | }, | ||
633 | 'repartition': { | ||
634 | 'BTC': D('10'), | ||
635 | 'ETH': D('0.3') | ||
636 | } | ||
637 | }) | ||
638 | |||
639 | @mock.patch.object(market.ReportStore, "print_log") | ||
640 | @mock.patch.object(market.ReportStore, "add_log") | ||
641 | def test_log_trades(self, add_log, print_log): | ||
642 | report_store = market.ReportStore(self.m) | ||
643 | trade_mock1 = mock.Mock() | ||
644 | trade_mock2 = mock.Mock() | ||
645 | trade_mock1.as_json.return_value = { "trade": "1" } | ||
646 | trade_mock2.as_json.return_value = { "trade": "2" } | ||
647 | |||
648 | matching_and_trades = [ | ||
649 | (True, trade_mock1), | ||
650 | (False, trade_mock2), | ||
651 | ] | ||
652 | report_store.log_trades(matching_and_trades, "only") | ||
653 | |||
654 | print_log.assert_not_called() | ||
655 | add_log.assert_called_with({ | ||
656 | 'type': 'trades', | ||
657 | 'only': 'only', | ||
658 | 'debug': False, | ||
659 | 'trades': [ | ||
660 | {'trade': '1', 'skipped': False}, | ||
661 | {'trade': '2', 'skipped': True} | ||
662 | ] | ||
663 | }) | ||
664 | |||
665 | @mock.patch.object(market.ReportStore, "print_log") | ||
666 | @mock.patch.object(market.ReportStore, "add_log") | ||
667 | def test_log_orders(self, add_log, print_log): | ||
668 | report_store = market.ReportStore(self.m) | ||
669 | |||
670 | order_mock1 = mock.Mock() | ||
671 | order_mock2 = mock.Mock() | ||
672 | |||
673 | order_mock1.as_json.return_value = "order1" | ||
674 | order_mock2.as_json.return_value = "order2" | ||
675 | |||
676 | orders = [order_mock1, order_mock2] | ||
677 | |||
678 | report_store.log_orders(orders, tick="tick", | ||
679 | only="only", compute_value="compute_value") | ||
680 | |||
681 | print_log.assert_called_once_with("[Orders]") | ||
682 | self.m.trades.print_all_with_order.assert_called_once_with(ind="\t") | ||
683 | |||
684 | add_log.assert_called_with({ | ||
685 | 'type': 'orders', | ||
686 | 'only': 'only', | ||
687 | 'compute_value': 'compute_value', | ||
688 | 'tick': 'tick', | ||
689 | 'orders': ['order1', 'order2'] | ||
690 | }) | ||
691 | |||
692 | add_log.reset_mock() | ||
693 | def compute_value(x, y): | ||
694 | return x[y] | ||
695 | report_store.log_orders(orders, tick="tick", | ||
696 | only="only", compute_value=compute_value) | ||
697 | add_log.assert_called_with({ | ||
698 | 'type': 'orders', | ||
699 | 'only': 'only', | ||
700 | 'compute_value': 'def compute_value(x, y):\n return x[y]', | ||
701 | 'tick': 'tick', | ||
702 | 'orders': ['order1', 'order2'] | ||
703 | }) | ||
704 | |||
705 | |||
706 | @mock.patch.object(market.ReportStore, "print_log") | ||
707 | @mock.patch.object(market.ReportStore, "add_log") | ||
708 | def test_log_order(self, add_log, print_log): | ||
709 | report_store = market.ReportStore(self.m) | ||
710 | order_mock = mock.Mock() | ||
711 | order_mock.as_json.return_value = "order" | ||
712 | new_order_mock = mock.Mock() | ||
713 | new_order_mock.as_json.return_value = "new_order" | ||
714 | order_mock.__repr__ = mock.Mock() | ||
715 | order_mock.__repr__.return_value = "Order Mock" | ||
716 | new_order_mock.__repr__ = mock.Mock() | ||
717 | new_order_mock.__repr__.return_value = "New order Mock" | ||
718 | |||
719 | with self.subTest(finished=True): | ||
720 | report_store.log_order(order_mock, 1, finished=True) | ||
721 | print_log.assert_called_once_with("[Order] Finished Order Mock") | ||
722 | add_log.assert_called_once_with({ | ||
723 | 'type': 'order', | ||
724 | 'tick': 1, | ||
725 | 'update': None, | ||
726 | 'order': 'order', | ||
727 | 'compute_value': None, | ||
728 | 'new_order': None | ||
729 | }) | ||
730 | |||
731 | add_log.reset_mock() | ||
732 | print_log.reset_mock() | ||
733 | |||
734 | with self.subTest(update="waiting"): | ||
735 | report_store.log_order(order_mock, 1, update="waiting") | ||
736 | print_log.assert_called_once_with("[Order] Order Mock, tick 1, waiting") | ||
737 | add_log.assert_called_once_with({ | ||
738 | 'type': 'order', | ||
739 | 'tick': 1, | ||
740 | 'update': 'waiting', | ||
741 | 'order': 'order', | ||
742 | 'compute_value': None, | ||
743 | 'new_order': None | ||
744 | }) | ||
745 | |||
746 | add_log.reset_mock() | ||
747 | print_log.reset_mock() | ||
748 | with self.subTest(update="adjusting"): | ||
749 | compute_value = lambda x: (x["bid"] + x["ask"]*2)/3 | ||
750 | report_store.log_order(order_mock, 3, | ||
751 | update="adjusting", new_order=new_order_mock, | ||
752 | compute_value=compute_value) | ||
753 | print_log.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock") | ||
754 | add_log.assert_called_once_with({ | ||
755 | 'type': 'order', | ||
756 | 'tick': 3, | ||
757 | 'update': 'adjusting', | ||
758 | 'order': 'order', | ||
759 | 'compute_value': 'compute_value = lambda x: (x["bid"] + x["ask"]*2)/3', | ||
760 | 'new_order': 'new_order' | ||
761 | }) | ||
762 | |||
763 | add_log.reset_mock() | ||
764 | print_log.reset_mock() | ||
765 | with self.subTest(update="market_fallback"): | ||
766 | report_store.log_order(order_mock, 7, | ||
767 | update="market_fallback", new_order=new_order_mock) | ||
768 | print_log.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value") | ||
769 | add_log.assert_called_once_with({ | ||
770 | 'type': 'order', | ||
771 | 'tick': 7, | ||
772 | 'update': 'market_fallback', | ||
773 | 'order': 'order', | ||
774 | 'compute_value': None, | ||
775 | 'new_order': 'new_order' | ||
776 | }) | ||
777 | |||
778 | add_log.reset_mock() | ||
779 | print_log.reset_mock() | ||
780 | with self.subTest(update="market_adjusting"): | ||
781 | report_store.log_order(order_mock, 17, | ||
782 | update="market_adjust", new_order=new_order_mock) | ||
783 | print_log.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock") | ||
784 | add_log.assert_called_once_with({ | ||
785 | 'type': 'order', | ||
786 | 'tick': 17, | ||
787 | 'update': 'market_adjust', | ||
788 | 'order': 'order', | ||
789 | 'compute_value': None, | ||
790 | 'new_order': 'new_order' | ||
791 | }) | ||
792 | |||
793 | @mock.patch.object(market.ReportStore, "print_log") | ||
794 | @mock.patch.object(market.ReportStore, "add_log") | ||
795 | def test_log_move_balances(self, add_log, print_log): | ||
796 | report_store = market.ReportStore(self.m) | ||
797 | needed = { | ||
798 | "BTC": portfolio.Amount("BTC", 10), | ||
799 | "USDT": 1 | ||
800 | } | ||
801 | moving = { | ||
802 | "BTC": portfolio.Amount("BTC", 3), | ||
803 | "USDT": -2 | ||
804 | } | ||
805 | report_store.log_move_balances(needed, moving) | ||
806 | print_log.assert_not_called() | ||
807 | add_log.assert_called_once_with({ | ||
808 | 'type': 'move_balances', | ||
809 | 'debug': False, | ||
810 | 'needed': { | ||
811 | 'BTC': D('10'), | ||
812 | 'USDT': 1 | ||
813 | }, | ||
814 | 'moving': { | ||
815 | 'BTC': D('3'), | ||
816 | 'USDT': -2 | ||
817 | } | ||
818 | }) | ||
819 | |||
820 | @mock.patch.object(market.ReportStore, "print_log") | ||
821 | @mock.patch.object(market.ReportStore, "add_log") | ||
822 | def test_log_http_request(self, add_log, print_log): | ||
823 | report_store = market.ReportStore(self.m) | ||
824 | response = mock.Mock() | ||
825 | response.status_code = 200 | ||
826 | response.text = "Hey" | ||
827 | |||
828 | report_store.log_http_request("method", "url", "body", | ||
829 | "headers", response) | ||
830 | print_log.assert_not_called() | ||
831 | add_log.assert_called_once_with({ | ||
832 | 'type': 'http_request', | ||
833 | 'method': 'method', | ||
834 | 'url': 'url', | ||
835 | 'body': 'body', | ||
836 | 'headers': 'headers', | ||
837 | 'status': 200, | ||
838 | 'response': 'Hey' | ||
839 | }) | ||
840 | |||
841 | add_log.reset_mock() | ||
842 | report_store.log_http_request("method", "url", "body", | ||
843 | "headers", ValueError("Foo")) | ||
844 | add_log.assert_called_once_with({ | ||
845 | 'type': 'http_request', | ||
846 | 'method': 'method', | ||
847 | 'url': 'url', | ||
848 | 'body': 'body', | ||
849 | 'headers': 'headers', | ||
850 | 'status': -1, | ||
851 | 'response': None, | ||
852 | 'error': 'ValueError', | ||
853 | 'error_message': 'Foo', | ||
854 | }) | ||
855 | |||
856 | @mock.patch.object(market.ReportStore, "add_log") | ||
857 | def test_log_market(self, add_log): | ||
858 | report_store = market.ReportStore(self.m) | ||
859 | |||
860 | report_store.log_market(self.market_args(debug=True, quiet=False), 4, 1) | ||
861 | add_log.assert_called_once_with({ | ||
862 | "type": "market", | ||
863 | "commit": "$Format:%H$", | ||
864 | "args": { "report_path": None, "debug": True, "quiet": False }, | ||
865 | "user_id": 4, | ||
866 | "market_id": 1, | ||
867 | }) | ||
868 | |||
869 | @mock.patch.object(market.ReportStore, "print_log") | ||
870 | @mock.patch.object(market.ReportStore, "add_log") | ||
871 | def test_log_error(self, add_log, print_log): | ||
872 | report_store = market.ReportStore(self.m) | ||
873 | with self.subTest(message=None, exception=None): | ||
874 | report_store.log_error("action") | ||
875 | print_log.assert_called_once_with("[Error] action") | ||
876 | add_log.assert_called_once_with({ | ||
877 | 'type': 'error', | ||
878 | 'action': 'action', | ||
879 | 'exception_class': None, | ||
880 | 'exception_message': None, | ||
881 | 'message': None | ||
882 | }) | ||
883 | |||
884 | print_log.reset_mock() | ||
885 | add_log.reset_mock() | ||
886 | with self.subTest(message="Hey", exception=None): | ||
887 | report_store.log_error("action", message="Hey") | ||
888 | print_log.assert_has_calls([ | ||
889 | mock.call("[Error] action"), | ||
890 | mock.call("\tHey") | ||
891 | ]) | ||
892 | add_log.assert_called_once_with({ | ||
893 | 'type': 'error', | ||
894 | 'action': 'action', | ||
895 | 'exception_class': None, | ||
896 | 'exception_message': None, | ||
897 | 'message': "Hey" | ||
898 | }) | ||
899 | |||
900 | print_log.reset_mock() | ||
901 | add_log.reset_mock() | ||
902 | with self.subTest(message=None, exception=Exception("bouh")): | ||
903 | report_store.log_error("action", exception=Exception("bouh")) | ||
904 | print_log.assert_has_calls([ | ||
905 | mock.call("[Error] action"), | ||
906 | mock.call("\tException: bouh") | ||
907 | ]) | ||
908 | add_log.assert_called_once_with({ | ||
909 | 'type': 'error', | ||
910 | 'action': 'action', | ||
911 | 'exception_class': "Exception", | ||
912 | 'exception_message': "bouh", | ||
913 | 'message': None | ||
914 | }) | ||
915 | |||
916 | print_log.reset_mock() | ||
917 | add_log.reset_mock() | ||
918 | with self.subTest(message="Hey", exception=Exception("bouh")): | ||
919 | report_store.log_error("action", message="Hey", exception=Exception("bouh")) | ||
920 | print_log.assert_has_calls([ | ||
921 | mock.call("[Error] action"), | ||
922 | mock.call("\tException: bouh"), | ||
923 | mock.call("\tHey") | ||
924 | ]) | ||
925 | add_log.assert_called_once_with({ | ||
926 | 'type': 'error', | ||
927 | 'action': 'action', | ||
928 | 'exception_class': "Exception", | ||
929 | 'exception_message': "bouh", | ||
930 | 'message': "Hey" | ||
931 | }) | ||
932 | |||
933 | @mock.patch.object(market.ReportStore, "print_log") | ||
934 | @mock.patch.object(market.ReportStore, "add_log") | ||
935 | def test_log_debug_action(self, add_log, print_log): | ||
936 | report_store = market.ReportStore(self.m) | ||
937 | report_store.log_debug_action("Hey") | ||
938 | |||
939 | print_log.assert_called_once_with("[Debug] Hey") | ||
940 | add_log.assert_called_once_with({ | ||
941 | 'type': 'debug_action', | ||
942 | 'action': 'Hey' | ||
943 | }) | ||
944 | |||
945 | @unittest.skipUnless("unit" in limits, "Unit skipped") | ||
946 | class PortfolioTest(WebMockTestCase): | ||
947 | def setUp(self): | ||
948 | super().setUp() | ||
949 | |||
950 | with open("test_samples/test_portfolio.json") as example: | ||
951 | self.json_response = example.read() | ||
952 | |||
953 | self.wm.get(market.Portfolio.URL, text=self.json_response) | ||
954 | |||
955 | @mock.patch.object(market.Portfolio, "parse_cryptoportfolio") | ||
956 | def test_get_cryptoportfolio(self, parse_cryptoportfolio): | ||
957 | with self.subTest(parallel=False): | ||
958 | self.wm.get(market.Portfolio.URL, [ | ||
959 | {"text":'{ "foo": "bar" }', "status_code": 200}, | ||
960 | {"text": "System Error", "status_code": 500}, | ||
961 | {"exc": requests.exceptions.ConnectTimeout}, | ||
962 | ]) | ||
963 | market.Portfolio.get_cryptoportfolio() | ||
964 | self.assertIn("foo", market.Portfolio.data.get()) | ||
965 | self.assertEqual("bar", market.Portfolio.data.get()["foo"]) | ||
966 | self.assertTrue(self.wm.called) | ||
967 | self.assertEqual(1, self.wm.call_count) | ||
968 | market.Portfolio.report.log_error.assert_not_called() | ||
969 | market.Portfolio.report.log_http_request.assert_called_once() | ||
970 | parse_cryptoportfolio.assert_called_once_with() | ||
971 | market.Portfolio.report.log_http_request.reset_mock() | ||
972 | parse_cryptoportfolio.reset_mock() | ||
973 | market.Portfolio.data = store.LockedVar(None) | ||
974 | |||
975 | market.Portfolio.get_cryptoportfolio() | ||
976 | self.assertIsNone(market.Portfolio.data.get()) | ||
977 | self.assertEqual(2, self.wm.call_count) | ||
978 | parse_cryptoportfolio.assert_not_called() | ||
979 | market.Portfolio.report.log_error.assert_not_called() | ||
980 | market.Portfolio.report.log_http_request.assert_called_once() | ||
981 | market.Portfolio.report.log_http_request.reset_mock() | ||
982 | parse_cryptoportfolio.reset_mock() | ||
983 | |||
984 | market.Portfolio.data = store.LockedVar("Foo") | ||
985 | market.Portfolio.get_cryptoportfolio() | ||
986 | self.assertEqual(2, self.wm.call_count) | ||
987 | parse_cryptoportfolio.assert_not_called() | ||
988 | |||
989 | market.Portfolio.get_cryptoportfolio(refetch=True) | ||
990 | self.assertEqual("Foo", market.Portfolio.data.get()) | ||
991 | self.assertEqual(3, self.wm.call_count) | ||
992 | market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio", | ||
993 | exception=mock.ANY) | ||
994 | market.Portfolio.report.log_http_request.assert_not_called() | ||
995 | with self.subTest(parallel=True): | ||
996 | with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\ | ||
997 | mock.patch.object(market.Portfolio, "notify_and_wait") as notify: | ||
998 | with self.subTest(worker=True): | ||
999 | market.Portfolio.data = store.LockedVar(None) | ||
1000 | market.Portfolio.worker = mock.Mock() | ||
1001 | is_worker.return_value = True | ||
1002 | self.wm.get(market.Portfolio.URL, [ | ||
1003 | {"text":'{ "foo": "bar" }', "status_code": 200}, | ||
1004 | ]) | ||
1005 | market.Portfolio.get_cryptoportfolio() | ||
1006 | self.assertIn("foo", market.Portfolio.data.get()) | ||
1007 | parse_cryptoportfolio.reset_mock() | ||
1008 | with self.subTest(worker=False): | ||
1009 | market.Portfolio.data = store.LockedVar(None) | ||
1010 | market.Portfolio.worker = mock.Mock() | ||
1011 | is_worker.return_value = False | ||
1012 | market.Portfolio.get_cryptoportfolio() | ||
1013 | notify.assert_called_once_with() | ||
1014 | parse_cryptoportfolio.assert_not_called() | ||
1015 | |||
1016 | def test_parse_cryptoportfolio(self): | ||
1017 | with self.subTest(description="Normal case"): | ||
1018 | market.Portfolio.data = store.LockedVar(store.json.loads( | ||
1019 | self.json_response, parse_int=D, parse_float=D)) | ||
1020 | market.Portfolio.parse_cryptoportfolio() | ||
1021 | |||
1022 | self.assertListEqual( | ||
1023 | ["medium", "high"], | ||
1024 | list(market.Portfolio.liquidities.get().keys())) | ||
1025 | |||
1026 | liquidities = market.Portfolio.liquidities.get() | ||
1027 | self.assertEqual(10, len(liquidities["medium"].keys())) | ||
1028 | self.assertEqual(10, len(liquidities["high"].keys())) | ||
1029 | |||
1030 | expected = { | ||
1031 | 'BTC': (D("0.2857"), "long"), | ||
1032 | 'DGB': (D("0.1015"), "long"), | ||
1033 | 'DOGE': (D("0.1805"), "long"), | ||
1034 | 'SC': (D("0.0623"), "long"), | ||
1035 | 'ZEC': (D("0.3701"), "long"), | ||
1036 | } | ||
1037 | date = portfolio.datetime(2018, 1, 8) | ||
1038 | self.assertDictEqual(expected, liquidities["high"][date]) | ||
1039 | |||
1040 | expected = { | ||
1041 | 'BTC': (D("1.1102e-16"), "long"), | ||
1042 | 'ETC': (D("0.1"), "long"), | ||
1043 | 'FCT': (D("0.1"), "long"), | ||
1044 | 'GAS': (D("0.1"), "long"), | ||
1045 | 'NAV': (D("0.1"), "long"), | ||
1046 | 'OMG': (D("0.1"), "long"), | ||
1047 | 'OMNI': (D("0.1"), "long"), | ||
1048 | 'PPC': (D("0.1"), "long"), | ||
1049 | 'RIC': (D("0.1"), "long"), | ||
1050 | 'VIA': (D("0.1"), "long"), | ||
1051 | 'XCP': (D("0.1"), "long"), | ||
1052 | } | ||
1053 | self.assertDictEqual(expected, liquidities["medium"][date]) | ||
1054 | self.assertEqual(portfolio.datetime(2018, 1, 15), market.Portfolio.last_date.get()) | ||
1055 | |||
1056 | with self.subTest(description="Missing weight"): | ||
1057 | data = store.json.loads(self.json_response, parse_int=D, parse_float=D) | ||
1058 | del(data["portfolio_2"]["weights"]) | ||
1059 | market.Portfolio.data = store.LockedVar(data) | ||
1060 | |||
1061 | market.Portfolio.parse_cryptoportfolio() | ||
1062 | self.assertListEqual( | ||
1063 | ["medium", "high"], | ||
1064 | list(market.Portfolio.liquidities.get().keys())) | ||
1065 | self.assertEqual({}, market.Portfolio.liquidities.get("medium")) | ||
1066 | |||
1067 | with self.subTest(description="All missing weights"): | ||
1068 | data = store.json.loads(self.json_response, parse_int=D, parse_float=D) | ||
1069 | del(data["portfolio_1"]["weights"]) | ||
1070 | del(data["portfolio_2"]["weights"]) | ||
1071 | market.Portfolio.data = store.LockedVar(data) | ||
1072 | |||
1073 | market.Portfolio.parse_cryptoportfolio() | ||
1074 | self.assertEqual({}, market.Portfolio.liquidities.get("medium")) | ||
1075 | self.assertEqual({}, market.Portfolio.liquidities.get("high")) | ||
1076 | self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get()) | ||
1077 | |||
1078 | |||
1079 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | ||
1080 | def test_repartition(self, get_cryptoportfolio): | ||
1081 | market.Portfolio.liquidities = store.LockedVar({ | ||
1082 | "medium": { | ||
1083 | "2018-03-01": "medium_2018-03-01", | ||
1084 | "2018-03-08": "medium_2018-03-08", | ||
1085 | }, | ||
1086 | "high": { | ||
1087 | "2018-03-01": "high_2018-03-01", | ||
1088 | "2018-03-08": "high_2018-03-08", | ||
1089 | } | ||
1090 | }) | ||
1091 | market.Portfolio.last_date = store.LockedVar("2018-03-08") | ||
1092 | |||
1093 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition()) | ||
1094 | get_cryptoportfolio.assert_called_once_with() | ||
1095 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) | ||
1096 | self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) | ||
1097 | |||
1098 | @mock.patch.object(market.time, "sleep") | ||
1099 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | ||
1100 | def test_wait_for_recent(self, get_cryptoportfolio, sleep): | ||
1101 | self.call_count = 0 | ||
1102 | def _get(refetch=False): | ||
1103 | if self.call_count != 0: | ||
1104 | self.assertTrue(refetch) | ||
1105 | else: | ||
1106 | self.assertFalse(refetch) | ||
1107 | self.call_count += 1 | ||
1108 | market.Portfolio.last_date = store.LockedVar(store.datetime.now()\ | ||
1109 | - store.timedelta(10)\ | ||
1110 | + store.timedelta(self.call_count)) | ||
1111 | get_cryptoportfolio.side_effect = _get | ||
1112 | |||
1113 | market.Portfolio.wait_for_recent() | ||
1114 | sleep.assert_called_with(30) | ||
1115 | self.assertEqual(6, sleep.call_count) | ||
1116 | self.assertEqual(7, get_cryptoportfolio.call_count) | ||
1117 | market.Portfolio.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio") | ||
1118 | |||
1119 | sleep.reset_mock() | ||
1120 | get_cryptoportfolio.reset_mock() | ||
1121 | market.Portfolio.last_date = store.LockedVar(None) | ||
1122 | self.call_count = 0 | ||
1123 | market.Portfolio.wait_for_recent(delta=15) | ||
1124 | sleep.assert_not_called() | ||
1125 | self.assertEqual(1, get_cryptoportfolio.call_count) | ||
1126 | |||
1127 | sleep.reset_mock() | ||
1128 | get_cryptoportfolio.reset_mock() | ||
1129 | market.Portfolio.last_date = store.LockedVar(None) | ||
1130 | self.call_count = 0 | ||
1131 | market.Portfolio.wait_for_recent(delta=1) | ||
1132 | sleep.assert_called_with(30) | ||
1133 | self.assertEqual(9, sleep.call_count) | ||
1134 | self.assertEqual(10, get_cryptoportfolio.call_count) | ||
1135 | |||
1136 | def test_is_worker_thread(self): | ||
1137 | with self.subTest(worker=None): | ||
1138 | self.assertFalse(store.Portfolio.is_worker_thread()) | ||
1139 | |||
1140 | with self.subTest(worker="not self"),\ | ||
1141 | mock.patch("threading.current_thread") as current_thread: | ||
1142 | current = mock.Mock() | ||
1143 | current_thread.return_value = current | ||
1144 | store.Portfolio.worker = mock.Mock() | ||
1145 | self.assertFalse(store.Portfolio.is_worker_thread()) | ||
1146 | |||
1147 | with self.subTest(worker="self"),\ | ||
1148 | mock.patch("threading.current_thread") as current_thread: | ||
1149 | current = mock.Mock() | ||
1150 | current_thread.return_value = current | ||
1151 | store.Portfolio.worker = current | ||
1152 | self.assertTrue(store.Portfolio.is_worker_thread()) | ||
1153 | |||
1154 | def test_start_worker(self): | ||
1155 | with mock.patch.object(store.Portfolio, "wait_for_notification") as notification: | ||
1156 | store.Portfolio.start_worker() | ||
1157 | notification.assert_called_once_with(poll=30) | ||
1158 | |||
1159 | self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__) | ||
1160 | self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__) | ||
1161 | store.Portfolio.report.start_lock.assert_called_once_with() | ||
1162 | |||
1163 | self.assertIsNotNone(store.Portfolio.worker) | ||
1164 | self.assertIsNotNone(store.Portfolio.worker_notify) | ||
1165 | self.assertIsNotNone(store.Portfolio.callback) | ||
1166 | self.assertTrue(store.Portfolio.worker_started) | ||
1167 | |||
1168 | self.assertFalse(store.Portfolio.worker.is_alive()) | ||
1169 | |||
1170 | def test_wait_for_notification(self): | ||
1171 | with self.assertRaises(RuntimeError): | ||
1172 | store.Portfolio.wait_for_notification() | ||
1173 | |||
1174 | with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ | ||
1175 | mock.patch.object(store.Portfolio, "report") as report,\ | ||
1176 | mock.patch.object(store.time, "sleep") as sleep: | ||
1177 | store.Portfolio.start_worker(poll=3) | ||
1178 | |||
1179 | store.Portfolio.worker_notify.set() | ||
1180 | |||
1181 | store.Portfolio.callback.wait() | ||
1182 | |||
1183 | report.print_log.assert_called_once_with("Fetching cryptoportfolio") | ||
1184 | get.assert_called_once_with(refetch=True) | ||
1185 | sleep.assert_called_once_with(3) | ||
1186 | self.assertFalse(store.Portfolio.worker_notify.is_set()) | ||
1187 | self.assertTrue(store.Portfolio.worker.is_alive()) | ||
1188 | |||
1189 | store.Portfolio.callback.clear() | ||
1190 | store.Portfolio.worker_started = False | ||
1191 | store.Portfolio.worker_notify.set() | ||
1192 | store.Portfolio.callback.wait() | ||
1193 | self.assertFalse(store.Portfolio.worker.is_alive()) | ||
1194 | |||
1195 | def test_notify_and_wait(self): | ||
1196 | with mock.patch.object(store.Portfolio, "callback") as callback,\ | ||
1197 | mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: | ||
1198 | store.Portfolio.notify_and_wait() | ||
1199 | callback.clear.assert_called_once_with() | ||
1200 | worker_notify.set.assert_called_once_with() | ||
1201 | callback.wait.assert_called_once_with() | ||
1202 | |||
1203 | |||