From 9bde69bfc1dcd17a92ac2ee5abfcda5b30034d93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Thu, 8 Mar 2018 02:15:50 +0100 Subject: Add merge method for report --- market.py | 1 + store.py | 4 ++++ test.py | 26 ++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/market.py b/market.py index 388dea0..c9629c9 100644 --- a/market.py +++ b/market.py @@ -46,6 +46,7 @@ class Market: return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path) def store_report(self): + self.report.merge(Portfolio.report) try: if self.report_path is not None: report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id) diff --git a/store.py b/store.py index 78dfe2d..c8cdc42 100644 --- a/store.py +++ b/store.py @@ -17,6 +17,10 @@ class ReportStore: self.logs = [] + def merge(self, other_report): + self.logs += other_report.logs + self.logs.sort(key=lambda x: x["date"]) + def print_log(self, message): message = str(message) if self.verbose_print: diff --git a/test.py b/test.py index d4432f6..c0e6a8a 100644 --- a/test.py +++ b/test.py @@ -1203,11 +1203,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"),\ @@ -1223,15 +1227,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): @@ -2782,6 +2791,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),\ -- cgit v1.2.3