+ mouvement = portfolio.Mouvement("ETH", "BTC", { "foo": "bar" })
+ self.assertIsNone(mouvement.date)
+ self.assertIsNone(mouvement.id)
+ self.assertIsNone(mouvement.action)
+ self.assertEqual(-1, mouvement.fee_rate)
+ self.assertEqual(0, mouvement.rate)
+ self.assertEqual(portfolio.Amount("ETH", 0), mouvement.total)
+ self.assertEqual(portfolio.Amount("BTC", 0), mouvement.total_in_base)
+
+ def test__repr(self):
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ self.assertEqual("Mouvement(2017-12-30 12:00:12 ; buy 10.00000000 ETH (1.00000000 BTC) fee: 0.1500%)", repr(mouvement))
+
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy",
+ "date": "garbage", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ self.assertEqual("Mouvement(No date ; buy 10.00000000 ETH (1.00000000 BTC))", repr(mouvement))
+
+ def test_as_json(self):
+ mouvement = portfolio.Mouvement("ETH", "BTC", {
+ "tradeID": 42, "type": "buy", "fee": "0.0015",
+ "date": "2017-12-30 12:00:12", "rate": "0.1",
+ "amount": "10", "total": "1"
+ })
+ as_json = mouvement.as_json()
+
+ self.assertEqual(D("0.0015"), as_json["fee_rate"])
+ self.assertEqual(portfolio.datetime(2017, 12, 30, 12, 0, 12), as_json["date"])
+ self.assertEqual("buy", as_json["action"])
+ self.assertEqual(D("10"), as_json["total"])
+ self.assertEqual(D("1"), as_json["total_in_base"])
+ self.assertEqual("BTC", as_json["base_currency"])
+ self.assertEqual("ETH", as_json["currency"])
+
+@unittest.skipUnless("unit" in limits, "Unit skipped")
+class ReportStoreTest(WebMockTestCase):
+ def test_add_log(self):
+ portfolio.ReportStore.add_log({"foo": "bar"})
+
+ self.assertEqual({"foo": "bar", "date": mock.ANY}, portfolio.ReportStore.logs[0])
+
+ def test_set_verbose(self):
+ with self.subTest(verbose=True):
+ portfolio.ReportStore.set_verbose(True)
+ self.assertTrue(portfolio.ReportStore.verbose_print)
+
+ with self.subTest(verbose=False):
+ portfolio.ReportStore.set_verbose(False)
+ self.assertFalse(portfolio.ReportStore.verbose_print)
+
+ def test_print_log(self):
+ with self.subTest(verbose=True),\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ portfolio.ReportStore.set_verbose(True)
+ portfolio.ReportStore.print_log("Coucou")
+ portfolio.ReportStore.print_log(portfolio.Amount("BTC", 1))
+ self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n")
+
+ with self.subTest(verbose=False),\
+ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ portfolio.ReportStore.set_verbose(False)
+ portfolio.ReportStore.print_log("Coucou")
+ portfolio.ReportStore.print_log(portfolio.Amount("BTC", 1))
+ self.assertEqual(stdout_mock.getvalue(), "")
+
+ def test_to_json(self):
+ portfolio.ReportStore.logs.append({"foo": "bar"})
+ self.assertEqual('[{"foo": "bar"}]', portfolio.ReportStore.to_json())
+ portfolio.ReportStore.logs.append({"date": portfolio.datetime(2018, 2, 24)})
+ self.assertEqual('[{"foo": "bar"}, {"date": "2018-02-24T00:00:00"}]', portfolio.ReportStore.to_json())
+ portfolio.ReportStore.logs.append({"amount": portfolio.Amount("BTC", 1)})
+ with self.assertRaises(TypeError):
+ portfolio.ReportStore.to_json()
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_stage(self, add_log, print_log):
+ portfolio.ReportStore.log_stage("foo")
+ print_log.assert_has_calls([
+ mock.call("-----------"),
+ mock.call("[Stage] foo"),
+ ])
+ add_log.assert_called_once_with({'type': 'stage', 'stage': 'foo'})
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ @mock.patch("store.BalanceStore")
+ def test_log_balances(self, balance_store, add_log, print_log):
+ balance_store.as_json.return_value = "json"
+ balance_store.all = { "FOO": "bar", "BAR": "baz" }
+
+ portfolio.ReportStore.log_balances("market", tag="tag")
+ print_log.assert_has_calls([
+ mock.call("[Balance]"),
+ mock.call("\tbar"),
+ mock.call("\tbaz"),
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'balance',
+ 'balances': 'json',
+ 'tag': 'tag'
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_tickers(self, add_log, print_log):
+ market = mock.Mock()
+ amounts = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "ETH": portfolio.Amount("BTC", D("0.3"))
+ }
+ amounts["ETH"].rate = D("0.1")
+
+ portfolio.ReportStore.log_tickers(market, amounts, "BTC", "default", "total")
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'tickers',
+ 'compute_value': 'default',
+ 'balance_type': 'total',
+ 'currency': 'BTC',
+ 'balances': {
+ 'BTC': D('10'),
+ 'ETH': D('0.3')
+ },
+ 'rates': {
+ 'BTC': None,
+ 'ETH': D('0.1')
+ },
+ 'total': D('10.3')
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_dispatch(self, add_log, print_log):
+ amount = portfolio.Amount("BTC", "10.3")
+ amounts = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "ETH": portfolio.Amount("BTC", D("0.3"))
+ }
+ portfolio.ReportStore.log_dispatch(amount, amounts, "medium", "repartition")
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'dispatch',
+ 'liquidity': 'medium',
+ 'repartition_ratio': 'repartition',
+ 'total_amount': {
+ 'currency': 'BTC',
+ 'value': D('10.3')
+ },
+ 'repartition': {
+ 'BTC': D('10'),
+ 'ETH': D('0.3')
+ }
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_trades(self, add_log, print_log):
+ trade_mock1 = mock.Mock()
+ trade_mock2 = mock.Mock()
+ trade_mock1.as_json.return_value = { "trade": "1" }
+ trade_mock2.as_json.return_value = { "trade": "2" }
+
+ matching_and_trades = [
+ (True, trade_mock1),
+ (False, trade_mock2),
+ ]
+ portfolio.ReportStore.log_trades(matching_and_trades, "only", "debug")
+
+ print_log.assert_not_called()
+ add_log.assert_called_with({
+ 'type': 'trades',
+ 'only': 'only',
+ 'debug': 'debug',
+ 'trades': [
+ {'trade': '1', 'skipped': False},
+ {'trade': '2', 'skipped': True}
+ ]
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ @mock.patch.object(portfolio.TradeStore, "print_all_with_order")
+ def test_log_orders(self, print_all_with_order, add_log, print_log):
+ order_mock1 = mock.Mock()
+ order_mock2 = mock.Mock()
+
+ order_mock1.as_json.return_value = "order1"
+ order_mock2.as_json.return_value = "order2"
+
+ orders = [order_mock1, order_mock2]
+
+ portfolio.ReportStore.log_orders(orders, tick="tick",
+ only="only", compute_value="compute_value")
+
+ print_log.assert_called_once_with("[Orders]")
+ print_all_with_order.assert_called_once_with(ind="\t")
+
+ add_log.assert_called_with({
+ 'type': 'orders',
+ 'only': 'only',
+ 'compute_value': 'compute_value',
+ 'tick': 'tick',
+ 'orders': ['order1', 'order2']
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_order(self, add_log, print_log):
+ order_mock = mock.Mock()
+ order_mock.as_json.return_value = "order"
+ new_order_mock = mock.Mock()
+ new_order_mock.as_json.return_value = "new_order"
+ order_mock.__repr__ = mock.Mock()
+ order_mock.__repr__.return_value = "Order Mock"
+ new_order_mock.__repr__ = mock.Mock()
+ new_order_mock.__repr__.return_value = "New order Mock"
+
+ with self.subTest(finished=True):
+ portfolio.ReportStore.log_order(order_mock, 1, finished=True)
+ print_log.assert_called_once_with("[Order] Finished Order Mock")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 1,
+ 'update': None,
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': None
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+
+ with self.subTest(update="waiting"):
+ portfolio.ReportStore.log_order(order_mock, 1, update="waiting")
+ print_log.assert_called_once_with("[Order] Order Mock, tick 1, waiting")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 1,
+ 'update': 'waiting',
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': None
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+ with self.subTest(update="adjusting"):
+ portfolio.ReportStore.log_order(order_mock, 3,
+ update="adjusting", new_order=new_order_mock,
+ compute_value="default")
+ print_log.assert_called_once_with("[Order] Order Mock, tick 3, cancelling and adjusting to New order Mock")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 3,
+ 'update': 'adjusting',
+ 'order': 'order',
+ 'compute_value': "default",
+ 'new_order': 'new_order'
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+ with self.subTest(update="market_fallback"):
+ portfolio.ReportStore.log_order(order_mock, 7,
+ update="market_fallback", new_order=new_order_mock)
+ print_log.assert_called_once_with("[Order] Order Mock, tick 7, fallbacking to market value")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 7,
+ 'update': 'market_fallback',
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': 'new_order'
+ })
+
+ add_log.reset_mock()
+ print_log.reset_mock()
+ with self.subTest(update="market_adjusting"):
+ portfolio.ReportStore.log_order(order_mock, 17,
+ update="market_adjust", new_order=new_order_mock)
+ print_log.assert_called_once_with("[Order] Order Mock, tick 17, market value, cancelling and adjusting to New order Mock")
+ add_log.assert_called_once_with({
+ 'type': 'order',
+ 'tick': 17,
+ 'update': 'market_adjust',
+ 'order': 'order',
+ 'compute_value': None,
+ 'new_order': 'new_order'
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_move_balances(self, add_log, print_log):
+ needed = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "USDT": 1
+ }
+ moving = {
+ "BTC": portfolio.Amount("BTC", 3),
+ "USDT": -2
+ }
+ portfolio.ReportStore.log_move_balances(needed, moving, True)
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'move_balances',
+ 'debug': True,
+ 'needed': {
+ 'BTC': D('10'),
+ 'USDT': 1
+ },
+ 'moving': {
+ 'BTC': D('3'),
+ 'USDT': -2
+ }
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_http_request(self, add_log, print_log):
+ response = mock.Mock()
+ response.status_code = 200
+ response.text = "Hey"
+
+ portfolio.ReportStore.log_http_request("method", "url", "body",
+ "headers", response)
+ print_log.assert_not_called()
+ add_log.assert_called_once_with({
+ 'type': 'http_request',
+ 'method': 'method',
+ 'url': 'url',
+ 'body': 'body',
+ 'headers': 'headers',
+ 'status': 200,
+ 'response': 'Hey'
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_error(self, add_log, print_log):
+ with self.subTest(message=None, exception=None):
+ portfolio.ReportStore.log_error("action")
+ print_log.assert_called_once_with("[Error] action")
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': None,
+ 'exception_message': None,
+ 'message': None
+ })
+
+ print_log.reset_mock()
+ add_log.reset_mock()
+ with self.subTest(message="Hey", exception=None):
+ portfolio.ReportStore.log_error("action", message="Hey")
+ print_log.assert_has_calls([
+ mock.call("[Error] action"),
+ mock.call("\tHey")
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': None,
+ 'exception_message': None,
+ 'message': "Hey"
+ })
+
+ print_log.reset_mock()
+ add_log.reset_mock()
+ with self.subTest(message=None, exception=Exception("bouh")):
+ portfolio.ReportStore.log_error("action", exception=Exception("bouh"))
+ print_log.assert_has_calls([
+ mock.call("[Error] action"),
+ mock.call("\tException: bouh")
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': "Exception",
+ 'exception_message': "bouh",
+ 'message': None
+ })
+
+ print_log.reset_mock()
+ add_log.reset_mock()
+ with self.subTest(message="Hey", exception=Exception("bouh")):
+ portfolio.ReportStore.log_error("action", message="Hey", exception=Exception("bouh"))
+ print_log.assert_has_calls([
+ mock.call("[Error] action"),
+ mock.call("\tException: bouh"),
+ mock.call("\tHey")
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'error',
+ 'action': 'action',
+ 'exception_class': "Exception",
+ 'exception_message': "bouh",
+ 'message': "Hey"
+ })
+
+ @mock.patch.object(portfolio.ReportStore, "print_log")
+ @mock.patch.object(portfolio.ReportStore, "add_log")
+ def test_log_debug_action(self, add_log, print_log):
+ portfolio.ReportStore.log_debug_action("Hey")
+
+ print_log.assert_called_once_with("[Debug] Hey")
+ add_log.assert_called_once_with({
+ 'type': 'debug_action',
+ 'action': 'Hey'
+ })
+