tvkit 0.18.2__tar.gz → 0.19.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tvkit-0.18.2/tvkit.egg-info → tvkit-0.19.1}/PKG-INFO +3 -2
- {tvkit-0.18.2 → tvkit-0.19.1}/pyproject.toml +10 -2
- tvkit-0.19.1/tests/test_auth_cache_cookies_extra.py +265 -0
- tvkit-0.19.1/tests/test_chart_ohlcv_models_extra.py +269 -0
- tvkit-0.19.1/tests/test_cli.py +455 -0
- tvkit-0.19.1/tests/test_connection_reader.py +208 -0
- tvkit-0.19.1/tests/test_data_exporter_paths.py +319 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_dependency_declarations.py +62 -1
- tvkit-0.19.1/tests/test_export_formatters.py +1110 -0
- tvkit-0.19.1/tests/test_fetch_robustness.py +179 -0
- tvkit-0.19.1/tests/test_helpers.py +363 -0
- tvkit-0.19.1/tests/test_helpers_fixes.py +101 -0
- tvkit-0.19.1/tests/test_indicator_search_encoding.py +49 -0
- tvkit-0.19.1/tests/test_indicator_service_http.py +503 -0
- tvkit-0.19.1/tests/test_message_service_and_probe.py +126 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_ohlcv_range_filter.py +12 -0
- tvkit-0.19.1/tests/test_ohlcv_streams.py +416 -0
- tvkit-0.19.1/tests/test_profile_parser.py +234 -0
- tvkit-0.19.1/tests/test_quickstart.py +519 -0
- tvkit-0.19.1/tests/test_scanner_request_models.py +333 -0
- tvkit-0.19.1/tests/test_scanner_response_shapes.py +83 -0
- tvkit-0.19.1/tests/test_scanner_service.py +554 -0
- tvkit-0.19.1/tests/test_symbol_validator.py +433 -0
- tvkit-0.19.1/tests/test_symbol_validator_2xx.py +72 -0
- tvkit-0.19.1/tests/test_timestamp_numeric_strings.py +73 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/__init__.py +1 -1
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/models/ohlcv.py +18 -3
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/ohlcv.py +32 -6
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/scanner/models/scanner.py +30 -6
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/scanner/services/scanner_service.py +4 -3
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/utils/indicator_service.py +4 -2
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/utils/symbol_validator.py +10 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/cookie_provider.py +3 -3
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/credentials.py +6 -5
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/formatters/base_formatter.py +25 -9
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/helpers.py +29 -11
- {tvkit-0.18.2 → tvkit-0.19.1/tvkit.egg-info}/PKG-INFO +3 -2
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit.egg-info/SOURCES.txt +21 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit.egg-info/requires.txt +2 -1
- {tvkit-0.18.2 → tvkit-0.19.1}/LICENSE +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/README.md +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/setup.cfg +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_adjustment_enum.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_authentication.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_batch_downloader.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_connection_retry.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_connection_service.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_credential_safety.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_docs_markdown.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_export_module.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_export_timestamp_format.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_export_validation.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_fundamentals_catalog.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_fundamentals_client.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_fundamentals_export.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_fundamentals_models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_fundamentals_transport.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_futures_range_degradation.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_historical_ohlcv.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_indicator_service.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_interval_validation.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_ohlcv_models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_ohlcv_reconnect.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_ohlcv_symbol_integration.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_range_end_bounds.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_range_segmentation.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_realtime_models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_retry_backoff.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_scanner_models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_scanner_row_drop.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_segmented_fetch.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_series_errors.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_symbols_config.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_symbols_normalizer.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_timezone_utils.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_utils.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_validation_checks.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tests/test_validation_composite.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/__main__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/exceptions.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/models/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/models/adjustment.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/models/realtime.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/models/stream_models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/services/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/services/connection_service.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/services/message_service.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/services/segmented_fetch_service.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/chart/utils.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/fundamentals/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/fundamentals/catalog.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/fundamentals/client.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/fundamentals/exceptions.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/fundamentals/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/fundamentals/transport.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/scanner/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/scanner/markets.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/scanner/models/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/scanner/services/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/utils/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/utils/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/utils/retry.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/api/utils/timestamp.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/auth_manager.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/capability_detector.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/exceptions.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/probe_cache.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/profile_parser.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/auth/token_provider.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/batch/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/batch/downloader.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/batch/exceptions.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/batch/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/cli.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/data_exporter.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/formatters/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/formatters/csv_formatter.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/formatters/json_formatter.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/formatters/polars_formatter.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/export/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/py.typed +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/quickstart.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/symbols/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/symbols/exceptions.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/symbols/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/symbols/normalizer.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/time/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/time/conversion.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/time/exchange.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/validation/__init__.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/validation/checks.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/validation/exceptions.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/validation/models.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit/validation/validator.py +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit.egg-info/dependency_links.txt +0 -0
- {tvkit-0.18.2 → tvkit-0.19.1}/tvkit.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tvkit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.19.1
|
|
4
4
|
Summary: tvkit is a Python library that fetches real-time stock data from TradingView, including price, market cap, P/E ratio, ROE, and more for stocks from multiple countries. Easily access and analyze financial metrics for global markets.
|
|
5
5
|
Author-email: lumduan <b@candythink.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -25,7 +25,8 @@ License-File: LICENSE
|
|
|
25
25
|
Requires-Dist: pydantic>=2.0.0
|
|
26
26
|
Requires-Dist: pydantic-settings>=2.15.0
|
|
27
27
|
Requires-Dist: websockets>=17.0.1
|
|
28
|
-
Requires-Dist: httpx>=0.
|
|
28
|
+
Requires-Dist: httpx>=0.25.1
|
|
29
|
+
Requires-Dist: h11>=0.16.0
|
|
29
30
|
Requires-Dist: polars>=1.44.1
|
|
30
31
|
Requires-Dist: browser-cookie3==0.20.1
|
|
31
32
|
Requires-Dist: pyyaml>=6.0.3
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "tvkit"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.19.1"
|
|
4
4
|
description = "tvkit is a Python library that fetches real-time stock data from TradingView, including price, market cap, P/E ratio, ROE, and more for stocks from multiple countries. Easily access and analyze financial metrics for global markets."
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.11"
|
|
@@ -24,7 +24,15 @@ dependencies = [
|
|
|
24
24
|
"pydantic>=2.0.0",
|
|
25
25
|
"pydantic-settings>=2.15.0",
|
|
26
26
|
"websockets>=17.0.1",
|
|
27
|
-
|
|
27
|
+
# httpx 0.25.1 is the oldest httpx that accepts httpcore 1.0.9, the first httpcore to allow a
|
|
28
|
+
# fixed h11 (below). Older httpx versions cap httpcore below 0.19, and so h11 below 0.15.
|
|
29
|
+
"httpx>=0.25.1",
|
|
30
|
+
# Security floor, NOT an import: tvkit never imports h11, but every install already has it
|
|
31
|
+
# (httpx -> httpcore -> h11). h11 < 0.16 is affected by PYSEC-2026-348 / CVE-2025-43859
|
|
32
|
+
# (lenient chunked-encoding parsing). Declaring the floor here makes installers pick a
|
|
33
|
+
# fixed h11, and makes an environment that pins an older httpcore/httpx/h11 keep resolving
|
|
34
|
+
# to tvkit 0.18.x instead of breaking. See SECURITY_FLOORS in tests/test_dependency_declarations.py.
|
|
35
|
+
"h11>=0.16.0",
|
|
28
36
|
"polars>=1.44.1",
|
|
29
37
|
# Exact pin, on purpose. browser-cookie3 reads and decrypts the user's browser cookie
|
|
30
38
|
# stores, and upstream has been dormant since 2024-12 (last release 0.20.1, 2025-01-04).
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
"""ProbeCache and CookieProvider paths not covered by test_authentication / test_credential_safety.
|
|
2
|
+
|
|
3
|
+
ProbeCache: a cache file that is valid JSON but not an object, entries for several users,
|
|
4
|
+
entries without a timestamp, write failures (the cache path is a directory; the temp-file
|
|
5
|
+
cleanup itself fails; serialisation fails mid-write) and a default directory that is already
|
|
6
|
+
private.
|
|
7
|
+
|
|
8
|
+
CookieProvider: the real lazy import of ``browser_cookie3`` (satisfied by a fake module placed in
|
|
9
|
+
``sys.modules`` — the real library is never called), the error when it is missing, cookies whose
|
|
10
|
+
domain is not a string, and invalidating a pair that was never cached.
|
|
11
|
+
|
|
12
|
+
No network and no sleeping: ages are produced by writing timestamps directly.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import logging
|
|
17
|
+
import os
|
|
18
|
+
import stat
|
|
19
|
+
import sys
|
|
20
|
+
import time
|
|
21
|
+
import types
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
from typing import Any
|
|
24
|
+
from unittest.mock import MagicMock, patch
|
|
25
|
+
|
|
26
|
+
import pytest
|
|
27
|
+
|
|
28
|
+
from tvkit.auth import BrowserCookieError, CookieProvider
|
|
29
|
+
from tvkit.auth import probe_cache as probe_cache_module
|
|
30
|
+
from tvkit.auth.cookie_provider import _get_browser_cookie3, _is_tradingview_cookie_domain
|
|
31
|
+
from tvkit.auth.probe_cache import ProbeCache
|
|
32
|
+
|
|
33
|
+
posix_only = pytest.mark.skipif(os.name == "nt", reason="POSIX permission bits")
|
|
34
|
+
|
|
35
|
+
_LOGGER = "tvkit.auth.probe_cache"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def _temp_files(directory: Path) -> list[str]:
|
|
39
|
+
return sorted(p.name for p in directory.iterdir() if p.name.endswith(".tmp"))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
# ---------------------------------------------------------------------------
|
|
43
|
+
# ProbeCache
|
|
44
|
+
# ---------------------------------------------------------------------------
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TestProbeCacheContents:
|
|
48
|
+
def test_entries_for_several_users_coexist(self, tmp_path: Path) -> None:
|
|
49
|
+
path = tmp_path / "caps.json"
|
|
50
|
+
cache = ProbeCache(path=path)
|
|
51
|
+
before = time.time()
|
|
52
|
+
|
|
53
|
+
cache.save(user_id=1, max_bars=5_000, plan="")
|
|
54
|
+
cache.save(user_id=2, max_bars=20_000, plan="pro_premium")
|
|
55
|
+
|
|
56
|
+
assert cache.load(user_id=1) == 5_000
|
|
57
|
+
assert cache.load(user_id=2) == 20_000
|
|
58
|
+
stored = json.loads(path.read_text(encoding="utf-8"))
|
|
59
|
+
assert sorted(stored) == ["1", "2"]
|
|
60
|
+
assert stored["2"]["plan"] == "pro_premium"
|
|
61
|
+
assert stored["2"]["max_bars"] == 20_000
|
|
62
|
+
assert before <= stored["2"]["timestamp"] <= time.time()
|
|
63
|
+
# No credential-shaped keys are ever written.
|
|
64
|
+
assert set(stored["2"]) == {"max_bars", "plan", "timestamp"}
|
|
65
|
+
|
|
66
|
+
def test_non_object_cache_file_is_a_miss_and_is_replaced_on_save(self, tmp_path: Path) -> None:
|
|
67
|
+
path = tmp_path / "caps.json"
|
|
68
|
+
path.write_text(json.dumps([1, 2, 3]), encoding="utf-8")
|
|
69
|
+
cache = ProbeCache(path=path)
|
|
70
|
+
|
|
71
|
+
assert cache.load(user_id=1) is None
|
|
72
|
+
|
|
73
|
+
cache.save(user_id=1, max_bars=10_000, plan="pro")
|
|
74
|
+
|
|
75
|
+
assert list(json.loads(path.read_text(encoding="utf-8"))) == ["1"]
|
|
76
|
+
assert cache.load(user_id=1) == 10_000
|
|
77
|
+
|
|
78
|
+
def test_entry_without_a_timestamp_is_treated_as_stale(self, tmp_path: Path) -> None:
|
|
79
|
+
path = tmp_path / "caps.json"
|
|
80
|
+
path.write_text(json.dumps({"1": {"max_bars": 5_000}}), encoding="utf-8")
|
|
81
|
+
|
|
82
|
+
assert ProbeCache(path=path).load(user_id=1) is None
|
|
83
|
+
|
|
84
|
+
def test_numeric_text_values_are_accepted(self, tmp_path: Path) -> None:
|
|
85
|
+
path = tmp_path / "caps.json"
|
|
86
|
+
entry = {"max_bars": "20000.0", "timestamp": str(time.time())}
|
|
87
|
+
path.write_text(json.dumps({"7": entry}), encoding="utf-8")
|
|
88
|
+
|
|
89
|
+
assert ProbeCache(path=path).load(user_id=7) == 20_000
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class TestProbeCacheWriteFailures:
|
|
93
|
+
def test_cache_path_that_is_a_directory_warns_and_leaves_no_temp_file(
|
|
94
|
+
self, tmp_path: Path, caplog: pytest.LogCaptureFixture
|
|
95
|
+
) -> None:
|
|
96
|
+
path = tmp_path / "caps.json"
|
|
97
|
+
path.mkdir()
|
|
98
|
+
cache = ProbeCache(path=path)
|
|
99
|
+
|
|
100
|
+
with caplog.at_level(logging.WARNING, logger=_LOGGER):
|
|
101
|
+
cache.save(user_id=1, max_bars=5_000, plan="") # must not raise
|
|
102
|
+
|
|
103
|
+
assert path.is_dir()
|
|
104
|
+
assert _temp_files(tmp_path) == []
|
|
105
|
+
assert "failed to read cache file" in caplog.text
|
|
106
|
+
assert "failed to write cache file" in caplog.text
|
|
107
|
+
assert cache.load(user_id=1) is None
|
|
108
|
+
|
|
109
|
+
def test_failed_cleanup_does_not_mask_the_write_error(
|
|
110
|
+
self, tmp_path: Path, caplog: pytest.LogCaptureFixture
|
|
111
|
+
) -> None:
|
|
112
|
+
path = tmp_path / "caps.json"
|
|
113
|
+
cache = ProbeCache(path=path)
|
|
114
|
+
|
|
115
|
+
with (
|
|
116
|
+
patch.object(probe_cache_module.os, "replace", side_effect=OSError("disk full")),
|
|
117
|
+
patch.object(probe_cache_module.os, "unlink", side_effect=OSError("busy")) as unlink,
|
|
118
|
+
caplog.at_level(logging.WARNING, logger=_LOGGER),
|
|
119
|
+
):
|
|
120
|
+
cache.save(user_id=1, max_bars=5_000, plan="") # must not raise
|
|
121
|
+
|
|
122
|
+
unlink.assert_called_once()
|
|
123
|
+
assert "disk full" in caplog.text
|
|
124
|
+
assert not path.exists()
|
|
125
|
+
|
|
126
|
+
def test_serialisation_error_keeps_the_previous_cache_intact(self, tmp_path: Path) -> None:
|
|
127
|
+
path = tmp_path / "caps.json"
|
|
128
|
+
cache = ProbeCache(path=path)
|
|
129
|
+
cache.save(user_id=1, max_bars=5_000, plan="")
|
|
130
|
+
original = path.read_bytes()
|
|
131
|
+
|
|
132
|
+
with pytest.raises(TypeError):
|
|
133
|
+
cache.save(user_id=2, max_bars=1, plan={"not", "json"}) # type: ignore[arg-type]
|
|
134
|
+
|
|
135
|
+
assert path.read_bytes() == original
|
|
136
|
+
assert _temp_files(tmp_path) == []
|
|
137
|
+
assert cache.load(user_id=1) == 5_000
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@posix_only
|
|
141
|
+
class TestProbeCacheDefaultDirectory:
|
|
142
|
+
def test_an_already_private_default_dir_is_left_as_is(
|
|
143
|
+
self,
|
|
144
|
+
tmp_path: Path,
|
|
145
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
146
|
+
caplog: pytest.LogCaptureFixture,
|
|
147
|
+
) -> None:
|
|
148
|
+
default = tmp_path / "home" / ".cache" / "tvkit" / "capabilities.json"
|
|
149
|
+
default.parent.mkdir(parents=True)
|
|
150
|
+
default.parent.chmod(0o700)
|
|
151
|
+
monkeypatch.setattr(probe_cache_module, "_CACHE_PATH", default)
|
|
152
|
+
|
|
153
|
+
with caplog.at_level(logging.INFO, logger=_LOGGER):
|
|
154
|
+
ProbeCache().save(user_id=1, max_bars=5_000, plan="")
|
|
155
|
+
|
|
156
|
+
assert default.is_file()
|
|
157
|
+
assert stat.S_IMODE(default.parent.stat().st_mode) == 0o700
|
|
158
|
+
assert "tightened" not in caplog.text
|
|
159
|
+
|
|
160
|
+
def test_the_default_path_is_used_when_none_is_given(
|
|
161
|
+
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
162
|
+
) -> None:
|
|
163
|
+
default = tmp_path / "home" / ".cache" / "tvkit" / "capabilities.json"
|
|
164
|
+
monkeypatch.setattr(probe_cache_module, "_CACHE_PATH", default)
|
|
165
|
+
|
|
166
|
+
ProbeCache(path=None).save(user_id=3, max_bars=10_000, plan="pro")
|
|
167
|
+
|
|
168
|
+
assert json.loads(default.read_text(encoding="utf-8"))["3"]["max_bars"] == 10_000
|
|
169
|
+
assert stat.S_IMODE(default.parent.stat().st_mode) == 0o700
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
# ---------------------------------------------------------------------------
|
|
173
|
+
# CookieProvider
|
|
174
|
+
# ---------------------------------------------------------------------------
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _cookie(name: str, value: str, domain: Any) -> MagicMock:
|
|
178
|
+
c = MagicMock()
|
|
179
|
+
c.name, c.value, c.domain = name, value, domain
|
|
180
|
+
return c
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class TestBrowserCookie3Import:
|
|
184
|
+
def test_returns_the_installed_module(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
185
|
+
fake = types.ModuleType("browser_cookie3")
|
|
186
|
+
fake.chrome = MagicMock( # type: ignore[attr-defined]
|
|
187
|
+
return_value=[_cookie("sessionid", "S", ".tradingview.com")]
|
|
188
|
+
)
|
|
189
|
+
monkeypatch.setitem(sys.modules, "browser_cookie3", fake)
|
|
190
|
+
|
|
191
|
+
assert _get_browser_cookie3() is fake
|
|
192
|
+
# The provider goes through the same import, with no patching of the helper.
|
|
193
|
+
assert CookieProvider().extract("chrome") == {"sessionid": "S"}
|
|
194
|
+
fake.chrome.assert_called_once_with(domain_name="tradingview.com") # type: ignore[attr-defined]
|
|
195
|
+
|
|
196
|
+
def test_missing_library_raises_a_clear_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
197
|
+
monkeypatch.setitem(sys.modules, "browser_cookie3", None) # makes the import fail
|
|
198
|
+
|
|
199
|
+
with pytest.raises(BrowserCookieError, match="uv add browser-cookie3") as excinfo:
|
|
200
|
+
_get_browser_cookie3()
|
|
201
|
+
|
|
202
|
+
# Raised "from None": the ImportError is not chained into the user-facing error.
|
|
203
|
+
assert excinfo.value.__cause__ is None
|
|
204
|
+
assert excinfo.value.__suppress_context__ is True
|
|
205
|
+
|
|
206
|
+
def test_extract_surfaces_the_missing_library_error(
|
|
207
|
+
self, monkeypatch: pytest.MonkeyPatch
|
|
208
|
+
) -> None:
|
|
209
|
+
monkeypatch.setitem(sys.modules, "browser_cookie3", None)
|
|
210
|
+
provider = CookieProvider()
|
|
211
|
+
|
|
212
|
+
with pytest.raises(BrowserCookieError, match="browser_cookie3 is required"):
|
|
213
|
+
provider.extract("firefox")
|
|
214
|
+
|
|
215
|
+
assert provider._cache == {} # a failure is never cached
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
class TestCookieDomainFilter:
|
|
219
|
+
@pytest.mark.parametrize(
|
|
220
|
+
("domain", "expected"),
|
|
221
|
+
[
|
|
222
|
+
("tradingview.com", True),
|
|
223
|
+
(".tradingview.com", True),
|
|
224
|
+
("WWW.TradingView.COM", True),
|
|
225
|
+
("tradingview.com.attacker.example", False),
|
|
226
|
+
("nottradingview.com", False),
|
|
227
|
+
("", False),
|
|
228
|
+
(None, False),
|
|
229
|
+
(b"tradingview.com", False),
|
|
230
|
+
(123, False),
|
|
231
|
+
],
|
|
232
|
+
)
|
|
233
|
+
def test_domain_predicate(self, domain: object, expected: bool) -> None:
|
|
234
|
+
assert _is_tradingview_cookie_domain(domain) is expected
|
|
235
|
+
|
|
236
|
+
def test_cookies_with_a_non_string_domain_are_dropped(self) -> None:
|
|
237
|
+
bc3 = MagicMock()
|
|
238
|
+
bc3.chrome.return_value = [
|
|
239
|
+
_cookie("sessionid", "REAL", ".tradingview.com"),
|
|
240
|
+
_cookie("csrftoken", "NO-DOMAIN", None),
|
|
241
|
+
_cookie("device_t", "BYTES", b"tradingview.com"),
|
|
242
|
+
_cookie("tv_ecuid", "UPPER", "WWW.TRADINGVIEW.COM"),
|
|
243
|
+
]
|
|
244
|
+
|
|
245
|
+
with patch("tvkit.auth.cookie_provider._get_browser_cookie3", return_value=bc3):
|
|
246
|
+
cookies = CookieProvider().extract("chrome")
|
|
247
|
+
|
|
248
|
+
assert cookies == {"sessionid": "REAL", "tv_ecuid": "UPPER"}
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
class TestCookieCacheInvalidation:
|
|
252
|
+
def test_invalidating_a_pair_that_was_never_cached_is_a_no_op(self) -> None:
|
|
253
|
+
bc3 = MagicMock()
|
|
254
|
+
bc3.chrome.return_value = [_cookie("sessionid", "S", ".tradingview.com")]
|
|
255
|
+
|
|
256
|
+
with patch("tvkit.auth.cookie_provider._get_browser_cookie3", return_value=bc3):
|
|
257
|
+
provider = CookieProvider()
|
|
258
|
+
provider.extract("chrome")
|
|
259
|
+
provider.invalidate_cache("firefox")
|
|
260
|
+
provider.invalidate_cache("chrome", profile="Profile 9")
|
|
261
|
+
provider.extract("chrome")
|
|
262
|
+
|
|
263
|
+
# The cached chrome/default entry survived both no-op invalidations.
|
|
264
|
+
assert bc3.chrome.call_count == 1
|
|
265
|
+
assert list(provider._cache) == [("chrome", None)]
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"""Frame-parsing tests for the chart WebSocket models not covered by test_ohlcv_models.py.
|
|
2
|
+
|
|
3
|
+
``tests/test_ohlcv_models.py`` covers ``OHLCVBar``, ``SeriesData``, ``NamespaceData``,
|
|
4
|
+
``LastBarStatus`` and ``OHLCVResponse``. This module covers ``TimescaleUpdateResponse`` (the
|
|
5
|
+
historical-bars frame and its skip-malformed-bars behaviour), ``QuoteSymbolData``,
|
|
6
|
+
``QuoteCompletedMessage`` and ``WebSocketMessage``. The frames mirror TradingView's wire shape;
|
|
7
|
+
no network is used.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import pytest
|
|
13
|
+
from pydantic import ValidationError
|
|
14
|
+
|
|
15
|
+
from tvkit.api.chart.models.ohlcv import (
|
|
16
|
+
OHLCVBar,
|
|
17
|
+
OHLCVResponse,
|
|
18
|
+
QuoteCompletedMessage,
|
|
19
|
+
QuoteSymbolData,
|
|
20
|
+
TimescaleUpdateResponse,
|
|
21
|
+
WebSocketMessage,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
TS: float = 1_753_692_060.0
|
|
25
|
+
GOOD_6: list[float] = [TS, 100.0, 110.0, 90.0, 105.0, 12.5]
|
|
26
|
+
GOOD_5: list[float] = [TS + 60, 105.0, 111.0, 104.0, 108.0]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _timescale(series: dict[str, Any], **extra: Any) -> dict[str, Any]:
|
|
30
|
+
return {"m": "timescale_update", "p": ["cs_abc123", series], **extra}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# ---------------------------------------------------------------------------
|
|
34
|
+
# TimescaleUpdateResponse
|
|
35
|
+
# ---------------------------------------------------------------------------
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class TestTimescaleUpdateResponse:
|
|
39
|
+
def test_extracts_bars_from_a_realistic_frame(self) -> None:
|
|
40
|
+
frame = _timescale(
|
|
41
|
+
{
|
|
42
|
+
"sds_1": {
|
|
43
|
+
"s": [{"i": 0, "v": GOOD_6}, {"i": 1, "v": GOOD_5}],
|
|
44
|
+
"ns": {"d": "", "indexes": []},
|
|
45
|
+
"t": "s1",
|
|
46
|
+
"lbs": {"bar_close_time": TS + 120},
|
|
47
|
+
},
|
|
48
|
+
# Non-series siblings that TradingView sends in the same dict.
|
|
49
|
+
"index": {"index": [0, 1], "zoffset": 0},
|
|
50
|
+
"marks": [],
|
|
51
|
+
"note": "text",
|
|
52
|
+
},
|
|
53
|
+
t_ms=1_753_692_100_000,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
response = TimescaleUpdateResponse.model_validate(frame)
|
|
57
|
+
|
|
58
|
+
assert response.message_type == "timescale_update"
|
|
59
|
+
assert response.session_id == "cs_abc123"
|
|
60
|
+
assert response.model_extra == {"t_ms": 1_753_692_100_000}
|
|
61
|
+
assert response.ohlcv_bars == [
|
|
62
|
+
OHLCVBar(timestamp=TS, open=100.0, high=110.0, low=90.0, close=105.0, volume=12.5),
|
|
63
|
+
# A five-element bar has no volume; it defaults to 0.0.
|
|
64
|
+
OHLCVBar(timestamp=TS + 60, open=105.0, high=111.0, low=104.0, close=108.0, volume=0.0),
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
def test_bars_from_several_series_are_concatenated(self) -> None:
|
|
68
|
+
frame = _timescale(
|
|
69
|
+
{"sds_1": {"s": [{"i": 0, "v": GOOD_6}]}, "sds_2": {"s": [{"v": GOOD_5}]}}
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
bars = TimescaleUpdateResponse.model_validate(frame).ohlcv_bars
|
|
73
|
+
|
|
74
|
+
assert [b.timestamp for b in bars] == [TS, TS + 60]
|
|
75
|
+
|
|
76
|
+
def test_malformed_items_are_skipped_and_good_ones_kept(self) -> None:
|
|
77
|
+
frame = _timescale(
|
|
78
|
+
{
|
|
79
|
+
"sds_1": {
|
|
80
|
+
"s": [
|
|
81
|
+
"not-a-dict",
|
|
82
|
+
{"i": 1}, # no values
|
|
83
|
+
{"i": 2, "v": "not-a-list"},
|
|
84
|
+
{"i": 3, "v": [TS, 1.0, 2.0, 3.0]}, # too short
|
|
85
|
+
{"i": 4, "v": [TS, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]}, # too long
|
|
86
|
+
{"i": 5, "v": [-1.0, 1.0, 2.0, 0.5, 1.5, 9.0]}, # negative timestamp
|
|
87
|
+
{"i": 6, "v": GOOD_6},
|
|
88
|
+
]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
bars = TimescaleUpdateResponse.model_validate(frame).ohlcv_bars
|
|
94
|
+
|
|
95
|
+
assert [(b.timestamp, b.close) for b in bars] == [(TS, 105.0)]
|
|
96
|
+
|
|
97
|
+
def test_a_frame_with_only_a_session_id_has_no_bars(self) -> None:
|
|
98
|
+
response = TimescaleUpdateResponse.model_validate({"m": "timescale_update", "p": ["cs_1"]})
|
|
99
|
+
|
|
100
|
+
assert response.session_id == "cs_1"
|
|
101
|
+
assert response.ohlcv_bars == []
|
|
102
|
+
|
|
103
|
+
def test_series_without_bars_yield_nothing(self) -> None:
|
|
104
|
+
frame = _timescale({"sds_1": {"s": []}, "sds_2": {"ns": {"d": ""}}})
|
|
105
|
+
|
|
106
|
+
assert TimescaleUpdateResponse.model_validate(frame).ohlcv_bars == []
|
|
107
|
+
|
|
108
|
+
def test_other_message_types_are_rejected(self) -> None:
|
|
109
|
+
with pytest.raises(ValidationError, match="Expected message type 'timescale_update'"):
|
|
110
|
+
TimescaleUpdateResponse.model_validate({"m": "du", "p": ["cs_1", {}]})
|
|
111
|
+
|
|
112
|
+
def test_computed_fields_are_serialised(self) -> None:
|
|
113
|
+
response = TimescaleUpdateResponse.model_validate(
|
|
114
|
+
_timescale({"sds_1": {"s": [{"v": GOOD_6}]}})
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
dumped = response.model_dump()
|
|
118
|
+
|
|
119
|
+
assert dumped["session_id"] == "cs_abc123"
|
|
120
|
+
assert dumped["ohlcv_bars"][0]["close"] == 105.0
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
class TestOHLCVResponseAcceptsTimescaleFrames:
|
|
124
|
+
def test_timescale_update_is_an_accepted_message_type(self) -> None:
|
|
125
|
+
frame = {
|
|
126
|
+
"m": "timescale_update",
|
|
127
|
+
"p": [
|
|
128
|
+
"cs_1",
|
|
129
|
+
{
|
|
130
|
+
"sds_1": {
|
|
131
|
+
"s": [{"i": 0, "v": GOOD_6}],
|
|
132
|
+
"ns": {"d": ""},
|
|
133
|
+
"t": "s1",
|
|
134
|
+
"lbs": {"bar_close_time": TS + 60},
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
"t_ms": 5,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
response = OHLCVResponse.model_validate(frame)
|
|
142
|
+
|
|
143
|
+
assert response.message_type == "timescale_update"
|
|
144
|
+
assert response.model_extra == {"t_ms": 5}
|
|
145
|
+
assert [b.close for b in response.ohlcv_bars] == [105.0]
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
# ---------------------------------------------------------------------------
|
|
149
|
+
# QuoteSymbolData ('qsd')
|
|
150
|
+
# ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
class TestQuoteSymbolData:
|
|
154
|
+
def test_realistic_frame(self) -> None:
|
|
155
|
+
frame = {
|
|
156
|
+
"m": "qsd",
|
|
157
|
+
"p": [
|
|
158
|
+
"qs_session",
|
|
159
|
+
{"n": "NASDAQ:AAPL", "s": "ok", "v": {"lp": 189.5, "ch": 1.25, "currency": "USD"}},
|
|
160
|
+
],
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
quote = QuoteSymbolData.model_validate(frame)
|
|
164
|
+
|
|
165
|
+
assert quote.session_id == "qs_session"
|
|
166
|
+
assert quote.quote_data["n"] == "NASDAQ:AAPL"
|
|
167
|
+
assert quote.current_price == 189.5
|
|
168
|
+
assert quote.symbol_info == {"lp": 189.5, "ch": 1.25, "currency": "USD"}
|
|
169
|
+
|
|
170
|
+
def test_values_without_a_last_price(self) -> None:
|
|
171
|
+
quote = QuoteSymbolData.model_validate(
|
|
172
|
+
{"m": "qsd", "p": ["qs", {"n": "SET:PTT", "s": "ok", "v": {"ch": -0.25}}]}
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
assert quote.current_price is None
|
|
176
|
+
assert quote.symbol_info == {"ch": -0.25}
|
|
177
|
+
|
|
178
|
+
@pytest.mark.parametrize(
|
|
179
|
+
"payload",
|
|
180
|
+
[
|
|
181
|
+
{"n": "SET:PTT", "s": "error"}, # no "v" block at all
|
|
182
|
+
{"n": "SET:PTT", "s": "ok", "v": ["not", "a", "dict"]},
|
|
183
|
+
],
|
|
184
|
+
)
|
|
185
|
+
def test_missing_or_non_dict_values_block(self, payload: dict[str, Any]) -> None:
|
|
186
|
+
quote = QuoteSymbolData.model_validate({"m": "qsd", "p": ["qs", payload]})
|
|
187
|
+
|
|
188
|
+
assert quote.quote_data == payload
|
|
189
|
+
assert quote.current_price is None
|
|
190
|
+
assert quote.symbol_info == {}
|
|
191
|
+
|
|
192
|
+
@pytest.mark.parametrize("params", [["qs"], ["qs", "not-a-dict"], ["qs", ["list"]]])
|
|
193
|
+
def test_missing_or_non_dict_quote_payload(self, params: list[Any]) -> None:
|
|
194
|
+
quote = QuoteSymbolData.model_validate({"m": "qsd", "p": params})
|
|
195
|
+
|
|
196
|
+
assert quote.session_id == "qs"
|
|
197
|
+
assert quote.quote_data == {}
|
|
198
|
+
assert quote.current_price is None
|
|
199
|
+
assert quote.symbol_info == {}
|
|
200
|
+
|
|
201
|
+
def test_empty_parameters(self) -> None:
|
|
202
|
+
quote = QuoteSymbolData.model_validate({"m": "qsd", "p": []})
|
|
203
|
+
|
|
204
|
+
assert quote.session_id == ""
|
|
205
|
+
assert quote.quote_data == {}
|
|
206
|
+
|
|
207
|
+
def test_other_message_types_are_rejected(self) -> None:
|
|
208
|
+
with pytest.raises(ValidationError, match="Expected message type 'qsd'"):
|
|
209
|
+
QuoteSymbolData.model_validate({"m": "du", "p": ["qs", {}]})
|
|
210
|
+
|
|
211
|
+
def test_unknown_top_level_keys_are_rejected(self) -> None:
|
|
212
|
+
with pytest.raises(ValidationError, match="Extra inputs are not permitted"):
|
|
213
|
+
QuoteSymbolData.model_validate({"m": "qsd", "p": ["qs", {}], "t_ms": 1})
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
# ---------------------------------------------------------------------------
|
|
217
|
+
# QuoteCompletedMessage
|
|
218
|
+
# ---------------------------------------------------------------------------
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class TestQuoteCompletedMessage:
|
|
222
|
+
def test_realistic_frame(self) -> None:
|
|
223
|
+
msg = QuoteCompletedMessage.model_validate(
|
|
224
|
+
{"m": "quote_completed", "p": ["qs_session", "NASDAQ:AAPL"]}
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
assert msg.session_id == "qs_session"
|
|
228
|
+
assert msg.symbol == "NASDAQ:AAPL"
|
|
229
|
+
assert msg.model_dump()["symbol"] == "NASDAQ:AAPL"
|
|
230
|
+
|
|
231
|
+
def test_missing_symbol(self) -> None:
|
|
232
|
+
msg = QuoteCompletedMessage.model_validate({"m": "quote_completed", "p": ["qs_session"]})
|
|
233
|
+
|
|
234
|
+
assert msg.session_id == "qs_session"
|
|
235
|
+
assert msg.symbol == ""
|
|
236
|
+
|
|
237
|
+
def test_empty_parameters(self) -> None:
|
|
238
|
+
msg = QuoteCompletedMessage.model_validate({"m": "quote_completed", "p": []})
|
|
239
|
+
|
|
240
|
+
assert msg.session_id == ""
|
|
241
|
+
assert msg.symbol == ""
|
|
242
|
+
|
|
243
|
+
def test_other_message_types_are_rejected(self) -> None:
|
|
244
|
+
with pytest.raises(ValidationError, match="Expected message type 'quote_completed'"):
|
|
245
|
+
QuoteCompletedMessage.model_validate({"m": "qsd", "p": ["qs", "X"]})
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
# ---------------------------------------------------------------------------
|
|
249
|
+
# WebSocketMessage
|
|
250
|
+
# ---------------------------------------------------------------------------
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
class TestWebSocketMessage:
|
|
254
|
+
def test_any_message_type_is_accepted_and_extras_kept(self) -> None:
|
|
255
|
+
msg = WebSocketMessage.model_validate(
|
|
256
|
+
{"m": "protocol_error", "p": [42, "wrong data"], "t_ms": 7}
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
assert msg.message_type == "protocol_error"
|
|
260
|
+
assert msg.parameters == [42, "wrong data"]
|
|
261
|
+
assert msg.session_id == "42"
|
|
262
|
+
assert msg.model_extra == {"t_ms": 7}
|
|
263
|
+
|
|
264
|
+
def test_empty_parameters_have_no_session(self) -> None:
|
|
265
|
+
assert WebSocketMessage.model_validate({"m": "series_loading", "p": []}).session_id == ""
|
|
266
|
+
|
|
267
|
+
def test_parameters_are_required(self) -> None:
|
|
268
|
+
with pytest.raises(ValidationError):
|
|
269
|
+
WebSocketMessage.model_validate({"m": "du"})
|