Skip to content

Commit 034baa8

Browse files
committed
Pytest utilities
1 parent 2398bb2 commit 034baa8

File tree

3 files changed

+155
-2
lines changed

3 files changed

+155
-2
lines changed

mqpy/utilities.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def check_trade_availability(self, symbol: str, count_until: int) -> bool:
5757
self.__counter_flag = True
5858

5959
if self.__minutes_counter == count_until:
60-
logger.info("Trading is allowed.\n")
60+
logger.info("Trading is allowed.")
6161
self.__reset_counters()
6262

6363
return self.__allowed_to_trade
@@ -68,3 +68,59 @@ def __reset_counters(self) -> None:
6868
self.__counter_flag = True
6969
self.__allow_to_count = False
7070
self.__allowed_to_trade = True
71+
72+
# Test-only methods
73+
def _test_get_minutes_counter(self) -> int:
74+
"""Get the minutes counter (for testing only)."""
75+
logger.warning("This method is for testing purposes only and should not be used in production code.")
76+
return self.__minutes_counter
77+
78+
def _test_get_counter_flag(self) -> bool:
79+
"""Get the counter flag (for testing only)."""
80+
logger.warning("This method is for testing purposes only and should not be used in production code.")
81+
return self.__counter_flag
82+
83+
def _test_get_allowed_to_trade(self) -> bool:
84+
"""Get the allowed to trade flag (for testing only)."""
85+
logger.warning("This method is for testing purposes only and should not be used in production code.")
86+
return self.__allowed_to_trade
87+
88+
def _test_get_allow_to_count(self) -> bool:
89+
"""Get the allow to count flag (for testing only)."""
90+
logger.warning("This method is for testing purposes only and should not be used in production code.")
91+
return self.__allow_to_count
92+
93+
def _test_get_recent_trade(self) -> bool:
94+
"""Get the recent trade flag (for testing only)."""
95+
logger.warning("This method is for testing purposes only and should not be used in production code.")
96+
return self.__recent_trade
97+
98+
def _test_set_minutes_counter(self, value: int) -> None:
99+
"""Set the minutes counter (for testing only)."""
100+
logger.warning("This method is for testing purposes only and should not be used in production code.")
101+
self.__minutes_counter = value
102+
103+
def _test_set_counter_flag(self, value: bool) -> None:
104+
"""Set the counter flag (for testing only)."""
105+
logger.warning("This method is for testing purposes only and should not be used in production code.")
106+
self.__counter_flag = value
107+
108+
def _test_set_allowed_to_trade(self, value: bool) -> None:
109+
"""Set the allowed to trade flag (for testing only)."""
110+
logger.warning("This method is for testing purposes only and should not be used in production code.")
111+
self.__allowed_to_trade = value
112+
113+
def _test_set_allow_to_count(self, value: bool) -> None:
114+
"""Set the allow to count flag (for testing only)."""
115+
logger.warning("This method is for testing purposes only and should not be used in production code.")
116+
self.__allow_to_count = value
117+
118+
def _test_set_recent_trade(self, value: bool) -> None:
119+
"""Set the recent trade flag (for testing only)."""
120+
logger.warning("This method is for testing purposes only and should not be used in production code.")
121+
self.__recent_trade = value
122+
123+
def _test_reset_counters(self) -> None:
124+
"""Reset counters (for testing only)."""
125+
logger.warning("This method is for testing purposes only and should not be used in production code.")
126+
self.__reset_counters()

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ max-complexity = 10
134134

135135
[tool.ruff.lint.per-file-ignores]
136136
"conftest.py" = ["S101", "D100", "D103", "D417", "FBT001", "INP001"]
137-
"test_*.py" = ["S101", "D100", "D103", "D417", "FBT001", "INP001"]
137+
"test_*.py" = ["S101", "D100", "D103", "D417", "FBT001", "INP001", "SLF001", "FBT003"]
138+
"mqpy/utilities.py" = ["FBT001"]
138139

139140
[tool.ruff.lint.pydocstyle]
140141
convention = "google"

tests/test_utilities.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Tests for the Utilities class that provides helper functions for trading operations."""
2+
3+
from __future__ import annotations
4+
5+
import time
6+
from typing import Generator
7+
8+
import MetaTrader5 as Mt5
9+
import pytest
10+
11+
from mqpy.utilities import Utilities
12+
13+
14+
@pytest.fixture(scope="module", autouse=True)
15+
def setup_teardown() -> Generator[None, None, None]:
16+
"""Set up and tear down MetaTrader5 connection for the test module."""
17+
if not Mt5.initialize():
18+
pytest.skip("MetaTrader5 could not be initialized")
19+
20+
time.sleep(5)
21+
22+
yield
23+
24+
Mt5.shutdown()
25+
26+
27+
@pytest.fixture
28+
def symbol() -> str:
29+
"""Provides a valid trading symbol for testing."""
30+
time.sleep(1)
31+
32+
symbols = Mt5.symbols_get()
33+
if not symbols:
34+
pytest.skip("No symbols available for testing")
35+
36+
for symbol in symbols:
37+
if symbol.name == "EURUSD":
38+
return "EURUSD"
39+
40+
return symbols[0].name
41+
42+
43+
@pytest.fixture
44+
def utilities() -> Utilities:
45+
"""Provides a Utilities instance for testing."""
46+
return Utilities()
47+
48+
49+
def test_utilities_initialization() -> None:
50+
"""Test initialization of Utilities class."""
51+
utilities = Utilities()
52+
assert isinstance(utilities, Utilities)
53+
symbol = "EURUSD"
54+
assert utilities.check_trade_availability(symbol, 5) is True
55+
56+
57+
def test_multiple_utilities_instances() -> None:
58+
"""Test that multiple Utilities instances work independently."""
59+
utilities1 = Utilities()
60+
utilities2 = Utilities()
61+
assert utilities1.check_trade_availability("EURUSD", 5) is True
62+
assert utilities2.check_trade_availability("EURUSD", 5) is True
63+
assert utilities1 is not utilities2
64+
65+
66+
def test_utilities_attributes(utilities: Utilities) -> None:
67+
"""Test that the Utilities class has the expected attributes."""
68+
assert hasattr(utilities, "_test_get_minutes_counter")
69+
assert hasattr(utilities, "_test_get_counter_flag")
70+
assert hasattr(utilities, "_test_get_allowed_to_trade")
71+
assert hasattr(utilities, "_test_get_allow_to_count")
72+
assert hasattr(utilities, "_test_get_recent_trade")
73+
74+
assert utilities._test_get_minutes_counter() == 0
75+
assert utilities._test_get_counter_flag() is True
76+
assert utilities._test_get_allowed_to_trade() is True
77+
assert utilities._test_get_allow_to_count() is False
78+
assert utilities._test_get_recent_trade() is False
79+
80+
assert hasattr(utilities, "check_trade_availability")
81+
assert callable(utilities.check_trade_availability)
82+
83+
84+
def test_reset_counters_functionality(utilities: Utilities) -> None:
85+
"""Test the reset_counters functionality directly."""
86+
utilities._test_set_minutes_counter(5)
87+
utilities._test_set_counter_flag(False)
88+
utilities._test_set_allowed_to_trade(False)
89+
utilities._test_set_allow_to_count(True)
90+
91+
utilities._test_reset_counters()
92+
93+
assert utilities._test_get_minutes_counter() == 0
94+
assert utilities._test_get_counter_flag() is True
95+
assert utilities._test_get_allowed_to_trade() is True
96+
assert utilities._test_get_allow_to_count() is False

0 commit comments

Comments
 (0)