1- import pytest
21import logging
32import time
43
5- from mqpy .book import Book
64import MetaTrader5 as Mt5
5+ import pytest
6+
7+ from mqpy .book import Book
78
89
910@pytest .fixture (scope = "module" , autouse = True )
1011def 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
2224def 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+
3639def 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+
4347def 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+
6469def 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+
7278def 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+
9298def 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+
115122def 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