X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=test.py;h=ea1fd9a72c17da48fd398d68504125e5b04bbed7;hb=337c8286cc31d81ffdad06a225996f86c46c46f0;hp=3ee34c69a4ff974e428854107527c4070b858be7;hpb=07fa7a4bf8f7a6f799120fb9a5965a09bea6c38e;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FCryptoportfolio%2FTrader.git diff --git a/test.py b/test.py index 3ee34c6..ea1fd9a 100644 --- a/test.py +++ b/test.py @@ -70,6 +70,18 @@ class poloniexETest(unittest.TestCase): self.wm.stop() super(poloniexETest, self).tearDown() + def test__init(self): + with mock.patch("market.ccxt.poloniexE.session") as session: + session.request.return_value = "response" + ccxt = market.ccxt.poloniexE() + ccxt._market = mock.Mock + ccxt._market.report = mock.Mock() + + ccxt.session.request("GET", "URL", data="data", + headers="headers") + ccxt._market.report.log_http_request.assert_called_with('GET', 'URL', 'data', + 'headers', 'response') + def test_nanoseconds(self): with mock.patch.object(market.ccxt.time, "time") as time: time.return_value = 123456.7890123456 @@ -80,6 +92,58 @@ class poloniexETest(unittest.TestCase): time.return_value = 123456.7890123456 self.assertEqual(123456789012345, self.s.nonce()) + def test_request(self): + with mock.patch.object(market.ccxt.poloniex, "request") as request,\ + mock.patch("market.ccxt.retry_call") as retry_call: + with self.subTest(wrapped=True): + with self.subTest(desc="public"): + self.s.request("foo") + retry_call.assert_called_with(request, + delay=1, tries=10, fargs=["foo"], + fkwargs={'api': 'public', 'method': 'GET', 'params': {}, 'headers': None, 'body': None}, + exceptions=(market.ccxt.RequestTimeout,)) + request.assert_not_called() + + with self.subTest(desc="private GET"): + self.s.request("foo", api="private") + retry_call.assert_called_with(request, + delay=1, tries=10, fargs=["foo"], + fkwargs={'api': 'private', 'method': 'GET', 'params': {}, 'headers': None, 'body': None}, + exceptions=(market.ccxt.RequestTimeout,)) + request.assert_not_called() + + with self.subTest(desc="private POST regexp"): + self.s.request("returnFoo", api="private", method="POST") + retry_call.assert_called_with(request, + delay=1, tries=10, fargs=["returnFoo"], + fkwargs={'api': 'private', 'method': 'POST', 'params': {}, 'headers': None, 'body': None}, + exceptions=(market.ccxt.RequestTimeout,)) + request.assert_not_called() + + with self.subTest(desc="private POST non-regexp"): + self.s.request("getMarginPosition", api="private", method="POST") + retry_call.assert_called_with(request, + delay=1, tries=10, fargs=["getMarginPosition"], + fkwargs={'api': 'private', 'method': 'POST', 'params': {}, 'headers': None, 'body': None}, + exceptions=(market.ccxt.RequestTimeout,)) + request.assert_not_called() + retry_call.reset_mock() + request.reset_mock() + with self.subTest(wrapped=False): + with self.subTest(desc="private POST non-matching regexp"): + self.s.request("marginBuy", api="private", method="POST") + request.assert_called_with("marginBuy", + api="private", method="POST", params={}, + headers=None, body=None) + retry_call.assert_not_called() + + with self.subTest(desc="private POST non-matching non-regexp"): + self.s.request("closeMarginPositionOther", api="private", method="POST") + request.assert_called_with("closeMarginPositionOther", + api="private", method="POST", params={}, + headers=None, body=None) + retry_call.assert_not_called() + def test_order_precision(self): self.assertEqual(8, self.s.order_precision("FOO")) @@ -1125,17 +1189,11 @@ class MarketTest(WebMockTestCase): def test_from_config(self, ccxt): with mock.patch("market.ReportStore"): ccxt.poloniexE.return_value = self.ccxt - self.ccxt.session.request.return_value = "response" m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args()) self.assertEqual(self.ccxt, m.ccxt) - self.ccxt.session.request("GET", "URL", data="data", - headers="headers") - m.report.log_http_request.assert_called_with('GET', 'URL', 'data', - 'headers', 'response') - m = market.Market.from_config({"key": "key", "secred": "secret"}, self.market_args(debug=True)) self.assertEqual(True, m.debug) @@ -1386,18 +1444,140 @@ class MarketTest(WebMockTestCase): self.ccxt.transfer_balance.assert_any_call("USDT", 100, "exchange", "margin") self.ccxt.transfer_balance.assert_any_call("ETC", 5, "margin", "exchange") - def test_store_report(self): + m.report.reset_mock() + fetch_balances.reset_mock() + with self.subTest(retry=True): + with mock.patch("market.ReportStore"): + m = market.Market(self.ccxt, self.market_args()) - file_open = mock.mock_open() - m = market.Market(self.ccxt, self.market_args(), 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() + value_from = portfolio.Amount("BTC", "0.0") + value_from.linked_to = portfolio.Amount("ETH", "0.0") + value_to = portfolio.Amount("BTC", "-3.0") + trade = portfolio.Trade(value_from, value_to, "ETH", m) - report.reset_mock() + m.trades.all = [trade] + balance = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) + m.balances.all = {"BTC": balance} + + m.ccxt.transfer_balance.side_effect = [ + market.ccxt.RequestTimeout, + True + ] + m.move_balances() + self.ccxt.transfer_balance.assert_has_calls([ + mock.call("BTC", 3, "exchange", "margin"), + mock.call("BTC", 3, "exchange", "margin") + ]) + self.assertEqual(2, fetch_balances.call_count) + m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) + self.assertEqual(2, m.report.log_move_balances.call_count) + + self.ccxt.transfer_balance.reset_mock() + m.report.reset_mock() + fetch_balances.reset_mock() + with self.subTest(retry=True, too_much=True): + with mock.patch("market.ReportStore"): + m = market.Market(self.ccxt, self.market_args()) + + value_from = portfolio.Amount("BTC", "0.0") + value_from.linked_to = portfolio.Amount("ETH", "0.0") + value_to = portfolio.Amount("BTC", "-3.0") + trade = portfolio.Trade(value_from, value_to, "ETH", m) + + m.trades.all = [trade] + balance = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) + m.balances.all = {"BTC": balance} + + m.ccxt.transfer_balance.side_effect = [ + market.ccxt.RequestTimeout, + market.ccxt.RequestTimeout, + market.ccxt.RequestTimeout, + market.ccxt.RequestTimeout, + market.ccxt.RequestTimeout, + ] + with self.assertRaises(market.ccxt.RequestTimeout): + m.move_balances() + + self.ccxt.transfer_balance.reset_mock() + m.report.reset_mock() + fetch_balances.reset_mock() + with self.subTest(retry=True, partial_result=True): + with mock.patch("market.ReportStore"): + m = market.Market(self.ccxt, self.market_args()) + + value_from = portfolio.Amount("BTC", "1.0") + value_from.linked_to = portfolio.Amount("ETH", "10.0") + value_to = portfolio.Amount("BTC", "10.0") + trade1 = portfolio.Trade(value_from, value_to, "ETH", m) + + value_from = portfolio.Amount("BTC", "0.0") + value_from.linked_to = portfolio.Amount("ETH", "0.0") + value_to = portfolio.Amount("BTC", "-3.0") + trade2 = portfolio.Trade(value_from, value_to, "ETH", m) + + value_from = portfolio.Amount("USDT", "0.0") + value_from.linked_to = portfolio.Amount("XVG", "0.0") + value_to = portfolio.Amount("USDT", "-50.0") + trade3 = portfolio.Trade(value_from, value_to, "XVG", m) + + m.trades.all = [trade1, trade2, trade3] + balance1 = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "0" }) + balance2 = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "50" }) + balance3 = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "15" }) + m.balances.all = {"BTC": balance1, "USDT": balance2, "ETC": balance3} + + call_counts = { "BTC": 0, "USDT": 0, "ETC": 0 } + def _transfer_balance(currency, amount, from_, to_): + call_counts[currency] += 1 + if currency == "BTC": + m.balances.all["BTC"] = portfolio.Balance("BTC", { "margin_in_position": "0", "margin_available": "3" }) + if currency == "USDT": + if call_counts["USDT"] == 1: + raise market.ccxt.RequestTimeout + else: + m.balances.all["USDT"] = portfolio.Balance("USDT", { "margin_in_position": "100", "margin_available": "150" }) + if currency == "ETC": + m.balances.all["ETC"] = portfolio.Balance("ETC", { "margin_in_position": "10", "margin_available": "10" }) + + + m.ccxt.transfer_balance.side_effect = _transfer_balance + + m.move_balances() + self.ccxt.transfer_balance.assert_has_calls([ + mock.call("BTC", 3, "exchange", "margin"), + mock.call('USDT', 100, 'exchange', 'margin'), + mock.call('USDT', 100, 'exchange', 'margin'), + mock.call("ETC", 5, "margin", "exchange") + ]) + self.assertEqual(2, fetch_balances.call_count) + m.report.log_error.assert_called_with(mock.ANY, message="Retrying", exception=mock.ANY) + self.assertEqual(2, m.report.log_move_balances.call_count) + m.report.log_move_balances.asser_has_calls([ + mock.call( + { + 'BTC': portfolio.Amount("BTC", "3"), + 'USDT': portfolio.Amount("USDT", "150"), + 'ETC': portfolio.Amount("ETC", "10"), + }, + { + 'BTC': portfolio.Amount("BTC", "3"), + 'USDT': portfolio.Amount("USDT", "100"), + }), + mock.call( + { + 'BTC': portfolio.Amount("BTC", "3"), + 'USDT': portfolio.Amount("USDT", "150"), + 'ETC': portfolio.Amount("ETC", "10"), + }, + { + 'BTC': portfolio.Amount("BTC", "0"), + 'USDT': portfolio.Amount("USDT", "100"), + 'ETC': portfolio.Amount("ETC", "-5"), + }), + ]) + + + def test_store_file_report(self): file_open = mock.mock_open() m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1) with self.subTest(file="present"),\ @@ -1405,20 +1585,16 @@ class MarketTest(WebMockTestCase): mock.patch.object(m, "report") as report,\ mock.patch.object(market, "datetime") as time_mock: - time_mock.now.return_value = datetime.datetime(2018, 2, 25) report.print_logs = [[time_mock.now(), "Foo"], [time_mock.now(), "Bar"]] report.to_json.return_value = "json_content" - m.store_report() + m.store_file_report(datetime.datetime(2018, 2, 25)) file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") file_open.assert_any_call("present/2018-02-25T00:00:00_1.log", "w") file_open().write.assert_any_call("json_content") file_open().write.assert_any_call("Foo\nBar") m.report.to_json.assert_called_once_with() - report.merge.assert_called_with(store.Portfolio.report) - - report.reset_mock() m = market.Market(self.ccxt, self.market_args(), report_path="error", user_id=1) with self.subTest(file="error"),\ @@ -1427,10 +1603,106 @@ class MarketTest(WebMockTestCase): mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: file_open.side_effect = FileNotFoundError + m.store_file_report(datetime.datetime(2018, 2, 25)) + + self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") + + @mock.patch.object(market, "psycopg2") + def test_store_database_report(self, psycopg2): + connect_mock = mock.Mock() + cursor_mock = mock.MagicMock() + + connect_mock.cursor.return_value = cursor_mock + psycopg2.connect.return_value = connect_mock + m = market.Market(self.ccxt, self.market_args(), + pg_config={"config": "pg_config"}, user_id=1) + cursor_mock.fetchone.return_value = [42] + + with self.subTest(error=False),\ + mock.patch.object(m, "report") as report: + report.to_json_array.return_value = [ + ("date1", "type1", "payload1"), + ("date2", "type2", "payload2"), + ] + m.store_database_report(datetime.datetime(2018, 3, 24)) + connect_mock.assert_has_calls([ + mock.call.cursor(), + mock.call.cursor().execute('INSERT INTO reports("date", "market_config_id", "debug") VALUES (%s, %s, %s) RETURNING id;', (datetime.datetime(2018, 3, 24), None, False)), + mock.call.cursor().fetchone(), + mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date1', 42, 'type1', 'payload1')), + mock.call.cursor().execute('INSERT INTO report_lines("date", "report_id", "type", "payload") VALUES (%s, %s, %s, %s);', ('date2', 42, 'type2', 'payload2')), + mock.call.commit(), + mock.call.cursor().close(), + mock.call.close() + ]) + + connect_mock.reset_mock() + with self.subTest(error=True),\ + mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: + psycopg2.connect.side_effect = Exception("Bouh") + m.store_database_report(datetime.datetime(2018, 3, 24)) + self.assertEqual(stdout_mock.getvalue(), "impossible to store report to database: Exception; Bouh\n") + + def test_store_report(self): + m = market.Market(self.ccxt, self.market_args(), user_id=1) + with self.subTest(file=None, pg_config=None),\ + mock.patch.object(m, "report") as report,\ + mock.patch.object(m, "store_database_report") as db_report,\ + mock.patch.object(m, "store_file_report") as file_report: + m.store_report() + report.merge.assert_called_with(store.Portfolio.report) + + file_report.assert_not_called() + db_report.assert_not_called() + + report.reset_mock() + m = market.Market(self.ccxt, self.market_args(), report_path="present", user_id=1) + with self.subTest(file="present", pg_config=None),\ + mock.patch.object(m, "report") as report,\ + mock.patch.object(m, "store_file_report") as file_report,\ + mock.patch.object(m, "store_database_report") as db_report,\ + mock.patch.object(market, "datetime") as time_mock: + + time_mock.now.return_value = datetime.datetime(2018, 2, 25) + m.store_report() report.merge.assert_called_with(store.Portfolio.report) - self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") + file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) + db_report.assert_not_called() + + report.reset_mock() + m = market.Market(self.ccxt, self.market_args(), pg_config="present", user_id=1) + with self.subTest(file=None, pg_config="present"),\ + mock.patch.object(m, "report") as report,\ + mock.patch.object(m, "store_file_report") as file_report,\ + mock.patch.object(m, "store_database_report") as db_report,\ + mock.patch.object(market, "datetime") as time_mock: + + time_mock.now.return_value = datetime.datetime(2018, 2, 25) + + m.store_report() + + report.merge.assert_called_with(store.Portfolio.report) + file_report.assert_not_called() + db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) + + report.reset_mock() + m = market.Market(self.ccxt, self.market_args(), + pg_config="pg_config", report_path="present", user_id=1) + with self.subTest(file="present", pg_config="present"),\ + mock.patch.object(m, "report") as report,\ + mock.patch.object(m, "store_file_report") as file_report,\ + mock.patch.object(m, "store_database_report") as db_report,\ + mock.patch.object(market, "datetime") as time_mock: + + time_mock.now.return_value = datetime.datetime(2018, 2, 25) + + m.store_report() + + report.merge.assert_called_with(store.Portfolio.report) + file_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) + db_report.assert_called_once_with(datetime.datetime(2018, 2, 25)) def test_print_orders(self): m = market.Market(self.ccxt, self.market_args()) @@ -3050,6 +3322,14 @@ class ReportStoreTest(WebMockTestCase): report_store.print_log(portfolio.Amount("BTC", 1)) self.assertEqual(stdout_mock.getvalue(), "") + def test_default_json_serial(self): + report_store = market.ReportStore(self.m) + + self.assertEqual("2018-02-24T00:00:00", + report_store.default_json_serial(portfolio.datetime(2018, 2, 24))) + self.assertEqual("1.00000000 BTC", + report_store.default_json_serial(portfolio.Amount("BTC", 1))) + def test_to_json(self): report_store = market.ReportStore(self.m) report_store.logs.append({"foo": "bar"}) @@ -3059,6 +3339,20 @@ class ReportStoreTest(WebMockTestCase): report_store.logs.append({"amount": portfolio.Amount("BTC", 1)}) self.assertEqual('[\n {\n "foo": "bar"\n },\n {\n "date": "2018-02-24T00:00:00"\n },\n {\n "amount": "1.00000000 BTC"\n }\n]', report_store.to_json()) + def test_to_json_array(self): + report_store = market.ReportStore(self.m) + report_store.logs.append({ + "date": "date1", "type": "type1", "foo": "bar", "bla": "bla" + }) + report_store.logs.append({ + "date": "date2", "type": "type2", "foo": "bar", "bla": "bla" + }) + logs = list(report_store.to_json_array()) + + self.assertEqual(2, len(logs)) + self.assertEqual(("date1", "type1", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[0]) + self.assertEqual(("date2", "type2", '{\n "foo": "bar",\n "bla": "bla"\n}'), logs[1]) + @mock.patch.object(market.ReportStore, "print_log") @mock.patch.object(market.ReportStore, "add_log") def test_log_stage(self, add_log, print_log): @@ -3552,7 +3846,7 @@ class MainTest(WebMockTestCase): mock.patch("main.parse_config") as main_parse_config: with self.subTest(debug=False): main_parse_config.return_value = ["pg_config", "report_path"] - main_fetch_markets.return_value = [({"key": "market_config"},)] + main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)] m = main.get_user_market("config_path.ini", 1) self.assertIsInstance(m, market.Market) @@ -3560,7 +3854,7 @@ class MainTest(WebMockTestCase): with self.subTest(debug=True): main_parse_config.return_value = ["pg_config", "report_path"] - main_fetch_markets.return_value = [({"key": "market_config"},)] + main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)] m = main.get_user_market("config_path.ini", 1, debug=True) self.assertIsInstance(m, market.Market) @@ -3579,16 +3873,16 @@ class MainTest(WebMockTestCase): args_mock.after = "after" self.assertEqual("", stdout_mock.getvalue()) - main.process("config", 1, "report_path", args_mock) + main.process("config", 3, 1, args_mock, "report_path", "pg_config") market_mock.from_config.assert_has_calls([ - mock.call("config", args_mock, user_id=1, report_path="report_path"), + mock.call("config", args_mock, pg_config="pg_config", market_id=3, user_id=1, report_path="report_path"), mock.call().process("action", before="before", after="after"), ]) with self.subTest(exception=True): market_mock.from_config.side_effect = Exception("boo") - main.process("config", 1, "report_path", args_mock) + main.process(3, "config", 1, "report_path", args_mock, "pg_config") self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) def test_main(self): @@ -3606,7 +3900,7 @@ class MainTest(WebMockTestCase): parse_config.return_value = ["pg_config", "report_path"] - fetch_markets.return_value = [["config1", 1], ["config2", 2]] + fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] main.main(["Foo", "Bar"]) @@ -3616,8 +3910,8 @@ class MainTest(WebMockTestCase): 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), + mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"), + mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"), ]) with self.subTest(parallel=True): with mock.patch("main.parse_args") as parse_args,\ @@ -3634,7 +3928,7 @@ class MainTest(WebMockTestCase): parse_config.return_value = ["pg_config", "report_path"] - fetch_markets.return_value = [["config1", 1], ["config2", 2]] + fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] main.main(["Foo", "Bar"]) @@ -3646,9 +3940,9 @@ class MainTest(WebMockTestCase): self.assertEqual(2, process.call_count) process.assert_has_calls([ mock.call.__bool__(), - mock.call("config1", 1, "report_path", args_mock), + mock.call("config1", 3, 1, args_mock, "report_path", "pg_config"), mock.call.__bool__(), - mock.call("config2", 2, "report_path", args_mock), + mock.call("config2", 1, 2, args_mock, "report_path", "pg_config"), ]) @mock.patch.object(main.sys, "exit") @@ -3734,7 +4028,7 @@ class MainTest(WebMockTestCase): rows = list(main.fetch_markets({"foo": "bar"}, None)) psycopg2.connect.assert_called_once_with(foo="bar") - cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs") + cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs") self.assertEqual(["row_1", "row_2"], rows) @@ -3744,7 +4038,7 @@ class MainTest(WebMockTestCase): rows = list(main.fetch_markets({"foo": "bar"}, 1)) psycopg2.connect.assert_called_once_with(foo="bar") - cursor_mock.execute.assert_called_once_with("SELECT config,user_id FROM market_configs WHERE user_id = %s", 1) + cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE user_id = %s", 1) self.assertEqual(["row_1", "row_2"], rows)