1+ """Tests for the Book class that manages market depth information from MetaTrader 5."""
2+
3+ from __future__ import annotations
4+
15import logging
26import time
7+ from typing import TYPE_CHECKING , Generator
38
49import MetaTrader5 as Mt5
510import pytest
611
12+ if TYPE_CHECKING :
13+ from _pytest .logging import LogCaptureFixture
14+
715from mqpy .book import Book
816
917
1018@pytest .fixture (scope = "module" , autouse = True )
11- def setup_teardown ():
19+ def setup_teardown () -> Generator [ None , None , None ] :
1220 """Set up and tear down MetaTrader5 connection for the test module."""
1321 if not Mt5 .initialize ():
1422 pytest .skip ("MetaTrader5 could not be initialized" )
@@ -21,7 +29,7 @@ def setup_teardown():
2129
2230
2331@pytest .fixture
24- def symbol ():
32+ def symbol () -> str :
2533 """Provides a valid trading symbol for testing."""
2634 time .sleep (1 )
2735
@@ -36,15 +44,16 @@ def symbol():
3644 return symbols [0 ].name
3745
3846
39- def test_book_initialization (symbol , caplog ) :
47+ def test_book_initialization (symbol : str , caplog : LogCaptureFixture ) -> None :
4048 """Test initialization of Book with a real symbol."""
4149 caplog .set_level (logging .INFO )
42- book = Book (symbol )
50+ # Create book instance (used to trigger log message)
51+ Book (symbol )
4352
4453 assert f"The symbol { symbol } was successfully added to the market book" in caplog .text
4554
4655
47- def test_book_get (symbol ) :
56+ def test_book_get (symbol : str ) -> None :
4857 """Test getting real market book data."""
4958 book = Book (symbol )
5059
@@ -57,16 +66,23 @@ def test_book_get(symbol):
5766 if market_data :
5867 assert isinstance (market_data , list )
5968
60- has_bids = any (item .type == Mt5 .BOOK_TYPE_SELL for item in market_data )
61- has_asks = any (item .type == Mt5 .BOOK_TYPE_BUY for item in market_data )
69+ # Loop separately to check for bids and asks
70+ has_bids = False
71+ has_asks = False
72+
73+ for item in market_data :
74+ if item .type == Mt5 .BOOK_TYPE_SELL :
75+ has_bids = True
76+ if item .type == Mt5 .BOOK_TYPE_BUY :
77+ has_asks = True
6278
6379 if not (has_bids or has_asks ):
64- print (f"Warning: No bids or asks found in market book for { symbol } " )
80+ logging . warning (f"No bids or asks found in market book for { symbol } " )
6581
6682 book .release ()
6783
6884
69- def test_book_release (symbol ) :
85+ def test_book_release (symbol : str ) -> None :
7086 """Test releasing the market book."""
7187 book = Book (symbol )
7288
@@ -75,7 +91,7 @@ def test_book_release(symbol):
7591 assert result is True
7692
7793
78- def test_full_workflow (symbol ) :
94+ def test_full_workflow (symbol : str ) -> None :
7995 """Test a complete workflow with the real market book."""
8096 book = Book (symbol )
8197
@@ -92,10 +108,10 @@ def test_full_workflow(symbol):
92108 data_after_release = book .get ()
93109
94110 if data_after_release is not None and len (data_after_release ) > 0 :
95- print ( "Note: Market book data still available after release" )
111+ logging . info ( " Market book data still available after release" )
96112
97113
98- def test_multiple_symbols ():
114+ def test_multiple_symbols () -> None :
99115 """Test using Book with multiple symbols simultaneously."""
100116 symbols = Mt5 .symbols_get ()
101117 if len (symbols ) < 2 :
@@ -119,7 +135,7 @@ def test_multiple_symbols():
119135 book2 .release ()
120136
121137
122- def test_unavailable_symbol (caplog ) :
138+ def test_unavailable_symbol (caplog : LogCaptureFixture ) -> None :
123139 """Test behavior with an unavailable symbol."""
124140 caplog .set_level (logging .ERROR )
125141
0 commit comments