mock.patch.multiple(portfolio.TradeStore,
all=[],
debug=False),
- mock.patch.multiple(portfolio.Portfolio, data=None, liquidities={}),
+ mock.patch.multiple(portfolio.Portfolio, last_date=None, data=None, liquidities={}),
mock.patch.multiple(portfolio.Computation,
computations=portfolio.Computation.computations),
mock.patch.multiple(helper,
'SC': (D("0.0623"), "long"),
'ZEC': (D("0.3701"), "long"),
}
- self.assertDictEqual(expected, liquidities["high"]['2018-01-08'])
+ date = portfolio.datetime(2018, 1, 8)
+ self.assertDictEqual(expected, liquidities["high"][date])
expected = {
'BTC': (D("1.1102e-16"), "long"),
'VIA': (D("0.1"), "long"),
'XCP': (D("0.1"), "long"),
}
- self.assertDictEqual(expected, liquidities["medium"]['2018-01-08'])
+ self.assertDictEqual(expected, liquidities["medium"][date])
+ self.assertEqual(portfolio.datetime(2018, 1, 15), portfolio.Portfolio.last_date)
# It doesn't refetch the data when available
portfolio.Portfolio.parse_cryptoportfolio()
self.assertEqual(1, self.wm.call_count)
+ portfolio.Portfolio.parse_cryptoportfolio(refetch=True)
+ self.assertEqual(2, self.wm.call_count)
+
def test_repartition(self):
expected_medium = {
'BTC': (D("1.1102e-16"), "long"),
self.assertEqual(expected_medium, portfolio.Portfolio.repartition(liquidity="medium"))
self.assertEqual(expected_high, portfolio.Portfolio.repartition(liquidity="high"))
+ self.assertEqual(1, self.wm.call_count)
+
+ portfolio.Portfolio.repartition()
+ self.assertEqual(1, self.wm.call_count)
+
+ portfolio.Portfolio.repartition(refetch=True)
+ self.assertEqual(2, self.wm.call_count)
+
+ @mock.patch.object(portfolio.time, "sleep")
+ @mock.patch.object(portfolio.Portfolio, "repartition")
+ def test_wait_for_recent(self, repartition, sleep):
+ self.call_count = 0
+ def _repartition(refetch):
+ self.assertTrue(refetch)
+ self.call_count += 1
+ portfolio.Portfolio.last_date = portfolio.datetime.now()\
+ - portfolio.timedelta(10)\
+ + portfolio.timedelta(self.call_count)
+ repartition.side_effect = _repartition
+
+ portfolio.Portfolio.wait_for_recent()
+ sleep.assert_called_with(30)
+ self.assertEqual(6, sleep.call_count)
+ self.assertEqual(7, repartition.call_count)
+
+ sleep.reset_mock()
+ repartition.reset_mock()
+ portfolio.Portfolio.last_date = None
+ self.call_count = 0
+ portfolio.Portfolio.wait_for_recent(delta=15)
+ sleep.assert_not_called()
+ self.assertEqual(1, repartition.call_count)
+
+ sleep.reset_mock()
+ repartition.reset_mock()
+ portfolio.Portfolio.last_date = None
+ self.call_count = 0
+ portfolio.Portfolio.wait_for_recent(delta=1)
+ sleep.assert_called_with(30)
+ self.assertEqual(9, sleep.call_count)
+ self.assertEqual(10, repartition.call_count)
+
@unittest.skipUnless("unit" in limits, "Unit skipped")
class AmountTest(WebMockTestCase):
def test_values(self):
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 + amount4)
+ self.assertEqual(amount1, amount1 + 0)
+
def test__radd(self):
amount = portfolio.Amount("XVG", "12.9")
amount4 = portfolio.Amount("ETH", 0.0)
self.assertEqual(amount1, amount1 - amount4)
+ def test__rsub(self):
+ amount = portfolio.Amount("ETH", "1.6")
+ with self.assertRaises(Exception):
+ 3 - amount
+
+ self.assertEqual(portfolio.Amount("ETH", "-1.6"), -amount)
+
def test__mul(self):
amount = portfolio.Amount("XEM", 11)
portfolio.TradeStore.all = [trade1, trade2, trade3]
balance1 = portfolio.Balance("BTC", { "margin_free": "0" })
balance2 = portfolio.Balance("USDT", { "margin_free": "100" })
- portfolio.BalanceStore.all = {"BTC": balance1, "USDT": balance2}
+ balance3 = portfolio.Balance("ETC", { "margin_free": "10" })
+ portfolio.BalanceStore.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3}
market = mock.Mock()
else:
market.transfer_balance.assert_any_call("BTC", 3, "exchange", "margin")
market.transfer_balance.assert_any_call("USDT", 50, "margin", "exchange")
+ market.transfer_balance.assert_any_call("ETC", 10, "margin", "exchange")
@mock.patch.object(helper, "prepare_trades")
@mock.patch.object(portfolio.TradeStore, "prepare_orders")
}
helper.print_orders(market)
prepare_trades.assert_called_with(market, base_currency="BTC",
- compute_value="average")
+ compute_value="average", debug=True)
prepare_orders.assert_called_with(compute_value="average")
print_all_with_order.assert_called()
self.assertRegex(stdout_mock.getvalue(), "Balance")
+ @mock.patch.object(portfolio.BalanceStore, "fetch_balances")
+ @mock.patch.object(portfolio.BalanceStore, "in_currency")
+ @mock.patch('sys.stdout', new_callable=StringIO)
+ def test_print_balances(self, stdout_mock, in_currency, fetch_balances):
+ market = mock.Mock()
+ portfolio.BalanceStore.all = {
+ "BTC": portfolio.Balance("BTC", {
+ "total": "0.65",
+ "exchange_total":"0.65",
+ "exchange_free": "0.35",
+ "exchange_used": "0.30"}),
+ "ETH": portfolio.Balance("ETH", {
+ "total": 3,
+ "exchange_total": 3,
+ "exchange_free": 3,
+ "exchange_used": 0}),
+ }
+ in_currency.return_value = {
+ "BTC": portfolio.Amount("BTC", "0.65"),
+ "ETH": portfolio.Amount("BTC", "0.3"),
+ }
+ helper.print_balances(market)
+ fetch_balances.assert_called_with(market)
+ self.assertRegex(stdout_mock.getvalue(), "Balance")
+ self.assertRegex(stdout_mock.getvalue(), "0.95000000 BTC")
+
+ @mock.patch.object(helper, "prepare_trades")
+ @mock.patch.object(helper, "follow_orders")
+ @mock.patch.object(portfolio.TradeStore, "prepare_orders")
+ @mock.patch.object(portfolio.TradeStore, "print_all_with_order")
+ @mock.patch.object(portfolio.TradeStore, "run_orders")
+ @mock.patch('sys.stdout', new_callable=StringIO)
+ def test_process_sell_needed__1_sell(self, stdout_mock, run_orders,
+ print_all_with_order, prepare_orders, follow_orders,
+ prepare_trades):
+ market = mock.Mock()
+ portfolio.BalanceStore.all = {
+ "BTC": portfolio.Balance("BTC", {
+ "total": "0.65",
+ "exchange_total":"0.65",
+ "exchange_free": "0.35",
+ "exchange_used": "0.30"}),
+ "ETH": portfolio.Balance("ETH", {
+ "total": 3,
+ "exchange_total": 3,
+ "exchange_free": 3,
+ "exchange_used": 0}),
+ }
+ helper.process_sell_needed__1_sell(market)
+ prepare_trades.assert_called_with(market, base_currency="BTC",
+ debug=False)
+ prepare_orders.assert_called_with(compute_value="average",
+ only="dispose")
+ print_all_with_order.assert_called()
+ run_orders.assert_called()
+ follow_orders.assert_called()
+ self.assertRegex(stdout_mock.getvalue(), "Balance")
+
+ @mock.patch.object(helper, "update_trades")
+ @mock.patch.object(helper, "follow_orders")
+ @mock.patch.object(helper, "move_balances")
+ @mock.patch.object(portfolio.TradeStore, "prepare_orders")
+ @mock.patch.object(portfolio.TradeStore, "print_all_with_order")
+ @mock.patch.object(portfolio.TradeStore, "run_orders")
+ @mock.patch('sys.stdout', new_callable=StringIO)
+ def test_process_sell_needed__2_buy(self, stdout_mock, run_orders,
+ print_all_with_order, prepare_orders, move_balances,
+ follow_orders, update_trades):
+ market = mock.Mock()
+ portfolio.BalanceStore.all = {
+ "BTC": portfolio.Balance("BTC", {
+ "total": "0.65",
+ "exchange_total":"0.65",
+ "exchange_free": "0.35",
+ "exchange_used": "0.30"}),
+ "ETH": portfolio.Balance("ETH", {
+ "total": 3,
+ "exchange_total": 3,
+ "exchange_free": 3,
+ "exchange_used": 0}),
+ }
+ helper.process_sell_needed__2_buy(market)
+ update_trades.assert_called_with(market, base_currency="BTC",
+ debug=False, only="acquire")
+ prepare_orders.assert_called_with(compute_value="average",
+ only="acquire")
+ print_all_with_order.assert_called()
+ move_balances.assert_called_with(market, debug=False)
+ run_orders.assert_called()
+ follow_orders.assert_called()
+ self.assertRegex(stdout_mock.getvalue(), "Balance")
+
+ @mock.patch.object(helper, "prepare_trades_to_sell_all")
+ @mock.patch.object(helper, "follow_orders")
+ @mock.patch.object(portfolio.TradeStore, "prepare_orders")
+ @mock.patch.object(portfolio.TradeStore, "print_all_with_order")
+ @mock.patch.object(portfolio.TradeStore, "run_orders")
+ @mock.patch('sys.stdout', new_callable=StringIO)
+ def test_process_sell_all__1_sell(self, stdout_mock, run_orders,
+ print_all_with_order, prepare_orders, follow_orders,
+ prepare_trades_to_sell_all):
+ market = mock.Mock()
+ portfolio.BalanceStore.all = {
+ "BTC": portfolio.Balance("BTC", {
+ "total": "0.65",
+ "exchange_total":"0.65",
+ "exchange_free": "0.35",
+ "exchange_used": "0.30"}),
+ "ETH": portfolio.Balance("ETH", {
+ "total": 3,
+ "exchange_total": 3,
+ "exchange_free": 3,
+ "exchange_used": 0}),
+ }
+ helper.process_sell_all__1_all_sell(market)
+ prepare_trades_to_sell_all.assert_called_with(market, base_currency="BTC",
+ debug=False)
+ prepare_orders.assert_called_with(compute_value="average")
+ print_all_with_order.assert_called()
+ run_orders.assert_called()
+ follow_orders.assert_called()
+ self.assertRegex(stdout_mock.getvalue(), "Balance")
+
+ @mock.patch.object(helper, "prepare_trades")
+ @mock.patch.object(helper, "follow_orders")
+ @mock.patch.object(helper, "move_balances")
+ @mock.patch.object(portfolio.TradeStore, "prepare_orders")
+ @mock.patch.object(portfolio.TradeStore, "print_all_with_order")
+ @mock.patch.object(portfolio.TradeStore, "run_orders")
+ @mock.patch('sys.stdout', new_callable=StringIO)
+ def test_process_sell_all__2_all_buy(self, stdout_mock, run_orders,
+ print_all_with_order, prepare_orders, move_balances,
+ follow_orders, prepare_trades):
+ market = mock.Mock()
+ portfolio.BalanceStore.all = {
+ "BTC": portfolio.Balance("BTC", {
+ "total": "0.65",
+ "exchange_total":"0.65",
+ "exchange_free": "0.35",
+ "exchange_used": "0.30"}),
+ "ETH": portfolio.Balance("ETH", {
+ "total": 3,
+ "exchange_total": 3,
+ "exchange_free": 3,
+ "exchange_used": 0}),
+ }
+ helper.process_sell_all__2_all_buy(market)
+ prepare_trades.assert_called_with(market, base_currency="BTC",
+ debug=False)
+ prepare_orders.assert_called_with()
+ print_all_with_order.assert_called()
+ move_balances.assert_called_with(market, debug=False)
+ run_orders.assert_called()
+ follow_orders.assert_called()
+ self.assertRegex(stdout_mock.getvalue(), "Balance")
+
@unittest.skipUnless("unit" in limits, "Unit skipped")
class TradeStoreTest(WebMockTestCase):