5 import market
, portfolio
, store
7 @unittest.skipUnless("unit" in limits
, "Unit skipped")
8 class NoopLockTest(unittest
.TestCase
):
10 noop_lock
= store
.NoopLock()
14 @unittest.skipUnless("unit" in limits
, "Unit skipped")
15 class LockedVarTest(unittest
.TestCase
):
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
)
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"))
33 locked_var
= store
.LockedVar("Foo")
35 self
.assertEqual("Bar", locked_var
.get())
37 def test__getattr(self
):
38 dummy
= type('Dummy', (object,), {})()
39 dummy
.attribute
= "Hey"
41 locked_var
= store
.LockedVar(dummy
)
42 self
.assertEqual("Hey", locked_var
.attribute
)
43 with self
.assertRaises(AttributeError):
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
__)
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"])
60 self
.assertEqual("Foo", locked_var
.val
)
64 self
.assertEqual("Bar", locked_var
.get()[0:3])
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"]
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")),
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")),
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
95 trade_store
.compute_trades(values_in_base
,
96 new_repartition
, only
="only")
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")
103 def test_trade_if_matching(self
):
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")),
111 self
.assertEqual(False, result
[0])
112 self
.assertIsInstance(result
[1], portfolio
.Trade
)
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")),
120 self
.assertEqual(True, result
[0])
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])
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])
138 def test_prepare_orders(self
):
139 trade_store
= market
.TradeStore(self
.m
)
141 trade_mock1
= mock
.Mock()
142 trade_mock2
= mock
.Mock()
143 trade_mock3
= mock
.Mock()
145 trade_mock1
.prepare_order
.return_value
= 1
146 trade_mock2
.prepare_order
.return_value
= 2
147 trade_mock3
.prepare_order
.return_value
= 3
149 trade_mock1
.pending
= True
150 trade_mock2
.pending
= True
151 trade_mock3
.pending
= False
153 trade_store
.all
.append(trade_mock1
)
154 trade_store
.all
.append(trade_mock2
)
155 trade_store
.all
.append(trade_mock3
)
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")
163 self
.m
.report
.log_orders
.reset_mock()
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")
170 trade_mock1
.prepare_order
.reset_mock()
171 trade_mock2
.prepare_order
.reset_mock()
172 self
.m
.report
.log_orders
.reset_mock()
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")
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
]
188 trade_store
.print_all_with_order()
190 trade_mock1
.print_with_order
.assert_called()
191 trade_mock2
.print_with_order
.assert_called()
192 trade_mock3
.print_with_order
.assert_called()
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
)
201 all_orders
.return_value
= [order_mock1
, order_mock2
, order_mock3
]
203 trade_store
.run_orders()
205 all_orders
.assert_called_with(state
="pending")
207 order_mock1
.run
.assert_called()
208 order_mock2
.run
.assert_called()
209 order_mock3
.run
.assert_called()
211 self
.m
.report
.log_stage
.assert_called_with("run_orders")
212 self
.m
.report
.log_orders
.assert_called_with([order_mock1
, order_mock2
,
215 def test_all_orders(self
):
216 trade_mock1
= mock
.Mock()
217 trade_mock2
= mock
.Mock()
219 order_mock1
= mock
.Mock()
220 order_mock2
= mock
.Mock()
221 order_mock3
= mock
.Mock()
223 trade_mock1
.orders
= [order_mock1
, order_mock2
]
224 trade_mock2
.orders
= [order_mock3
]
226 order_mock1
.status
= "pending"
227 order_mock2
.status
= "open"
228 order_mock3
.status
= "open"
230 trade_store
= market
.TradeStore(self
.m
)
231 trade_store
.all
.append(trade_mock1
)
232 trade_store
.all
.append(trade_mock2
)
234 orders
= trade_store
.all_orders()
235 self
.assertEqual(3, len(orders
))
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
)
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()
247 all_orders
.return_value
= [order_mock1
, order_mock2
, order_mock3
]
249 trade_store
= market
.TradeStore(self
.m
)
251 trade_store
.update_all_orders_status()
252 all_orders
.assert_called_with(state
="open")
254 order_mock1
.get_status
.assert_called()
255 order_mock2
.get_status
.assert_called()
256 order_mock3
.get_status
.assert_called()
258 def test_close_trades(self
):
259 trade_mock1
= mock
.Mock()
260 trade_mock2
= mock
.Mock()
261 trade_mock3
= mock
.Mock()
263 trade_store
= market
.TradeStore(self
.m
)
265 trade_store
.all
.append(trade_mock1
)
266 trade_store
.all
.append(trade_mock2
)
267 trade_store
.all
.append(trade_mock3
)
269 trade_store
.close_trades()
271 trade_mock1
.close
.assert_called_once_with()
272 trade_mock2
.close
.assert_called_once_with()
273 trade_mock3
.close
.assert_called_once_with()
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
283 trade_store
= market
.TradeStore(self
.m
)
285 trade_store
.all
.append(trade_mock1
)
286 trade_store
.all
.append(trade_mock2
)
287 trade_store
.all
.append(trade_mock3
)
289 self
.assertEqual([trade_mock1
, trade_mock2
], trade_store
.pending
)
291 @unittest.skipUnless("unit" in limits
, "Unit skipped")
292 class BalanceStoreTest(WebMockTestCase
):
296 self
.fetch_balance
= {
304 "exchange_free": D("6.0"),
305 "exchange_used": D("1.2"),
306 "exchange_total": D("7.2"),
312 "exchange_total": 16,
319 "margin_total": D("-1.0"),
324 def test_in_currency(self
):
325 self
.m
.get_ticker
.return_value
= {
331 balance_store
= market
.BalanceStore(self
.m
)
332 balance_store
.all
= {
333 "BTC": portfolio
.Balance("BTC", {
335 "exchange_total":"0.65",
336 "exchange_free": "0.35",
337 "exchange_used": "0.30"}),
338 "ETH": portfolio
.Balance("ETH", {
342 "exchange_used": 0}),
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",
351 self
.m
.report
.log_tickers
.reset_mock()
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",
358 self
.m
.report
.log_tickers
.reset_mock()
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()
367 def test_fetch_balances(self
):
368 self
.m
.ccxt
.fetch_all_balances
.return_value
= self
.fetch_balance
370 balance_store
= market
.BalanceStore(self
.m
)
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()))
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)
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',
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
= {
401 balance_store
.fetch_balances(add_portfolio
=True)
402 self
.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store
.currencies()))
404 self
.m
.ccxt
.fetch_all_balances
.return_value
= {
414 "exchange_total": 16,
421 "margin_total": D("-1.0"),
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
= {
433 balance_store
.fetch_balances(add_usdt
=True)
434 self
.assertListEqual(["XVG", "XMR", "USDT"], list(balance_store
.currencies()))
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
440 balance_store
= market
.BalanceStore(self
.m
)
441 balance_store
.fetch_balances()
443 self
.assertNotIn("XEM", balance_store
.currencies())
446 "XEM": (D("0.75"), "long"),
447 "BTC": (D("0.26"), "long"),
448 "DASH": (D("0.10"), "short"),
450 repartition
.return_value
= repartition_hash
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
)
462 def test_currencies(self
):
463 balance_store
= market
.BalanceStore(self
.m
)
465 balance_store
.all
= {
466 "BTC": portfolio
.Balance("BTC", {
468 "exchange_total":"0.65",
469 "exchange_free": "0.35",
470 "exchange_used": "0.30"}),
471 "ETH": portfolio
.Balance("ETH", {
475 "exchange_used": 0}),
477 self
.assertListEqual(["BTC", "ETH"], list(balance_store
.currencies()))
479 def test_as_json(self
):
480 balance_mock1
= mock
.Mock()
481 balance_mock1
.as_json
.return_value
= 1
483 balance_mock2
= mock
.Mock()
484 balance_mock2
.as_json
.return_value
= 2
486 balance_store
= market
.BalanceStore(self
.m
)
487 balance_store
.all
= {
488 "BTC": balance_mock1
,
489 "ETH": balance_mock2
,
492 as_json
= balance_store
.as_json()
493 self
.assertEqual(1, as_json
["BTC"])
494 self
.assertEqual(2, as_json
["ETH"])
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
):
502 report_store
= market
.ReportStore(self
.m
)
503 result
= report_store
.add_log({"foo": "bar"}
)
505 self
.assertEqual({"foo": "bar", "date": mock.ANY, "user_id": 1, "market_id": 3}
, result
)
506 self
.assertEqual(result
, report_store
.logs
[0])
508 with self
.subTest(market
=None):
509 report_store
= market
.ReportStore(None)
510 result
= report_store
.add_log({"foo": "bar"}
)
512 self
.assertEqual({"foo": "bar", "date": mock.ANY, "user_id": None, "market_id": None}
, result
)
514 def test_add_redis_status(self
):
515 report_store
= market
.ReportStore(self
.m
)
516 result
= report_store
.add_redis_status({"foo": "bar"}
)
518 self
.assertEqual({"foo": "bar"}
, result
)
519 self
.assertEqual(result
, report_store
.redis_status
[0])
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
)
527 with self
.subTest(verbose
=False):
528 report_store
.set_verbose(False)
529 self
.assertFalse(report_store
.verbose_print
)
531 def test_merge(self
):
534 report_store1
= market
.ReportStore(self
.m
, verbose_print
=False)
535 report_store2
= market
.ReportStore(None, verbose_print
=False)
537 report_store2
.log_stage("1")
538 report_store1
.log_stage("2")
539 report_store2
.log_stage("3")
541 report_store1
.merge(report_store2
)
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
))
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")
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(), "")
565 def test_default_json_serial(self
):
566 report_store
= market
.ReportStore(self
.m
)
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)))
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())
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"
587 report_store
.logs
.append({
588 "date": "date2", "type": "type2", "foo": "bar", "bla": "bla"
590 logs
= list(report_store
.to_json_array())
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])
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"
601 report_store
.redis_status
.append({
602 "type": "type2", "foo": "bar", "bla": "bla"
604 logs
= list(report_store
.to_json_redis())
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])
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
)
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')}"),
620 add_log
.assert_called_once_with({
625 'c': 'c = lambda x: x',
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" }
641 with self
.subTest(tickers
=None):
642 report_store
.log_balances(tag
="tag")
643 print_log
.assert_has_calls([
644 mock
.call("[Balance]"),
648 add_log
.assert_called_once_with({
654 add_redis_status
.assert_called_once_with({
661 add_redis_status
.reset_mock()
662 with self
.subTest(tickers
="present"):
664 "BTC": portfolio
.Amount("BTC", 10),
665 "ETH": portfolio
.Amount("BTC", D("0.3"))
667 amounts
["ETH"].rate
= D("0.1")
669 report_store
.log_balances(tag
="tag", tickers
=amounts
,
670 ticker_currency
="BTC", compute_value
="default",
672 add_log
.assert_called_once_with({
678 'compute_value': 'default',
679 'balance_type': 'total',
692 add_redis_status
.assert_called_once_with({
698 'compute_value': 'default',
699 'balance_type': 'total',
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
)
718 "BTC": portfolio
.Amount("BTC", 10),
719 "ETH": portfolio
.Amount("BTC", D("0.3"))
721 amounts
["ETH"].rate
= D("0.1")
723 report_store
.log_tickers(amounts
, "BTC", "default", "total")
724 print_log
.assert_not_called()
725 add_log
.assert_called_once_with({
727 'compute_value': 'default',
728 'balance_type': 'total',
742 compute_value
= lambda x
: x
["bid"]
743 report_store
.log_tickers(amounts
, "BTC", compute_value
, "total")
744 add_log
.assert_called_once_with({
746 'compute_value': 'compute_value = lambda x: x["bid"]',
747 'balance_type': 'total',
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")
766 "BTC": portfolio
.Amount("BTC", 10),
767 "ETH": portfolio
.Amount("BTC", D("0.3"))
769 report_store
.log_dispatch(amount
, amounts
, "medium", "repartition")
770 print_log
.assert_not_called()
771 add_log
.assert_called_once_with({
773 'liquidity': 'medium',
774 'repartition_ratio': 'repartition',
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" }
794 matching_and_trades
= [
796 (False, trade_mock2
),
798 report_store
.log_trades(matching_and_trades
, "only")
800 print_log
.assert_not_called()
801 add_log
.assert_called_with({
806 {'trade': '1', 'skipped': False}
,
807 {'trade': '2', 'skipped': True}
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
)
816 order_mock1
= mock
.Mock()
817 order_mock2
= mock
.Mock()
819 order_mock1
.as_json
.return_value
= "order1"
820 order_mock2
.as_json
.return_value
= "order2"
822 orders
= [order_mock1
, order_mock2
]
824 report_store
.log_orders(orders
, tick
="tick",
825 only
="only", compute_value
="compute_value")
827 print_log
.assert_called_once_with("[Orders]")
828 self
.m
.trades
.print_all_with_order
.assert_called_once_with(ind
="\t")
830 add_log
.assert_called_with({
833 'compute_value': 'compute_value',
835 'orders': ['order1', 'order2']
839 def compute_value(x
, y
):
841 report_store
.log_orders(orders
, tick
="tick",
842 only
="only", compute_value
=compute_value
)
843 add_log
.assert_called_with({
846 'compute_value': 'def compute_value(x, y):\n return x[y]',
848 'orders': ['order1', 'order2']
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"
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({
873 'compute_value': None,
878 print_log
.reset_mock()
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({
888 'compute_value': None,
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({
903 'update': 'adjusting',
905 'compute_value': 'compute_value = lambda x: (x["bid"] + x["ask"]*2)/3',
906 'new_order': 'new_order'
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({
918 'update': 'market_fallback',
920 'compute_value': None,
921 'new_order': 'new_order'
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({
933 'update': 'market_adjust',
935 'compute_value': None,
936 'new_order': 'new_order'
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
)
944 "BTC": portfolio
.Amount("BTC", 10),
948 "BTC": portfolio
.Amount("BTC", 3),
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',
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
974 report_store
.log_http_request("method", "url", "body",
976 add_log
.assert_called_once_with({
977 'type': 'http_request',
981 'headers': 'headers',
985 'response_same_as': None,
989 report_store
.log_http_request("method", "url", "body",
990 "headers", ValueError("Foo"))
991 add_log
.assert_called_once_with({
992 'type': 'http_request',
996 'headers': 'headers',
999 'error': 'ValueError',
1000 'error_message': 'Foo',
1003 with self
.subTest(no_http_dup
=True, duplicate
=True):
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"):
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"):
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
1036 response2
= mock
.Mock()
1037 response2
.status_code
= 200
1038 response2
.text
= "Hey there!"
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"])
1052 @mock.patch.object(market
.ReportStore
, "add_log")
1053 def test_log_market(self
, add_log
):
1054 report_store
= market
.ReportStore(self
.m
)
1056 report_store
.log_market(self
.market_args(debug
=True, quiet
=False))
1057 add_log
.assert_called_once_with({
1059 "commit": "$Format:%H$",
1060 "args": { "report_path": None, "debug": True, "quiet": False }
,
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({
1073 'exception_class': None,
1074 'exception_message': None,
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"),
1086 add_log
.assert_called_once_with({
1089 'exception_class': None,
1090 'exception_message': None,
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")
1102 add_log
.assert_called_once_with({
1105 'exception_class': "Exception",
1106 'exception_message': "bouh",
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"),
1119 add_log
.assert_called_once_with({
1122 'exception_class': "Exception",
1123 'exception_message': "bouh",
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")
1133 print_log
.assert_called_once_with("[Debug] Hey")
1134 add_log
.assert_called_once_with({
1135 'type': 'debug_action',
1139 @unittest.skipUnless("unit" in limits
, "Unit skipped")
1140 class PortfolioTest(WebMockTestCase
):
1144 with open("test_samples/test_portfolio.json") as example
:
1145 self
.json_response
= example
.read()
1147 self
.wm
.get(market
.Portfolio
.URL
, text
=self
.json_response
)
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},
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)
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()
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()
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",
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},
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()
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()
1224 self
.assertListEqual(
1226 list(market
.Portfolio
.liquidities
.get().keys()))
1228 liquidities
= market
.Portfolio
.liquidities
.get()
1229 self
.assertEqual(10, len(liquidities
["medium"].keys()))
1230 self
.assertEqual(10, len(liquidities
["high"].keys()))
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"),
1239 date
= portfolio
.datetime
.datetime(2018, 1, 8)
1240 self
.assertDictEqual(expected
, liquidities
["high"][date
])
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"),
1255 self
.assertDictEqual(expected
, liquidities
["medium"][date
])
1256 self
.assertEqual(portfolio
.datetime
.datetime(2018, 1, 15), market
.Portfolio
.last_date
.get())
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
)
1263 market
.Portfolio
.parse_cryptoportfolio()
1264 self
.assertListEqual(
1266 list(market
.Portfolio
.liquidities
.get().keys()))
1267 self
.assertEqual({}, market
.Portfolio
.liquidities
.get("medium"))
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
)
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())
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({
1285 datetime
.datetime(2018,3,1): "medium_2018-03-01",
1286 datetime
.datetime(2018,3,8): "medium_2018-03-08",
1289 datetime
.datetime(2018,3,1): "high_2018-03-01",
1290 datetime
.datetime(2018,3,8): "high_2018-03-08",
1293 store
.Portfolio
.last_date
= store
.LockedVar(datetime
.datetime(2018,3,8))
1295 with self
.subTest(redis_connected
=False):
1296 redis_connected
.return_value
= False
1297 store
.Portfolio
.store_cryptoportfolio()
1298 redis
.set.assert_not_called()
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"),
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())
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
)
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" }',
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' }
,
1338 self
.assertEqual(expected_liquidities
, store
.Portfolio
.liquidities
.get())
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({
1346 "2018-03-01": "medium_2018-03-01",
1347 "2018-03-08": "medium_2018-03-08",
1350 "2018-03-01": "high_2018-03-01",
1351 "2018-03-08": "high_2018-03-08",
1354 market
.Portfolio
.last_date
= store
.LockedVar("2018-03-08")
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"))
1362 retrieve_cryptoportfolio
.reset_mock()
1363 get_cryptoportfolio
.reset_mock()
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()
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
):
1374 def _get(refetch
=False):
1375 if self
.call_count
!= 0:
1376 self
.assertTrue(refetch
)
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
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")
1392 get_cryptoportfolio
.reset_mock()
1393 market
.Portfolio
.last_date
= store
.LockedVar(None)
1395 market
.Portfolio
.wait_for_recent(delta
=15)
1396 sleep
.assert_not_called()
1397 self
.assertEqual(1, get_cryptoportfolio
.call_count
)
1400 get_cryptoportfolio
.reset_mock()
1401 market
.Portfolio
.last_date
= store
.LockedVar(None)
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
)
1408 def test_is_worker_thread(self
):
1409 with self
.subTest(worker
=None):
1410 self
.assertFalse(store
.Portfolio
.is_worker_thread())
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())
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())
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)
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()
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
)
1440 self
.assertFalse(store
.Portfolio
.worker
.is_alive())
1441 self
.assertEqual(1, threading
.active_count())
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())
1455 def test_wait_for_notification(self
):
1456 with self
.assertRaises(RuntimeError):
1457 store
.Portfolio
.wait_for_notification()
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)
1464 store
.Portfolio
.worker_notify
.set()
1466 store
.Portfolio
.callback
.wait()
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())
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())
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()