balance_store = market.BalanceStore(self.m)
- balance_store.fetch_balances()
- self.assertNotIn("ETC", balance_store.currencies())
- self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies()))
+ with self.subTest(log_tickers=False):
+ balance_store.fetch_balances()
+ self.assertNotIn("ETC", balance_store.currencies())
+ self.assertListEqual(["USDT", "XVG", "XMR"], list(balance_store.currencies()))
+
+ balance_store.all["ETC"] = portfolio.Balance("ETC", {
+ "exchange_total": "1", "exchange_free": "0",
+ "exchange_used": "1" })
+ balance_store.fetch_balances(tag="foo")
+ self.assertEqual(0, balance_store.all["ETC"].total)
+ self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
+ self.m.report.log_balances.assert_called_with(tag="foo", checkpoint=None)
+
+ with self.subTest(log_tickers=True),\
+ mock.patch.object(balance_store, "in_currency") as in_currency:
+ in_currency.return_value = "tickers"
+ balance_store.fetch_balances(log_tickers=True, ticker_currency="FOO",
+ ticker_compute_value="compute", ticker_type="type")
+ self.m.report.log_balances.assert_called_with(compute_value='compute',
+ tag=None, checkpoint=None, ticker_currency='FOO', tickers='tickers',
+ type='type')
+
+ balance_store = market.BalanceStore(self.m)
+
+ with self.subTest(add_portfolio=True),\
+ mock.patch.object(market.Portfolio, "repartition") as repartition:
+ repartition.return_value = {
+ "DOGE": D("0.5"),
+ "USDT": D("0.5"),
+ }
+ balance_store.fetch_balances(add_portfolio=True)
+ self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies()))
- balance_store.all["ETC"] = portfolio.Balance("ETC", {
- "exchange_total": "1", "exchange_free": "0",
- "exchange_used": "1" })
- balance_store.fetch_balances(tag="foo")
- self.assertEqual(0, balance_store.all["ETC"].total)
- self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies()))
- self.m.report.log_balances.assert_called_with(tag="foo")
@mock.patch.object(market.Portfolio, "repartition")
def test_dispatch_assets(self, repartition):
self.assertEqual(D("2.6"), amounts["BTC"].value)
self.assertEqual(D("7.5"), amounts["XEM"].value)
self.assertEqual(D("-1.0"), amounts["DASH"].value)
- self.m.report.log_balances.assert_called_with(tag=None)
+ self.m.report.log_balances.assert_called_with(tag=None, checkpoint=None)
self.m.report.log_dispatch.assert_called_once_with(portfolio.Amount("BTC",
"11.1"), amounts, "medium", repartition_hash)
self.m.balances.as_json.return_value = "json"
self.m.balances.all = { "FOO": "bar", "BAR": "baz" }
- report_store.log_balances(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'
- })
- add_redis_status.assert_called_once_with({
- 'type': 'balance',
- 'balances': 'json',
- 'tag': 'tag'
- })
+ with self.subTest(tickers=None):
+ report_store.log_balances(tag="tag")
+ print_log.assert_has_calls([
+ mock.call("[Balance]"),
+ mock.call("\tbar"),
+ mock.call("\tbaz"),
+ ])
+ add_log.assert_called_once_with({
+ 'type': 'balance',
+ 'checkpoint': None,
+ 'balances': 'json',
+ 'tag': 'tag'
+ })
+ add_redis_status.assert_called_once_with({
+ 'type': 'balance',
+ 'balances': 'json',
+ 'checkpoint': None,
+ 'tag': 'tag'
+ })
+ add_log.reset_mock()
+ add_redis_status.reset_mock()
+ with self.subTest(tickers="present"):
+ amounts = {
+ "BTC": portfolio.Amount("BTC", 10),
+ "ETH": portfolio.Amount("BTC", D("0.3"))
+ }
+ amounts["ETH"].rate = D("0.1")
+
+ report_store.log_balances(tag="tag", tickers=amounts,
+ ticker_currency="BTC", compute_value="default",
+ type="total")
+ add_log.assert_called_once_with({
+ 'type': 'balance',
+ 'checkpoint': None,
+ 'balances': 'json',
+ 'tag': 'tag',
+ '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')
+ },
+ })
+ add_redis_status.assert_called_once_with({
+ 'type': 'balance',
+ 'checkpoint': None,
+ 'balances': 'json',
+ 'tag': 'tag',
+ '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(market.ReportStore, "print_log")
@mock.patch.object(market.ReportStore, "add_log")
- @mock.patch.object(market.ReportStore, "add_redis_status")
- def test_log_tickers(self, add_redis_status, add_log, print_log):
+ def test_log_tickers(self, add_log, print_log):
report_store = market.ReportStore(self.m)
amounts = {
"BTC": portfolio.Amount("BTC", 10),
},
'total': D('10.3')
})
- add_redis_status.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')
- })
add_log.reset_mock()
compute_value = lambda x: x["bid"]
self.wm.get(market.Portfolio.URL, text=self.json_response)
@mock.patch.object(market.Portfolio, "parse_cryptoportfolio")
- def test_get_cryptoportfolio(self, parse_cryptoportfolio):
+ @mock.patch.object(market.Portfolio, "store_cryptoportfolio")
+ def test_get_cryptoportfolio(self, store_cryptoportfolio, parse_cryptoportfolio):
with self.subTest(parallel=False):
self.wm.get(market.Portfolio.URL, [
{"text":'{ "foo": "bar" }', "status_code": 200},
market.Portfolio.report.log_error.assert_not_called()
market.Portfolio.report.log_http_request.assert_called_once()
parse_cryptoportfolio.assert_called_once_with()
+ store_cryptoportfolio.assert_called_once_with()
market.Portfolio.report.log_http_request.reset_mock()
parse_cryptoportfolio.reset_mock()
+ store_cryptoportfolio.reset_mock()
market.Portfolio.data = store.LockedVar(None)
market.Portfolio.get_cryptoportfolio()
self.assertIsNone(market.Portfolio.data.get())
self.assertEqual(2, self.wm.call_count)
parse_cryptoportfolio.assert_not_called()
+ store_cryptoportfolio.assert_not_called()
market.Portfolio.report.log_error.assert_not_called()
market.Portfolio.report.log_http_request.assert_called_once()
market.Portfolio.report.log_http_request.reset_mock()
parse_cryptoportfolio.reset_mock()
+ store_cryptoportfolio.reset_mock()
market.Portfolio.data = store.LockedVar("Foo")
market.Portfolio.get_cryptoportfolio()
self.assertEqual(2, self.wm.call_count)
parse_cryptoportfolio.assert_not_called()
+ store_cryptoportfolio.assert_not_called()
market.Portfolio.get_cryptoportfolio(refetch=True)
self.assertEqual("Foo", market.Portfolio.data.get())
market.Portfolio.get_cryptoportfolio()
self.assertIn("foo", market.Portfolio.data.get())
parse_cryptoportfolio.reset_mock()
+ store_cryptoportfolio.reset_mock()
with self.subTest(worker=False):
market.Portfolio.data = store.LockedVar(None)
market.Portfolio.worker = mock.Mock()
market.Portfolio.get_cryptoportfolio()
notify.assert_called_once_with()
parse_cryptoportfolio.assert_not_called()
+ store_cryptoportfolio.assert_not_called()
def test_parse_cryptoportfolio(self):
with self.subTest(description="Normal case"):
self.assertEqual({}, market.Portfolio.liquidities.get("high"))
self.assertEqual(datetime.datetime(1,1,1), market.Portfolio.last_date.get())
-
- @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
- def test_repartition(self, get_cryptoportfolio):
- market.Portfolio.liquidities = store.LockedVar({
+ @mock.patch.object(store.dbs, "redis_connected")
+ @mock.patch.object(store.dbs, "redis")
+ def test_store_cryptoportfolio(self, redis, redis_connected):
+ store.Portfolio.liquidities = store.LockedVar({
"medium": {
- "2018-03-01": "medium_2018-03-01",
- "2018-03-08": "medium_2018-03-08",
+ datetime.datetime(2018,3,1): "medium_2018-03-01",
+ datetime.datetime(2018,3,8): "medium_2018-03-08",
},
"high": {
- "2018-03-01": "high_2018-03-01",
- "2018-03-08": "high_2018-03-08",
+ datetime.datetime(2018,3,1): "high_2018-03-01",
+ datetime.datetime(2018,3,8): "high_2018-03-08",
}
})
- market.Portfolio.last_date = store.LockedVar("2018-03-08")
+ store.Portfolio.last_date = store.LockedVar(datetime.datetime(2018,3,8))
+
+ with self.subTest(redis_connected=False):
+ redis_connected.return_value = False
+ store.Portfolio.store_cryptoportfolio()
+ redis.set.assert_not_called()
+
+ with self.subTest(redis_connected=True):
+ redis_connected.return_value = True
+ store.Portfolio.store_cryptoportfolio()
+ redis.set.assert_has_calls([
+ mock.call("/cryptoportfolio/repartition/latest", '{"medium": "medium_2018-03-08", "high": "high_2018-03-08"}'),
+ mock.call("/cryptoportfolio/repartition/date", "2018-03-08"),
+ ])
+
+ @mock.patch.object(store.dbs, "redis_connected")
+ @mock.patch.object(store.dbs, "redis")
+ def test_retrieve_cryptoportfolio(self, redis, redis_connected):
+ with self.subTest(redis_connected=False):
+ redis_connected.return_value = False
+ store.Portfolio.retrieve_cryptoportfolio()
+ redis.get.assert_not_called()
+ self.assertIsNone(store.Portfolio.data.get())
+
+ with self.subTest(redis_connected=True, value=None):
+ redis_connected.return_value = True
+ redis.get.return_value = None
+ store.Portfolio.retrieve_cryptoportfolio()
+ self.assertEqual(2, redis.get.call_count)
+
+ redis.reset_mock()
+ with self.subTest(redis_connected=True, value="present"):
+ redis_connected.return_value = True
+ redis.get.side_effect = [
+ b'{ "medium": "medium_repartition", "high": "high_repartition" }',
+ b"2018-03-08"
+ ]
+ store.Portfolio.retrieve_cryptoportfolio()
+ self.assertEqual(2, redis.get.call_count)
+ self.assertEqual(datetime.datetime(2018,3,8), store.Portfolio.last_date.get())
+ self.assertEqual("", store.Portfolio.data.get())
+ expected_liquidities = {
+ 'high': { datetime.datetime(2018, 3, 8): 'high_repartition' },
+ 'medium': { datetime.datetime(2018, 3, 8): 'medium_repartition' },
+ }
+ self.assertEqual(expected_liquidities, store.Portfolio.liquidities.get())
+
+ @mock.patch.object(market.Portfolio, "get_cryptoportfolio")
+ @mock.patch.object(market.Portfolio, "retrieve_cryptoportfolio")
+ def test_repartition(self, retrieve_cryptoportfolio, get_cryptoportfolio):
+ with self.subTest(from_cache=False):
+ market.Portfolio.liquidities = store.LockedVar({
+ "medium": {
+ "2018-03-01": "medium_2018-03-01",
+ "2018-03-08": "medium_2018-03-08",
+ },
+ "high": {
+ "2018-03-01": "high_2018-03-01",
+ "2018-03-08": "high_2018-03-08",
+ }
+ })
+ market.Portfolio.last_date = store.LockedVar("2018-03-08")
+
+ self.assertEqual("medium_2018-03-08", market.Portfolio.repartition())
+ get_cryptoportfolio.assert_called_once_with()
+ retrieve_cryptoportfolio.assert_not_called()
+ self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium"))
+ self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high"))
+
+ retrieve_cryptoportfolio.reset_mock()
+ get_cryptoportfolio.reset_mock()
- self.assertEqual("medium_2018-03-08", market.Portfolio.repartition())
- get_cryptoportfolio.assert_called_once_with()
- self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium"))
- self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high"))
+ with self.subTest(from_cache=True):
+ self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(from_cache=True))
+ get_cryptoportfolio.assert_called_once_with()
+ retrieve_cryptoportfolio.assert_called_once_with()
@mock.patch.object(market.time, "sleep")
@mock.patch.object(market.Portfolio, "get_cryptoportfolio")