aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--market.py6
-rw-r--r--store.py8
-rw-r--r--test.py10
3 files changed, 19 insertions, 5 deletions
diff --git a/market.py b/market.py
index 8672c59..2ddebfa 100644
--- a/market.py
+++ b/market.py
@@ -49,9 +49,11 @@ class Market:
49 self.report.merge(Portfolio.report) 49 self.report.merge(Portfolio.report)
50 try: 50 try:
51 if self.report_path is not None: 51 if self.report_path is not None:
52 report_file = "{}/{}_{}.json".format(self.report_path, datetime.now().isoformat(), self.user_id) 52 report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id)
53 with open(report_file, "w") as f: 53 with open(report_file + ".json", "w") as f:
54 f.write(self.report.to_json()) 54 f.write(self.report.to_json())
55 with open(report_file + ".log", "w") as f:
56 f.write("\n".join(map(lambda x: x[1], self.report.print_logs)))
55 except Exception as e: 57 except Exception as e:
56 print("impossible to store report file: {}; {}".format(e.__class__.__name__, e)) 58 print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
57 59
diff --git a/store.py b/store.py
index f655be5..d875a98 100644
--- a/store.py
+++ b/store.py
@@ -15,14 +15,20 @@ class ReportStore:
15 self.market = market 15 self.market = market
16 self.verbose_print = verbose_print 16 self.verbose_print = verbose_print
17 17
18 self.print_logs = []
18 self.logs = [] 19 self.logs = []
19 20
20 def merge(self, other_report): 21 def merge(self, other_report):
21 self.logs += other_report.logs 22 self.logs += other_report.logs
22 self.logs.sort(key=lambda x: x["date"]) 23 self.logs.sort(key=lambda x: x["date"])
23 24
25 self.print_logs += other_report.print_logs
26 self.print_logs.sort(key=lambda x: x[0])
27
24 def print_log(self, message): 28 def print_log(self, message):
25 message = str(message) 29 now = datetime.now()
30 message = "{:%Y-%m-%d %H:%M:%S}: {}".format(now, str(message))
31 self.print_logs.append([now, message])
26 if self.verbose_print: 32 if self.verbose_print:
27 print(message) 33 print(message)
28 34
diff --git a/test.py b/test.py
index ac9a6cd..13bd332 100644
--- a/test.py
+++ b/test.py
@@ -1395,12 +1395,15 @@ class MarketTest(WebMockTestCase):
1395 mock.patch.object(market, "datetime") as time_mock: 1395 mock.patch.object(market, "datetime") as time_mock:
1396 1396
1397 time_mock.now.return_value = datetime.datetime(2018, 2, 25) 1397 time_mock.now.return_value = datetime.datetime(2018, 2, 25)
1398 report.print_logs = [[time_mock.now(), "Foo"], [time_mock.now(), "Bar"]]
1398 report.to_json.return_value = "json_content" 1399 report.to_json.return_value = "json_content"
1399 1400
1400 m.store_report() 1401 m.store_report()
1401 1402
1402 file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w") 1403 file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
1403 file_open().write.assert_called_once_with("json_content") 1404 file_open.assert_any_call("present/2018-02-25T00:00:00_1.log", "w")
1405 file_open().write.assert_any_call("json_content")
1406 file_open().write.assert_any_call("Foo\nBar")
1404 m.report.to_json.assert_called_once_with() 1407 m.report.to_json.assert_called_once_with()
1405 report.merge.assert_called_with(store.Portfolio.report) 1408 report.merge.assert_called_with(store.Portfolio.report)
1406 1409
@@ -3016,15 +3019,18 @@ class ReportStoreTest(WebMockTestCase):
3016 3019
3017 self.assertEqual(3, len(report_store1.logs)) 3020 self.assertEqual(3, len(report_store1.logs))
3018 self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs))) 3021 self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs)))
3022 self.assertEqual(6, len(report_store1.print_logs))
3019 3023
3020 def test_print_log(self): 3024 def test_print_log(self):
3021 report_store = market.ReportStore(self.m) 3025 report_store = market.ReportStore(self.m)
3022 with self.subTest(verbose=True),\ 3026 with self.subTest(verbose=True),\
3027 mock.patch.object(store, "datetime") as time_mock,\
3023 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: 3028 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
3029 time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10)
3024 report_store.set_verbose(True) 3030 report_store.set_verbose(True)
3025 report_store.print_log("Coucou") 3031 report_store.print_log("Coucou")
3026 report_store.print_log(portfolio.Amount("BTC", 1)) 3032 report_store.print_log(portfolio.Amount("BTC", 1))
3027 self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n") 3033 self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n")
3028 3034
3029 with self.subTest(verbose=False),\ 3035 with self.subTest(verbose=False),\
3030 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: 3036 mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: