diff options
author | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-08 02:15:50 +0100 |
---|---|---|
committer | Ismaël Bouya <ismael.bouya@normalesup.org> | 2018-03-08 11:35:16 +0100 |
commit | 9bde69bfc1dcd17a92ac2ee5abfcda5b30034d93 (patch) | |
tree | 456489d0088ab36dd7b3c438a597183134f43ccc | |
parent | ada1b5f109ebaa6f3adb7cd87b007c6db891811c (diff) | |
download | Trader-9bde69bfc1dcd17a92ac2ee5abfcda5b30034d93.tar.gz Trader-9bde69bfc1dcd17a92ac2ee5abfcda5b30034d93.tar.zst Trader-9bde69bfc1dcd17a92ac2ee5abfcda5b30034d93.zip |
Add merge method for report
-rw-r--r-- | market.py | 1 | ||||
-rw-r--r-- | store.py | 4 | ||||
-rw-r--r-- | test.py | 26 |
3 files changed, 29 insertions, 2 deletions
@@ -46,6 +46,7 @@ class Market: | |||
46 | return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path) | 46 | return cls(ccxt_instance, debug=debug, user_id=user_id, report_path=report_path) |
47 | 47 | ||
48 | def store_report(self): | 48 | def store_report(self): |
49 | self.report.merge(Portfolio.report) | ||
49 | try: | 50 | try: |
50 | if self.report_path is not None: | 51 | if self.report_path is not None: |
51 | report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id) | 52 | report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id) |
@@ -17,6 +17,10 @@ class ReportStore: | |||
17 | 17 | ||
18 | self.logs = [] | 18 | self.logs = [] |
19 | 19 | ||
20 | def merge(self, other_report): | ||
21 | self.logs += other_report.logs | ||
22 | self.logs.sort(key=lambda x: x["date"]) | ||
23 | |||
20 | def print_log(self, message): | 24 | def print_log(self, message): |
21 | message = str(message) | 25 | message = str(message) |
22 | if self.verbose_print: | 26 | if self.verbose_print: |
@@ -1203,11 +1203,15 @@ class MarketTest(WebMockTestCase): | |||
1203 | def test_store_report(self): | 1203 | def test_store_report(self): |
1204 | 1204 | ||
1205 | file_open = mock.mock_open() | 1205 | file_open = mock.mock_open() |
1206 | with self.subTest(file=None), mock.patch("market.open", file_open): | 1206 | m = market.Market(self.ccxt, user_id=1) |
1207 | m = market.Market(self.ccxt, user_id=1) | 1207 | with self.subTest(file=None),\ |
1208 | mock.patch.object(m, "report") as report,\ | ||
1209 | mock.patch("market.open", file_open): | ||
1208 | m.store_report() | 1210 | m.store_report() |
1211 | report.merge.assert_called_with(store.Portfolio.report) | ||
1209 | file_open.assert_not_called() | 1212 | file_open.assert_not_called() |
1210 | 1213 | ||
1214 | report.reset_mock() | ||
1211 | file_open = mock.mock_open() | 1215 | file_open = mock.mock_open() |
1212 | m = market.Market(self.ccxt, report_path="present", user_id=1) | 1216 | m = market.Market(self.ccxt, report_path="present", user_id=1) |
1213 | with self.subTest(file="present"),\ | 1217 | with self.subTest(file="present"),\ |
@@ -1223,15 +1227,20 @@ class MarketTest(WebMockTestCase): | |||
1223 | file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") | 1227 | file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") |
1224 | file_open().write.assert_called_once_with("json_content") | 1228 | file_open().write.assert_called_once_with("json_content") |
1225 | m.report.to_json.assert_called_once_with() | 1229 | m.report.to_json.assert_called_once_with() |
1230 | report.merge.assert_called_with(store.Portfolio.report) | ||
1231 | |||
1232 | report.reset_mock() | ||
1226 | 1233 | ||
1227 | m = market.Market(self.ccxt, report_path="error", user_id=1) | 1234 | m = market.Market(self.ccxt, report_path="error", user_id=1) |
1228 | with self.subTest(file="error"),\ | 1235 | with self.subTest(file="error"),\ |
1229 | mock.patch("market.open") as file_open,\ | 1236 | mock.patch("market.open") as file_open,\ |
1237 | mock.patch.object(m, "report") as report,\ | ||
1230 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | 1238 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: |
1231 | file_open.side_effect = FileNotFoundError | 1239 | file_open.side_effect = FileNotFoundError |
1232 | 1240 | ||
1233 | m.store_report() | 1241 | m.store_report() |
1234 | 1242 | ||
1243 | report.merge.assert_called_with(store.Portfolio.report) | ||
1235 | self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") | 1244 | self.assertRegex(stdout_mock.getvalue(), "impossible to store report file: FileNotFoundError;") |
1236 | 1245 | ||
1237 | def test_print_orders(self): | 1246 | def test_print_orders(self): |
@@ -2782,6 +2791,19 @@ class ReportStoreTest(WebMockTestCase): | |||
2782 | report_store.set_verbose(False) | 2791 | report_store.set_verbose(False) |
2783 | self.assertFalse(report_store.verbose_print) | 2792 | self.assertFalse(report_store.verbose_print) |
2784 | 2793 | ||
2794 | def test_merge(self): | ||
2795 | report_store1 = market.ReportStore(self.m, verbose_print=False) | ||
2796 | report_store2 = market.ReportStore(None, verbose_print=False) | ||
2797 | |||
2798 | report_store2.log_stage("1") | ||
2799 | report_store1.log_stage("2") | ||
2800 | report_store2.log_stage("3") | ||
2801 | |||
2802 | report_store1.merge(report_store2) | ||
2803 | |||
2804 | self.assertEqual(3, len(report_store1.logs)) | ||
2805 | self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs))) | ||
2806 | |||
2785 | def test_print_log(self): | 2807 | def test_print_log(self): |
2786 | report_store = market.ReportStore(self.m) | 2808 | report_store = market.ReportStore(self.m) |
2787 | with self.subTest(verbose=True),\ | 2809 | with self.subTest(verbose=True),\ |