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)
- with open(report_file, "w") as f:
+ report_file = "{}/{}_{}".format(self.report_path, datetime.now().isoformat(), self.user_id)
+ with open(report_file + ".json", "w") as f:
f.write(self.report.to_json())
+ with open(report_file + ".log", "w") as f:
+ f.write("\n".join(map(lambda x: x[1], self.report.print_logs)))
except Exception as e:
print("impossible to store report file: {}; {}".format(e.__class__.__name__, e))
self.market = market
self.verbose_print = verbose_print
+ self.print_logs = []
self.logs = []
def merge(self, other_report):
self.logs += other_report.logs
self.logs.sort(key=lambda x: x["date"])
+ self.print_logs += other_report.print_logs
+ self.print_logs.sort(key=lambda x: x[0])
+
def print_log(self, message):
- message = str(message)
+ now = datetime.now()
+ message = "{:%Y-%m-%d %H:%M:%S}: {}".format(now, str(message))
+ self.print_logs.append([now, message])
if self.verbose_print:
print(message)
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()
file_open.assert_any_call("present/2018-02-25T00:00:00_1.json", "w")
- file_open().write.assert_called_once_with("json_content")
+ 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)
self.assertEqual(3, len(report_store1.logs))
self.assertEqual(["1", "2", "3"], list(map(lambda x: x["stage"], report_store1.logs)))
+ self.assertEqual(6, len(report_store1.print_logs))
def test_print_log(self):
report_store = market.ReportStore(self.m)
with self.subTest(verbose=True),\
+ mock.patch.object(store, "datetime") as time_mock,\
mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock:
+ time_mock.now.return_value = datetime.datetime(2018, 2, 25, 2, 20, 10)
report_store.set_verbose(True)
report_store.print_log("Coucou")
report_store.print_log(portfolio.Amount("BTC", 1))
- self.assertEqual(stdout_mock.getvalue(), "Coucou\n1.00000000 BTC\n")
+ self.assertEqual(stdout_mock.getvalue(), "2018-02-25 02:20:10: Coucou\n2018-02-25 02:20:10: 1.00000000 BTC\n")
with self.subTest(verbose=False),\
mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: