]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Trader.git/commitdiff
Add merge method for report
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 8 Mar 2018 01:15:50 +0000 (02:15 +0100)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Thu, 8 Mar 2018 10:35:16 +0000 (11:35 +0100)
market.py
store.py
test.py

index 388dea0cadb1265f966c1aa4afe2c66b85f93c26..c9629c9762d9384e09385fc824b9f74551955147 100644 (file)
--- 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)
index 78dfe2dcba77390d3fffa42df937c1704b393949..c8cdc42b8b3a290974035869522f807b39c205d6 100644 (file)
--- 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 d4432f6e7a87636efc8c94913382b385177a4841..c0e6a8aa68aa30c7a06dc42f39164de1d7a6ca63 100644 (file)
--- 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),\