-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_utils.py
More file actions
73 lines (57 loc) · 2.67 KB
/
test_utils.py
File metadata and controls
73 lines (57 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Tests for utility functions."""
import pytest
from tradingview_mcp.utils.asset_detector import detect_asset_type, format_pair_for_alpha_vantage
from tradingview_mcp.utils.formatters import format_error_response, safe_float, round_price
class TestAssetDetector:
"""Tests for asset type detection."""
def test_detect_forex(self):
"""Test forex detection."""
assert detect_asset_type("EURUSD") == ('forex', 'EURUSD')
assert detect_asset_type("EUR/USD") == ('forex', 'EURUSD')
assert detect_asset_type("XAUUSD") == ('forex', 'XAUUSD')
def test_detect_crypto(self):
"""Test crypto detection."""
assert detect_asset_type("BTC") == ('crypto', 'BTC')
assert detect_asset_type("ETH") == ('crypto', 'ETH')
assert detect_asset_type("BTCUSD") == ('crypto', 'BTC')
def test_detect_stock(self):
"""Test stock detection."""
assert detect_asset_type("AAPL") == ('stock', 'AAPL')
assert detect_asset_type("MSFT") == ('stock', 'MSFT')
assert detect_asset_type("GOOGL") == ('stock', 'GOOGL')
def test_format_forex_pair(self):
"""Test forex pair formatting."""
assert format_pair_for_alpha_vantage("EURUSD") == ('EUR', 'USD')
assert format_pair_for_alpha_vantage("GBPJPY") == ('GBP', 'JPY')
assert format_pair_for_alpha_vantage("XAUUSD") == ('XAU', 'USD')
def test_format_invalid_pair(self):
"""Test that invalid pairs raise ValueError."""
with pytest.raises(ValueError):
format_pair_for_alpha_vantage("INVALID")
class TestFormatters:
"""Tests for formatting utilities."""
def test_format_error_response(self):
"""Test error response formatting."""
error = format_error_response("Test error", symbol="AAPL")
assert error["error"] == "Test error"
assert error["symbol"] == "AAPL"
assert error["success"] is False
def test_format_error_with_suggestion(self):
"""Test error response with suggestion."""
error = format_error_response(
"Rate limit reached",
symbol="EURUSD",
suggestion="Wait 1 minute"
)
assert error["suggestion"] == "Wait 1 minute"
def test_safe_float(self):
"""Test safe float conversion."""
assert safe_float("123.45") == 123.45
assert safe_float("invalid", default=0.0) == 0.0
assert safe_float(None, default=1.0) == 1.0
assert safe_float(123) == 123.0
def test_round_price(self):
"""Test price rounding."""
assert round_price(1.23456789, 5) == 1.23457
assert round_price(1.23456789, 2) == 1.23
assert round_price(100.999, 0) == 101.0