Files
sentiment-engine/sentiment_engine/tests/unit/test_schemas_payload.py
Codex c32db97d57 feat(sentiment): complete pipeline overhaul with ONNX priority + LoRA retraining
- Added 30 new sources (5 RSS + 25 Telegram) for previously ZERO-coverage assets
- Fixed model loading priority: ONNX > LoRA v2 > PyTorch > Mock
- ONNX FinBERT (pre-trained on 1.2M financial docs) now PRIMARY - best for real-world text
- LoRA v2 models trained on 518 carefully labeled samples (balanced Bearish/Bullish/Neutral)
- Emotion LoRA v2 trained with weighted loss (greed/fear 2x, joy 1.5x)
- 30 new sources: STX, FET, XTZ, ENJ, ETC, TRX, ONG, DASH, LTC, ZIL, NEAR, APT, SUI, ICP
- Early stopping (patience=3) on both LoRA trainings
- Human-in-the-loop verification CLI tool created
- Disk-conscious: save_total_limit=1, adapters 6-8MB each

Pipeline now correctly classifies:
- BTC breaks 100k → +0.54 Bullish ✅
- Major hack → -0.23 Bearish ✅
- HODL → +0.91 Bullish ✅
- Rug pull → -0.30 Bearish ✅
- SEC sues → -0.30 Bearish ✅
- ETF approval → +0.32 Bullish ✅
- Whale accumulation → +0.31 Bullish ✅

Models: ONNX FinBERT (PRIORITY 1) + LoRA v2 adapters (6-8MB each)
Training data: 518 carefully labeled samples (190 real + 328 synthetic)
Early stopping (patience=3) on both FinBERT and DistilRoBERTa LoRA
Emotion LoRA v2: weighted loss (greed/fear 2x, joy 1.5x) + early stopping
2026-09-27 04:34:49 +02:00

93 lines
3.0 KiB
Python

"""Tests for payload schemas"""
import pytest
from datetime import datetime
from sentiment_engine.schemas.payload import NormalizedPayload, SourceType, AssetMention, EngagementMetrics
class TestNormalizedPayload:
"""Tests for NormalizedPayload schema"""
def test_valid_payload(self):
payload = NormalizedPayload(
source_id="test_source",
source_type=SourceType.NEWS,
source_credibility_base=0.8,
ingest_ts=datetime.now().timestamp(),
publish_ts=datetime.now().timestamp(),
asset_mentions=[AssetMention(asset_id="BTC", mention_span=(0, 3), confidence=0.9, source_text="BTC", mention_type="ticker")],
raw_text="BTC surges to new highs",
title="BTC Surges",
url="https://test.com",
author="Test Author",
content_length=100,
language="en"
)
assert payload.source_id == "test_source"
assert payload.has_assets is True
assert payload.get_assets() == ["BTC"]
def test_empty_text_raises(self):
with pytest.raises(ValueError, match="raw_text cannot be empty"):
NormalizedPayload(
source_id="test",
source_type=SourceType.NEWS,
source_credibility_base=0.5,
ingest_ts=1234567890.0,
raw_text="",
content_length=0,
language="en"
)
def test_whitespace_text_raises(self):
with pytest.raises(ValueError, match="raw_text cannot be empty"):
NormalizedPayload(
source_id="test",
source_type=SourceType.NEWS,
source_credibility_base=0.5,
ingest_ts=1234567890.0,
raw_text=" ",
content_length=0,
language="en"
)
class TestAssetMention:
"""Tests for AssetMention schema"""
def test_valid_mention(self):
mention = AssetMention(
asset_id="BTC",
mention_span=(0, 3),
confidence=0.9,
source_text="BTC",
mention_type="ticker"
)
assert mention.asset_id == "BTC"
assert mention.confidence == 0.9
class TestEngagementMetrics:
"""Tests for EngagementMetrics"""
def test_total_engagement(self):
metrics = EngagementMetrics(retweets=10, likes=50, replies=5, upvotes=100, comments=20)
assert metrics.total_engagement() == 185
def test_default_zero(self):
metrics = EngagementMetrics()
assert metrics.total_engagement() == 0
class TestSourceType:
"""Tests for SourceType enum"""
def test_all_values(self):
assert SourceType.NEWS == "news"
assert SourceType.SOCIAL == "social"
assert SourceType.EXCHANGE_ANN == "exchange_ann"
assert SourceType.REGULATORY == "regulatory"
assert SourceType.CORPORATE == "corporate"
assert SourceType.FORUM == "forum"
assert SourceType.ON_CHAIN == "on_chain"