Skip to content

Commit 7fcfaa5

Browse files
committed
pre-commit
1 parent 03a4d3c commit 7fcfaa5

File tree

6 files changed

+88
-81
lines changed

6 files changed

+88
-81
lines changed

.github/workflows/test-metatrader5-integration.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: Test | Integration Test
22

3-
on: [push]
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 0 * * 0' # Run every week monday at 00:00
47

58
jobs:
69
build:

.github/workflows/test-pytest.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ on:
88
jobs:
99
pytest:
1010
runs-on: windows-latest
11-
11+
1212
steps:
1313
- name: Checkout repository
1414
uses: actions/checkout@v4
15-
15+
1616
- name: Set up Python
1717
uses: actions/setup-python@v4
1818
with:
@@ -24,16 +24,16 @@ jobs:
2424
python -m pip install --upgrade pip
2525
pip install pytest pytest-cov MetaTrader5
2626
pip install -e .
27-
27+
2828
- name: Run tests with coverage
2929
run: |
3030
pytest -k . --cov=mqpy --cov-append --cov-report=term-missing:skip-covered --junitxml=pytest.xml -x | tee pytest-coverage.txt
31-
31+
3232
- name: Generate coverage summary
3333
shell: pwsh
3434
run: |
3535
"## Test Coverage Summary" | Out-File -FilePath summary.md -Encoding utf8
3636
'```' | Out-File -FilePath summary.md -Append -Encoding utf8
3737
Get-Content pytest-coverage.txt | Out-File -FilePath summary.md -Append -Encoding utf8
3838
'```' | Out-File -FilePath summary.md -Append -Encoding utf8
39-
Get-Content summary.md | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append -Encoding utf8
39+
Get-Content summary.md | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Append -Encoding utf8

tests/conftest.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,29 @@
11
import pytest
22

3+
34
@pytest.fixture
45
def test_symbols():
56
"""Provides common test symbols that can be used across tests."""
6-
return {
7-
"forex": "EURUSD",
8-
"indices": "US500",
9-
"commodities": "XAUUSD",
10-
"crypto": "BTCUSD",
11-
"invalid": "INVALID"
12-
}
7+
return {"forex": "EURUSD", "indices": "US500", "commodities": "XAUUSD", "crypto": "BTCUSD", "invalid": "INVALID"}
8+
139

1410
@pytest.fixture
1511
def configure_logging():
1612
"""Sets up logging configuration for tests."""
1713
import logging
18-
14+
1915
root = logging.getLogger()
2016
for handler in root.handlers[:]:
2117
root.removeHandler(handler)
22-
18+
2319
handler = logging.StreamHandler()
2420
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
2521
handler.setFormatter(formatter)
2622
root.addHandler(handler)
27-
23+
2824
root.setLevel(logging.INFO)
29-
25+
3026
yield
31-
27+
3228
for handler in root.handlers[:]:
33-
root.removeHandler(handler)
29+
root.removeHandler(handler)

tests/integration/test_mt5_connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
3030
password=os.getenv("MT5_PASSWORD"),
3131
server=os.getenv("MT5_SERVER"),
3232
path=os.getenv("MT5_PATH"),
33-
):
33+
):
3434
logger.info("MT5 initialized successfully")
3535
mt5.shutdown()
3636
success = True
3737
break
38-
except Exception as e:
38+
except Exception:
3939
try:
4040
mt5.initialize()
41-
except Exception as e:
41+
except Exception:
4242
logger.info(f"Attempt {attempt+1}: Not ready yet, sleeping...")
4343
time.sleep(5)
4444

tests/test_book.py

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,133 @@
1-
import pytest
21
import logging
32
import time
43

5-
from mqpy.book import Book
64
import MetaTrader5 as Mt5
5+
import pytest
6+
7+
from mqpy.book import Book
78

89

910
@pytest.fixture(scope="module", autouse=True)
1011
def setup_teardown():
1112
"""Set up and tear down MetaTrader5 connection for the test module."""
1213
if not Mt5.initialize():
1314
pytest.skip("MetaTrader5 could not be initialized")
14-
15+
1516
time.sleep(5)
16-
17+
1718
yield
18-
19+
1920
Mt5.shutdown()
2021

22+
2123
@pytest.fixture
2224
def symbol():
2325
"""Provides a valid trading symbol for testing."""
2426
time.sleep(1)
25-
27+
2628
symbols = Mt5.symbols_get()
2729
if not symbols:
2830
pytest.skip("No symbols available for testing")
29-
31+
3032
for symbol in symbols:
3133
if symbol.name == "EURUSD":
3234
return "EURUSD"
33-
35+
3436
return symbols[0].name
3537

38+
3639
def test_book_initialization(symbol, caplog):
3740
"""Test initialization of Book with a real symbol."""
3841
caplog.set_level(logging.INFO)
3942
book = Book(symbol)
40-
43+
4144
assert f"The symbol {symbol} was successfully added to the market book" in caplog.text
4245

46+
4347
def test_book_get(symbol):
4448
"""Test getting real market book data."""
4549
book = Book(symbol)
46-
50+
4751
time.sleep(1)
48-
52+
4953
market_data = book.get()
50-
54+
5155
assert market_data is not None
52-
56+
5357
if market_data:
5458
assert isinstance(market_data, list)
55-
59+
5660
has_bids = any(item.type == Mt5.BOOK_TYPE_SELL for item in market_data)
5761
has_asks = any(item.type == Mt5.BOOK_TYPE_BUY for item in market_data)
58-
62+
5963
if not (has_bids or has_asks):
6064
print(f"Warning: No bids or asks found in market book for {symbol}")
61-
65+
6266
book.release()
6367

68+
6469
def test_book_release(symbol):
6570
"""Test releasing the market book."""
6671
book = Book(symbol)
67-
72+
6873
result = book.release()
69-
74+
7075
assert result is True
7176

77+
7278
def test_full_workflow(symbol):
7379
"""Test a complete workflow with the real market book."""
7480
book = Book(symbol)
75-
81+
7682
time.sleep(1)
77-
83+
7884
market_data = book.get()
79-
85+
8086
assert market_data is not None
81-
87+
8288
release_result = book.release()
8389
assert release_result is True
84-
8590

8691
time.sleep(1)
8792
data_after_release = book.get()
88-
93+
8994
if data_after_release is not None and len(data_after_release) > 0:
9095
print("Note: Market book data still available after release")
9196

97+
9298
def test_multiple_symbols():
9399
"""Test using Book with multiple symbols simultaneously."""
94100
symbols = Mt5.symbols_get()
95101
if len(symbols) < 2:
96102
pytest.skip("Need at least 2 symbols for this test")
97-
103+
98104
symbol1 = symbols[0].name
99105
symbol2 = symbols[1].name
100-
106+
101107
book1 = Book(symbol1)
102108
book2 = Book(symbol2)
103-
109+
104110
time.sleep(1)
105-
111+
106112
data1 = book1.get()
107113
data2 = book2.get()
108-
114+
109115
assert data1 is not None
110116
assert data2 is not None
111-
117+
112118
book1.release()
113119
book2.release()
114120

121+
115122
def test_unavailable_symbol(caplog):
116123
"""Test behavior with an unavailable symbol."""
117124
caplog.set_level(logging.ERROR)
118-
125+
119126
invalid_symbol = "INVALID_SYMBOL_THAT_DOESNT_EXIST"
120-
127+
121128
book = Book(invalid_symbol)
122-
129+
123130
assert "Error adding INVALID_SYMBOL_THAT_DOESNT_EXIST to the market book" in caplog.text
124-
131+
125132
release_result = book.release()
126-
assert release_result is False
133+
assert release_result is False

0 commit comments

Comments
 (0)