diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-03 00:22:33 +0200 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-05-03 00:22:33 +0200 |
commit | 9b69786341d14fd4327b117a12437fd1650cd965 (patch) | |
tree | a534f8d64d247d50929f78c92c74acbe4131a1d2 /tests | |
parent | 40d0fa279e0745b33676f21cdc8b496ebd301cf8 (diff) | |
download | Trader-9b69786341d14fd4327b117a12437fd1650cd965.tar.gz Trader-9b69786341d14fd4327b117a12437fd1650cd965.tar.zst Trader-9b69786341d14fd4327b117a12437fd1650cd965.zip |
Include current portfolio currencies when printing balances
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_market.py | 9 | ||||
-rw-r--r-- | tests/test_store.py | 87 |
2 files changed, 80 insertions, 16 deletions
diff --git a/tests/test_market.py b/tests/test_market.py index ab3cd5e..46fad53 100644 --- a/tests/test_market.py +++ b/tests/test_market.py | |||
@@ -1032,6 +1032,15 @@ class ProcessorTest(WebMockTestCase): | |||
1032 | processor.process_step("foo", step, {"foo":"bar"}) | 1032 | processor.process_step("foo", step, {"foo":"bar"}) |
1033 | self.m.balances.fetch_balances.assert_not_called() | 1033 | self.m.balances.fetch_balances.assert_not_called() |
1034 | 1034 | ||
1035 | self.m.reset_mock() | ||
1036 | with mock.patch.object(processor, "run_action") as run_action: | ||
1037 | step = processor.scenarios["print_balances"][0] | ||
1038 | |||
1039 | processor.process_step("foo", step, {"foo":"bar"}) | ||
1040 | self.m.balances.fetch_balances.assert_called_once_with( | ||
1041 | add_portfolio=True, log_tickers=True, | ||
1042 | tag='process_foo__1_print_balances_begin') | ||
1043 | |||
1035 | def test_parse_args(self): | 1044 | def test_parse_args(self): |
1036 | processor = market.Processor(self.m) | 1045 | processor = market.Processor(self.m) |
1037 | 1046 | ||
diff --git a/tests/test_store.py b/tests/test_store.py index d0f7755..ee7e063 100644 --- a/tests/test_store.py +++ b/tests/test_store.py | |||
@@ -391,6 +391,18 @@ class BalanceStoreTest(WebMockTestCase): | |||
391 | tag=None, ticker_currency='FOO', tickers='tickers', | 391 | tag=None, ticker_currency='FOO', tickers='tickers', |
392 | type='type') | 392 | type='type') |
393 | 393 | ||
394 | balance_store = market.BalanceStore(self.m) | ||
395 | |||
396 | with self.subTest(add_portfolio=True),\ | ||
397 | mock.patch.object(market.Portfolio, "repartition") as repartition: | ||
398 | repartition.return_value = { | ||
399 | "DOGE": D("0.5"), | ||
400 | "USDT": D("0.5"), | ||
401 | } | ||
402 | balance_store.fetch_balances(add_portfolio=True) | ||
403 | self.assertListEqual(["USDT", "XVG", "XMR", "DOGE"], list(balance_store.currencies())) | ||
404 | |||
405 | |||
394 | @mock.patch.object(market.Portfolio, "repartition") | 406 | @mock.patch.object(market.Portfolio, "repartition") |
395 | def test_dispatch_assets(self, repartition): | 407 | def test_dispatch_assets(self, repartition): |
396 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance | 408 | self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance |
@@ -1259,24 +1271,67 @@ class PortfolioTest(WebMockTestCase): | |||
1259 | mock.call("/cryptoportfolio/repartition/date", "2018-03-08"), | 1271 | mock.call("/cryptoportfolio/repartition/date", "2018-03-08"), |
1260 | ]) | 1272 | ]) |
1261 | 1273 | ||
1262 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | 1274 | @mock.patch.object(store.dbs, "redis_connected") |
1263 | def test_repartition(self, get_cryptoportfolio): | 1275 | @mock.patch.object(store.dbs, "redis") |
1264 | market.Portfolio.liquidities = store.LockedVar({ | 1276 | def test_retrieve_cryptoportfolio(self, redis, redis_connected): |
1265 | "medium": { | 1277 | with self.subTest(redis_connected=False): |
1266 | "2018-03-01": "medium_2018-03-01", | 1278 | redis_connected.return_value = False |
1267 | "2018-03-08": "medium_2018-03-08", | 1279 | store.Portfolio.retrieve_cryptoportfolio() |
1268 | }, | 1280 | redis.get.assert_not_called() |
1269 | "high": { | 1281 | self.assertIsNone(store.Portfolio.data.get()) |
1270 | "2018-03-01": "high_2018-03-01", | 1282 | |
1271 | "2018-03-08": "high_2018-03-08", | 1283 | with self.subTest(redis_connected=True, value=None): |
1284 | redis_connected.return_value = True | ||
1285 | redis.get.return_value = None | ||
1286 | store.Portfolio.retrieve_cryptoportfolio() | ||
1287 | self.assertEqual(2, redis.get.call_count) | ||
1288 | |||
1289 | redis.reset_mock() | ||
1290 | with self.subTest(redis_connected=True, value="present"): | ||
1291 | redis_connected.return_value = True | ||
1292 | redis.get.side_effect = [ | ||
1293 | b'{ "medium": "medium_repartition", "high": "high_repartition" }', | ||
1294 | b"2018-03-08" | ||
1295 | ] | ||
1296 | store.Portfolio.retrieve_cryptoportfolio() | ||
1297 | self.assertEqual(2, redis.get.call_count) | ||
1298 | self.assertEqual(datetime.datetime(2018,3,8), store.Portfolio.last_date.get()) | ||
1299 | self.assertEqual("", store.Portfolio.data.get()) | ||
1300 | expected_liquidities = { | ||
1301 | 'high': { datetime.datetime(2018, 3, 8): 'high_repartition' }, | ||
1302 | 'medium': { datetime.datetime(2018, 3, 8): 'medium_repartition' }, | ||
1272 | } | 1303 | } |
1273 | }) | 1304 | self.assertEqual(expected_liquidities, store.Portfolio.liquidities.get()) |
1274 | market.Portfolio.last_date = store.LockedVar("2018-03-08") | 1305 | |
1306 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | ||
1307 | @mock.patch.object(market.Portfolio, "retrieve_cryptoportfolio") | ||
1308 | def test_repartition(self, retrieve_cryptoportfolio, get_cryptoportfolio): | ||
1309 | with self.subTest(from_cache=False): | ||
1310 | market.Portfolio.liquidities = store.LockedVar({ | ||
1311 | "medium": { | ||
1312 | "2018-03-01": "medium_2018-03-01", | ||
1313 | "2018-03-08": "medium_2018-03-08", | ||
1314 | }, | ||
1315 | "high": { | ||
1316 | "2018-03-01": "high_2018-03-01", | ||
1317 | "2018-03-08": "high_2018-03-08", | ||
1318 | } | ||
1319 | }) | ||
1320 | market.Portfolio.last_date = store.LockedVar("2018-03-08") | ||
1321 | |||
1322 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition()) | ||
1323 | get_cryptoportfolio.assert_called_once_with() | ||
1324 | retrieve_cryptoportfolio.assert_not_called() | ||
1325 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) | ||
1326 | self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) | ||
1327 | |||
1328 | retrieve_cryptoportfolio.reset_mock() | ||
1329 | get_cryptoportfolio.reset_mock() | ||
1275 | 1330 | ||
1276 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition()) | 1331 | with self.subTest(from_cache=True): |
1277 | get_cryptoportfolio.assert_called_once_with() | 1332 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(from_cache=True)) |
1278 | self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) | 1333 | get_cryptoportfolio.assert_called_once_with() |
1279 | self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) | 1334 | retrieve_cryptoportfolio.assert_called_once_with() |
1280 | 1335 | ||
1281 | @mock.patch.object(market.time, "sleep") | 1336 | @mock.patch.object(market.time, "sleep") |
1282 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") | 1337 | @mock.patch.object(market.Portfolio, "get_cryptoportfolio") |