X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=test.py;h=f61e739d9a3e664cd30bfca1d185d13846c46cee;hb=dc1ca9a306f09886c6c57f8d426c59a9d084b2b3;hp=bbe0697bbfd47273106bddc29b8536320d4f34e7;hpb=a18ce2f16973155c81f983643aba675f62dea7af;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/test.py b/test.py index bbe0697..f61e739 100644 --- a/test.py +++ b/test.py @@ -7,7 +7,8 @@ from unittest import mock import requests import requests_mock from io import StringIO -import portfolio, market, main +import threading +import portfolio, market, main, store limits = ["acceptance", "unit"] for test_type in limits: @@ -32,7 +33,15 @@ class WebMockTestCase(unittest.TestCase): self.m.debug = False self.patchers = [ - mock.patch.multiple(portfolio.Portfolio, last_date=None, data=None, liquidities={}), + mock.patch.multiple(market.Portfolio, + data=store.LockedVar(None), + liquidities=store.LockedVar({}), + last_date=store.LockedVar(None), + report=mock.Mock(), + worker=None, + worker_notify=None, + worker_started=False, + callback=None), mock.patch.multiple(portfolio.Computation, computations=portfolio.Computation.computations), ] @@ -438,173 +447,320 @@ class poloniexETest(unittest.TestCase): create_order.assert_called_once_with("symbol", "type", "side", "amount", price="price", params="params") @unittest.skipUnless("unit" in limits, "Unit skipped") -class PortfolioTest(WebMockTestCase): - def fill_data(self): - if self.json_response is not None: - portfolio.Portfolio.data = self.json_response +class NoopLockTest(unittest.TestCase): + def test_with(self): + noop_lock = store.NoopLock() + with noop_lock: + self.assertTrue(True) + +@unittest.skipUnless("unit" in limits, "Unit skipped") +class LockedVar(unittest.TestCase): + + def test_values(self): + locked_var = store.LockedVar("Foo") + self.assertIsInstance(locked_var.lock, store.NoopLock) + self.assertEqual("Foo", locked_var.val) + + def test_get(self): + with self.subTest(desc="Normal case"): + locked_var = store.LockedVar("Foo") + self.assertEqual("Foo", locked_var.get()) + with self.subTest(desc="Dict"): + locked_var = store.LockedVar({"foo": "bar"}) + self.assertEqual({"foo": "bar"}, locked_var.get()) + self.assertEqual("bar", locked_var.get("foo")) + self.assertIsNone(locked_var.get("other")) + + def test_set(self): + locked_var = store.LockedVar("Foo") + locked_var.set("Bar") + self.assertEqual("Bar", locked_var.get()) + + def test__getattr(self): + dummy = type('Dummy', (object,), {})() + dummy.attribute = "Hey" + + locked_var = store.LockedVar(dummy) + self.assertEqual("Hey", locked_var.attribute) + with self.assertRaises(AttributeError): + locked_var.other + + def test_start_lock(self): + locked_var = store.LockedVar("Foo") + locked_var.start_lock() + self.assertEqual("lock", locked_var.lock.__class__.__name__) + + thread1 = threading.Thread(target=locked_var.set, args=["Bar1"]) + thread2 = threading.Thread(target=locked_var.set, args=["Bar2"]) + thread3 = threading.Thread(target=locked_var.set, args=["Bar3"]) + + with locked_var.lock: + thread1.start() + thread2.start() + thread3.start() + + self.assertEqual("Foo", locked_var.val) + thread1.join() + thread2.join() + thread3.join() + self.assertEqual("Bar", locked_var.get()[0:3]) + + def test_wait_for_notification(self): + with self.assertRaises(RuntimeError): + store.Portfolio.wait_for_notification() + + with mock.patch.object(store.Portfolio, "get_cryptoportfolio") as get,\ + mock.patch.object(store.Portfolio, "report") as report,\ + mock.patch.object(store.time, "sleep") as sleep: + store.Portfolio.start_worker(poll=3) + + store.Portfolio.worker_notify.set() + + store.Portfolio.callback.wait() + + report.print_log.assert_called_once_with("Fetching cryptoportfolio") + get.assert_called_once_with(refetch=True) + sleep.assert_called_once_with(3) + self.assertFalse(store.Portfolio.worker_notify.is_set()) + self.assertTrue(store.Portfolio.worker.is_alive()) + + store.Portfolio.callback.clear() + store.Portfolio.worker_started = False + store.Portfolio.worker_notify.set() + store.Portfolio.callback.wait() + + self.assertFalse(store.Portfolio.worker.is_alive()) + + def test_notify_and_wait(self): + with mock.patch.object(store.Portfolio, "callback") as callback,\ + mock.patch.object(store.Portfolio, "worker_notify") as worker_notify: + store.Portfolio.notify_and_wait() + callback.clear.assert_called_once_with() + worker_notify.set.assert_called_once_with() + callback.wait.assert_called_once_with() +@unittest.skipUnless("unit" in limits, "Unit skipped") +class PortfolioTest(WebMockTestCase): def setUp(self): super(PortfolioTest, self).setUp() with open("test_samples/test_portfolio.json") as example: self.json_response = example.read() - self.wm.get(portfolio.Portfolio.URL, text=self.json_response) + self.wm.get(market.Portfolio.URL, text=self.json_response) - def test_get_cryptoportfolio(self): - self.wm.get(portfolio.Portfolio.URL, [ - {"text":'{ "foo": "bar" }', "status_code": 200}, - {"text": "System Error", "status_code": 500}, - {"exc": requests.exceptions.ConnectTimeout}, - ]) - portfolio.Portfolio.get_cryptoportfolio(self.m) - self.assertIn("foo", portfolio.Portfolio.data) - self.assertEqual("bar", portfolio.Portfolio.data["foo"]) - self.assertTrue(self.wm.called) - self.assertEqual(1, self.wm.call_count) - self.m.report.log_error.assert_not_called() - self.m.report.log_http_request.assert_called_once() - self.m.report.log_http_request.reset_mock() - - portfolio.Portfolio.get_cryptoportfolio(self.m) - self.assertIsNone(portfolio.Portfolio.data) - self.assertEqual(2, self.wm.call_count) - self.m.report.log_error.assert_not_called() - self.m.report.log_http_request.assert_called_once() - self.m.report.log_http_request.reset_mock() - - - portfolio.Portfolio.data = "Foo" - portfolio.Portfolio.get_cryptoportfolio(self.m) - self.assertEqual("Foo", portfolio.Portfolio.data) - self.assertEqual(3, self.wm.call_count) - self.m.report.log_error.assert_called_once_with("get_cryptoportfolio", - exception=mock.ANY) - self.m.report.log_http_request.assert_not_called() + @mock.patch.object(market.Portfolio, "parse_cryptoportfolio") + def test_get_cryptoportfolio(self, parse_cryptoportfolio): + with self.subTest(parallel=False): + self.wm.get(market.Portfolio.URL, [ + {"text":'{ "foo": "bar" }', "status_code": 200}, + {"text": "System Error", "status_code": 500}, + {"exc": requests.exceptions.ConnectTimeout}, + ]) + market.Portfolio.get_cryptoportfolio() + self.assertIn("foo", market.Portfolio.data.get()) + self.assertEqual("bar", market.Portfolio.data.get()["foo"]) + self.assertTrue(self.wm.called) + self.assertEqual(1, self.wm.call_count) + market.Portfolio.report.log_error.assert_not_called() + market.Portfolio.report.log_http_request.assert_called_once() + parse_cryptoportfolio.assert_called_once_with() + market.Portfolio.report.log_http_request.reset_mock() + parse_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() + 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() + + market.Portfolio.data = store.LockedVar("Foo") + market.Portfolio.get_cryptoportfolio() + self.assertEqual(2, self.wm.call_count) + parse_cryptoportfolio.assert_not_called() + + market.Portfolio.get_cryptoportfolio(refetch=True) + self.assertEqual("Foo", market.Portfolio.data.get()) + self.assertEqual(3, self.wm.call_count) + market.Portfolio.report.log_error.assert_called_once_with("get_cryptoportfolio", + exception=mock.ANY) + market.Portfolio.report.log_http_request.assert_not_called() + with self.subTest(parallel=True): + with mock.patch.object(market.Portfolio, "is_worker_thread") as is_worker,\ + mock.patch.object(market.Portfolio, "notify_and_wait") as notify: + with self.subTest(worker=True): + market.Portfolio.data = store.LockedVar(None) + market.Portfolio.worker = mock.Mock() + is_worker.return_value = True + self.wm.get(market.Portfolio.URL, [ + {"text":'{ "foo": "bar" }', "status_code": 200}, + ]) + market.Portfolio.get_cryptoportfolio() + self.assertIn("foo", market.Portfolio.data.get()) + parse_cryptoportfolio.reset_mock() + with self.subTest(worker=False): + market.Portfolio.data = store.LockedVar(None) + market.Portfolio.worker = mock.Mock() + is_worker.return_value = False + market.Portfolio.get_cryptoportfolio() + notify.assert_called_once_with() + parse_cryptoportfolio.assert_not_called() def test_parse_cryptoportfolio(self): - portfolio.Portfolio.parse_cryptoportfolio(self.m) - - self.assertListEqual( - ["medium", "high"], - list(portfolio.Portfolio.liquidities.keys())) - - liquidities = portfolio.Portfolio.liquidities - self.assertEqual(10, len(liquidities["medium"].keys())) - self.assertEqual(10, len(liquidities["high"].keys())) - - expected = { - 'BTC': (D("0.2857"), "long"), - 'DGB': (D("0.1015"), "long"), - 'DOGE': (D("0.1805"), "long"), - 'SC': (D("0.0623"), "long"), - 'ZEC': (D("0.3701"), "long"), - } - date = portfolio.datetime(2018, 1, 8) - self.assertDictEqual(expected, liquidities["high"][date]) - - expected = { - 'BTC': (D("1.1102e-16"), "long"), - 'ETC': (D("0.1"), "long"), - 'FCT': (D("0.1"), "long"), - 'GAS': (D("0.1"), "long"), - 'NAV': (D("0.1"), "long"), - 'OMG': (D("0.1"), "long"), - 'OMNI': (D("0.1"), "long"), - 'PPC': (D("0.1"), "long"), - 'RIC': (D("0.1"), "long"), - 'VIA': (D("0.1"), "long"), - 'XCP': (D("0.1"), "long"), - } - self.assertDictEqual(expected, liquidities["medium"][date]) - self.assertEqual(portfolio.datetime(2018, 1, 15), portfolio.Portfolio.last_date) - - self.m.report.log_http_request.assert_called_once_with("GET", - portfolio.Portfolio.URL, None, mock.ANY, mock.ANY) - self.m.report.log_http_request.reset_mock() - - # It doesn't refetch the data when available - portfolio.Portfolio.parse_cryptoportfolio(self.m) - self.m.report.log_http_request.assert_not_called() - - self.assertEqual(1, self.wm.call_count) - - portfolio.Portfolio.parse_cryptoportfolio(self.m, refetch=True) - self.assertEqual(2, self.wm.call_count) - self.m.report.log_http_request.assert_called_once() - - def test_repartition(self): - expected_medium = { - 'BTC': (D("1.1102e-16"), "long"), - 'USDT': (D("0.1"), "long"), - 'ETC': (D("0.1"), "long"), - 'FCT': (D("0.1"), "long"), - 'OMG': (D("0.1"), "long"), - 'STEEM': (D("0.1"), "long"), - 'STRAT': (D("0.1"), "long"), - 'XEM': (D("0.1"), "long"), - 'XMR': (D("0.1"), "long"), - 'XVC': (D("0.1"), "long"), - 'ZRX': (D("0.1"), "long"), - } - expected_high = { - 'USDT': (D("0.1226"), "long"), - 'BTC': (D("0.1429"), "long"), - 'ETC': (D("0.1127"), "long"), - 'ETH': (D("0.1569"), "long"), - 'FCT': (D("0.3341"), "long"), - 'GAS': (D("0.1308"), "long"), - } + with self.subTest(description="Normal case"): + market.Portfolio.data = store.LockedVar(store.json.loads( + self.json_response, parse_int=D, parse_float=D)) + market.Portfolio.parse_cryptoportfolio() - self.assertEqual(expected_medium, portfolio.Portfolio.repartition(self.m)) - self.assertEqual(expected_medium, portfolio.Portfolio.repartition(self.m, liquidity="medium")) - self.assertEqual(expected_high, portfolio.Portfolio.repartition(self.m, liquidity="high")) + self.assertListEqual( + ["medium", "high"], + list(market.Portfolio.liquidities.get().keys())) - self.assertEqual(1, self.wm.call_count) + liquidities = market.Portfolio.liquidities.get() + self.assertEqual(10, len(liquidities["medium"].keys())) + self.assertEqual(10, len(liquidities["high"].keys())) - portfolio.Portfolio.repartition(self.m) - self.assertEqual(1, self.wm.call_count) + expected = { + 'BTC': (D("0.2857"), "long"), + 'DGB': (D("0.1015"), "long"), + 'DOGE': (D("0.1805"), "long"), + 'SC': (D("0.0623"), "long"), + 'ZEC': (D("0.3701"), "long"), + } + date = portfolio.datetime(2018, 1, 8) + self.assertDictEqual(expected, liquidities["high"][date]) - portfolio.Portfolio.repartition(self.m, refetch=True) - self.assertEqual(2, self.wm.call_count) - self.m.report.log_http_request.assert_called() - self.assertEqual(2, self.m.report.log_http_request.call_count) + expected = { + 'BTC': (D("1.1102e-16"), "long"), + 'ETC': (D("0.1"), "long"), + 'FCT': (D("0.1"), "long"), + 'GAS': (D("0.1"), "long"), + 'NAV': (D("0.1"), "long"), + 'OMG': (D("0.1"), "long"), + 'OMNI': (D("0.1"), "long"), + 'PPC': (D("0.1"), "long"), + 'RIC': (D("0.1"), "long"), + 'VIA': (D("0.1"), "long"), + 'XCP': (D("0.1"), "long"), + } + self.assertDictEqual(expected, liquidities["medium"][date]) + self.assertEqual(portfolio.datetime(2018, 1, 15), market.Portfolio.last_date.get()) + + with self.subTest(description="Missing weight"): + data = store.json.loads(self.json_response, parse_int=D, parse_float=D) + del(data["portfolio_2"]["weights"]) + market.Portfolio.data = store.LockedVar(data) + + market.Portfolio.parse_cryptoportfolio() + self.assertListEqual( + ["medium", "high"], + list(market.Portfolio.liquidities.get().keys())) + self.assertEqual({}, market.Portfolio.liquidities.get("medium")) + + with self.subTest(description="All missing weights"): + data = store.json.loads(self.json_response, parse_int=D, parse_float=D) + del(data["portfolio_1"]["weights"]) + del(data["portfolio_2"]["weights"]) + market.Portfolio.data = store.LockedVar(data) + + market.Portfolio.parse_cryptoportfolio() + self.assertEqual({}, market.Portfolio.liquidities.get("medium")) + 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({ + "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() + self.assertEqual("medium_2018-03-08", market.Portfolio.repartition(liquidity="medium")) + self.assertEqual("high_2018-03-08", market.Portfolio.repartition(liquidity="high")) - @mock.patch.object(portfolio.time, "sleep") - @mock.patch.object(portfolio.Portfolio, "repartition") - def test_wait_for_recent(self, repartition, sleep): + @mock.patch.object(market.time, "sleep") + @mock.patch.object(market.Portfolio, "get_cryptoportfolio") + def test_wait_for_recent(self, get_cryptoportfolio, sleep): self.call_count = 0 - def _repartition(market, refetch): - self.assertEqual(self.m, market) - self.assertTrue(refetch) + def _get(refetch=False): + if self.call_count != 0: + self.assertTrue(refetch) + else: + self.assertFalse(refetch) self.call_count += 1 - portfolio.Portfolio.last_date = portfolio.datetime.now()\ - - portfolio.timedelta(10)\ - + portfolio.timedelta(self.call_count) - repartition.side_effect = _repartition + market.Portfolio.last_date = store.LockedVar(store.datetime.now()\ + - store.timedelta(10)\ + + store.timedelta(self.call_count)) + get_cryptoportfolio.side_effect = _get - portfolio.Portfolio.wait_for_recent(self.m) + market.Portfolio.wait_for_recent() sleep.assert_called_with(30) self.assertEqual(6, sleep.call_count) - self.assertEqual(7, repartition.call_count) - self.m.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio") + self.assertEqual(7, get_cryptoportfolio.call_count) + market.Portfolio.report.print_log.assert_called_with("Attempt to fetch up-to-date cryptoportfolio") sleep.reset_mock() - repartition.reset_mock() - portfolio.Portfolio.last_date = None + get_cryptoportfolio.reset_mock() + market.Portfolio.last_date = store.LockedVar(None) self.call_count = 0 - portfolio.Portfolio.wait_for_recent(self.m, delta=15) + market.Portfolio.wait_for_recent(delta=15) sleep.assert_not_called() - self.assertEqual(1, repartition.call_count) + self.assertEqual(1, get_cryptoportfolio.call_count) sleep.reset_mock() - repartition.reset_mock() - portfolio.Portfolio.last_date = None + get_cryptoportfolio.reset_mock() + market.Portfolio.last_date = store.LockedVar(None) self.call_count = 0 - portfolio.Portfolio.wait_for_recent(self.m, delta=1) + market.Portfolio.wait_for_recent(delta=1) sleep.assert_called_with(30) self.assertEqual(9, sleep.call_count) - self.assertEqual(10, repartition.call_count) + self.assertEqual(10, get_cryptoportfolio.call_count) + + def test_is_worker_thread(self): + with self.subTest(worker=None): + self.assertFalse(store.Portfolio.is_worker_thread()) + + with self.subTest(worker="not self"),\ + mock.patch("threading.current_thread") as current_thread: + current = mock.Mock() + current_thread.return_value = current + store.Portfolio.worker = mock.Mock() + self.assertFalse(store.Portfolio.is_worker_thread()) + + with self.subTest(worker="self"),\ + mock.patch("threading.current_thread") as current_thread: + current = mock.Mock() + current_thread.return_value = current + store.Portfolio.worker = current + self.assertTrue(store.Portfolio.is_worker_thread()) + + def test_start_worker(self): + with mock.patch.object(store.Portfolio, "wait_for_notification") as notification: + store.Portfolio.start_worker() + notification.assert_called_once_with(poll=30) + + self.assertEqual("lock", store.Portfolio.last_date.lock.__class__.__name__) + self.assertEqual("lock", store.Portfolio.liquidities.lock.__class__.__name__) + store.Portfolio.report.start_lock.assert_called_once_with() + + self.assertIsNotNone(store.Portfolio.worker) + self.assertIsNotNone(store.Portfolio.worker_notify) + self.assertIsNotNone(store.Portfolio.callback) + self.assertTrue(store.Portfolio.worker_started) @unittest.skipUnless("unit" in limits, "Unit skipped") class AmountTest(WebMockTestCase): @@ -1047,7 +1203,7 @@ class MarketTest(WebMockTestCase): self.assertEqual("Foo", m.fetch_fees()) self.ccxt.fetch_fees.assert_not_called() - @mock.patch.object(portfolio.Portfolio, "repartition") + @mock.patch.object(market.Portfolio, "repartition") @mock.patch.object(market.Market, "get_ticker") @mock.patch.object(market.TradeStore, "compute_trades") def test_prepare_trades(self, compute_trades, get_ticker, repartition): @@ -1098,7 +1254,7 @@ class MarketTest(WebMockTestCase): m.report.log_balances.assert_called_once_with(tag="tag") - @mock.patch.object(portfolio.time, "sleep") + @mock.patch.object(market.time, "sleep") @mock.patch.object(market.TradeStore, "all_orders") def test_follow_orders(self, all_orders, time_mock): for debug, sleep in [ @@ -1222,11 +1378,15 @@ class MarketTest(WebMockTestCase): def test_store_report(self): file_open = mock.mock_open() - with self.subTest(file=None), mock.patch("market.open", file_open): - m = market.Market(self.ccxt, user_id=1) + m = market.Market(self.ccxt, user_id=1) + with self.subTest(file=None),\ + mock.patch.object(m, "report") as report,\ + mock.patch("market.open", file_open): m.store_report() + report.merge.assert_called_with(store.Portfolio.report) file_open.assert_not_called() + report.reset_mock() file_open = mock.mock_open() m = market.Market(self.ccxt, report_path="present", user_id=1) with self.subTest(file="present"),\ @@ -1242,15 +1402,20 @@ class MarketTest(WebMockTestCase): file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") file_open().write.assert_called_once_with("json_content") m.report.to_json.assert_called_once_with() + report.merge.assert_called_with(store.Portfolio.report) + + report.reset_mock() m = market.Market(self.ccxt, report_path="error", user_id=1) with self.subTest(file="error"),\ mock.patch("market.open") as file_open,\ + mock.patch.object(m, "report") as report,\ mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: file_open.side_effect = FileNotFoundError m.store_report() + report.merge.assert_called_with(store.Portfolio.report) self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") def test_print_orders(self): @@ -1693,7 +1858,7 @@ class BalanceStoreTest(WebMockTestCase): self.assertListEqual(["USDT", "XVG", "XMR", "ETC"], list(balance_store.currencies())) self.m.report.log_balances.assert_called_with(tag="foo") - @mock.patch.object(portfolio.Portfolio, "repartition") + @mock.patch.object(market.Portfolio, "repartition") def test_dispatch_assets(self, repartition): self.m.ccxt.fetch_all_balances.return_value = self.fetch_balance @@ -1710,7 +1875,7 @@ class BalanceStoreTest(WebMockTestCase): repartition.return_value = repartition_hash amounts = balance_store.dispatch_assets(portfolio.Amount("BTC", "11.1")) - repartition.assert_called_with(self.m, liquidity="medium") + repartition.assert_called_with(liquidity="medium") self.assertIn("XEM", balance_store.currencies()) self.assertEqual(D("2.6"), amounts["BTC"].value) self.assertEqual(D("7.5"), amounts["XEM"].value) @@ -2801,6 +2966,19 @@ class ReportStoreTest(WebMockTestCase): report_store.set_verbose(False) self.assertFalse(report_store.verbose_print) + def test_merge(self): + report_store1 = market.ReportStore(self.m, verbose_print=False) + report_store2 = market.ReportStore(None, verbose_print=False) + + report_store2.log_stage("1") + report_store1.log_stage("2") + report_store2.log_stage("3") + + report_store1.merge(report_store2) + + self.assertEqual(3, len(report_store1.logs)) + self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs))) + def test_print_log(self): report_store = market.ReportStore(self.m) with self.subTest(verbose=True),\ @@ -3359,31 +3537,64 @@ class MainTest(WebMockTestCase): self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) def test_main(self): - with mock.patch("main.parse_args") as parse_args,\ - mock.patch("main.parse_config") as parse_config,\ - mock.patch("main.fetch_markets") as fetch_markets,\ - mock.patch("main.process") as process: + with self.subTest(parallel=False): + with mock.patch("main.parse_args") as parse_args,\ + mock.patch("main.parse_config") as parse_config,\ + mock.patch("main.fetch_markets") as fetch_markets,\ + mock.patch("main.process") as process: - args_mock = mock.Mock() - args_mock.config = "config" - args_mock.user = "user" - parse_args.return_value = args_mock + args_mock = mock.Mock() + args_mock.parallel = False + args_mock.config = "config" + args_mock.user = "user" + parse_args.return_value = args_mock - parse_config.return_value = ["pg_config", "report_path"] + parse_config.return_value = ["pg_config", "report_path"] - fetch_markets.return_value = [["config1", 1], ["config2", 2]] + fetch_markets.return_value = [["config1", 1], ["config2", 2]] - main.main(["Foo", "Bar"]) + main.main(["Foo", "Bar"]) - parse_args.assert_called_with(["Foo", "Bar"]) - parse_config.assert_called_with("config") - fetch_markets.assert_called_with("pg_config", "user") + parse_args.assert_called_with(["Foo", "Bar"]) + parse_config.assert_called_with("config") + fetch_markets.assert_called_with("pg_config", "user") - self.assertEqual(2, process.call_count) - process.assert_has_calls([ - mock.call("config1", 1, "report_path", args_mock), - mock.call("config2", 2, "report_path", args_mock), - ]) + self.assertEqual(2, process.call_count) + process.assert_has_calls([ + mock.call("config1", 1, "report_path", args_mock), + mock.call("config2", 2, "report_path", args_mock), + ]) + with self.subTest(parallel=True): + with mock.patch("main.parse_args") as parse_args,\ + mock.patch("main.parse_config") as parse_config,\ + mock.patch("main.fetch_markets") as fetch_markets,\ + mock.patch("main.process") as process,\ + mock.patch("store.Portfolio.start_worker") as start: + + args_mock = mock.Mock() + args_mock.parallel = True + args_mock.config = "config" + args_mock.user = "user" + parse_args.return_value = args_mock + + parse_config.return_value = ["pg_config", "report_path"] + + fetch_markets.return_value = [["config1", 1], ["config2", 2]] + + main.main(["Foo", "Bar"]) + + parse_args.assert_called_with(["Foo", "Bar"]) + parse_config.assert_called_with("config") + fetch_markets.assert_called_with("pg_config", "user") + + start.assert_called_once_with() + self.assertEqual(2, process.call_count) + process.assert_has_calls([ + mock.call.__bool__(), + mock.call("config1", 1, "report_path", args_mock), + mock.call.__bool__(), + mock.call("config2", 2, "report_path", args_mock), + ]) @mock.patch.object(main.sys, "exit") @mock.patch("main.configparser") @@ -3505,7 +3716,7 @@ class ProcessorTest(WebMockTestCase): processor.run_action("wait_for_recent", "bar", "baz") - method_mock.assert_called_with(self.m, foo="bar") + method_mock.assert_called_with(foo="bar") def test_select_step(self): processor = market.Processor(self.m) @@ -3547,8 +3758,8 @@ class ProcessorTest(WebMockTestCase): processor = market.Processor(m) method, arguments = processor.method_arguments("wait_for_recent") - self.assertEqual(portfolio.Portfolio.wait_for_recent, method) - self.assertEqual(["delta"], arguments) + self.assertEqual(market.Portfolio.wait_for_recent, method) + self.assertEqual(["delta", "poll"], arguments) method, arguments = processor.method_arguments("prepare_trades") self.assertEqual(m.prepare_trades, method) @@ -3730,7 +3941,7 @@ class AcceptanceTest(WebMockTestCase): market = mock.Mock() market.fetch_all_balances.return_value = fetch_balance market.fetch_ticker.side_effect = fetch_ticker - with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition): + with mock.patch.object(market.Portfolio, "repartition", return_value=repartition): # Action 1 helper.prepare_trades(market) @@ -3809,7 +4020,7 @@ class AcceptanceTest(WebMockTestCase): "amount": "10", "total": "1" } ] - with mock.patch.object(portfolio.time, "sleep") as sleep: + with mock.patch.object(market.time, "sleep") as sleep: # Action 4 helper.follow_orders(verbose=False) @@ -3850,7 +4061,7 @@ class AcceptanceTest(WebMockTestCase): } market.fetch_all_balances.return_value = fetch_balance - with mock.patch.object(portfolio.Portfolio, "repartition", return_value=repartition): + with mock.patch.object(market.Portfolio, "repartition", return_value=repartition): # Action 5 helper.prepare_trades(market, only="acquire", compute_value="average") @@ -3922,7 +4133,7 @@ class AcceptanceTest(WebMockTestCase): # TODO # portfolio.TradeStore.run_orders() - with mock.patch.object(portfolio.time, "sleep") as sleep: + with mock.patch.object(market.time, "sleep") as sleep: # Action 8 helper.follow_orders(verbose=False)