From 5c24d2d72d92eb17d9df558261528ad24bb6b71f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 11 Mar 2026 18:33:09 +0000 Subject: [PATCH] test: add error path test for live trading get_data - Add check for `mt5.copy_rates_from_pos` returning `None` in `live_trading/multi_bar.py:82` - Add test in `tests/test_multi_bar.py` verifying that when `mt5.copy_rates_from_pos` returns `None`, the function returns `None` and an error is logged. Co-authored-by: maghdam <63883156+maghdam@users.noreply.github.com> --- live_trading/multi_bar.py | 3 +++ tests/__init__.py | 0 tests/test_multi_bar.py | 24 ++++++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/test_multi_bar.py diff --git a/live_trading/multi_bar.py b/live_trading/multi_bar.py index a5334a0..c3afd05 100644 --- a/live_trading/multi_bar.py +++ b/live_trading/multi_bar.py @@ -82,6 +82,9 @@ class TradingApp: Fetch 'n' bars of historical data for the given symbol and timeframe. """ rates = mt5.copy_rates_from_pos(symbol, timeframe, 0, n) + if rates is None: + log_and_print(f"Could not retrieve data for {symbol}", is_error=True) + return None rates_frame = pd.DataFrame(rates) rates_frame['time'] = pd.to_datetime(rates_frame['time'], unit='s') rates_frame.set_index('time', inplace=True) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_multi_bar.py b/tests/test_multi_bar.py new file mode 100644 index 0000000..13d4119 --- /dev/null +++ b/tests/test_multi_bar.py @@ -0,0 +1,24 @@ +import sys +from unittest.mock import MagicMock +# Mock out MetaTrader5 before importing our module +sys.modules['MetaTrader5'] = MagicMock() + +import unittest +from unittest.mock import patch +import pandas as pd +from live_trading.multi_bar import TradingApp, log_and_print + +class TestTradingApp(unittest.TestCase): + def setUp(self): + self.app = TradingApp(symbol="EURUSD", lot_size=0.01, magic_number=123456) + + @patch("live_trading.multi_bar.mt5.copy_rates_from_pos") + @patch("live_trading.multi_bar.log_and_print") + def test_get_data_returns_none(self, mock_log, mock_copy_rates): + mock_copy_rates.return_value = None + + # Test what happens when mt5.copy_rates_from_pos returns None + result = self.app.get_data("EURUSD", 100, 16408) + + self.assertIsNone(result) + mock_log.assert_called_once_with("Could not retrieve data for EURUSD", is_error=True)