Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

test: harden optional market sentiment snapshot handling

Alex Schneider6631e673ca26
diff --git a/fincept-qt/src/services/equity/MarketSentimentService.cpp b/fincept-qt/src/services/equity/MarketSentimentService.cpp
index 4519b96b9..2bd45d8bb 100644
--- a/fincept-qt/src/services/equity/MarketSentimentService.cpp
+++ b/fincept-qt/src/services/equity/MarketSentimentService.cpp
@@ -27,8 +27,8 @@ struct PendingSnapshotRequest {
QMap<QString, SentimentSourceSnapshot> sources;
};
-QString cache_key_for_symbol(const QString& symbol) {
- return "equity:adanos:sentiment:" + symbol.toUpper();
+QString cache_key_for_symbol(const QString& symbol, int days) {
+ return QString("equity:adanos:sentiment:%1:%2").arg(symbol.toUpper(), QString::number(days));
}
QByteArray variant_to_json_bytes(const QVariant& value) {
@@ -97,7 +97,7 @@ void MarketSentimentService::fetch_snapshot(const QString& symbol, int days, boo
return;
}
- const QString key = cache_key_for_symbol(normalized_symbol);
+ const QString key = cache_key_for_symbol(normalized_symbol, days);
if (!force && CacheManager::instance().has(key)) {
const auto cached = variant_to_json_bytes(CacheManager::instance().get(key));
const auto doc = QJsonDocument::fromJson(cached);
diff --git a/fincept-qt/src/services/equity/MarketSentimentSupport.cpp b/fincept-qt/src/services/equity/MarketSentimentSupport.cpp
index e1c398092..b5ea7fe7c 100644
--- a/fincept-qt/src/services/equity/MarketSentimentSupport.cpp
+++ b/fincept-qt/src/services/equity/MarketSentimentSupport.cpp
@@ -68,6 +68,11 @@ QString source_label(const QString& source_id) {
return source_id.toUpper();
}
+bool source_has_signal(const SentimentSourceSnapshot& snapshot) {
+ return snapshot.activity_count > 0.0 || snapshot.buzz_score > 0.0 || snapshot.bullish_pct > 0.0 ||
+ snapshot.sentiment_score != 0.0;
+}
+
SentimentSourceSnapshot parse_compare_payload(const QString& source_id, const QJsonObject& payload) {
SentimentSourceSnapshot snapshot;
snapshot.source_id = source_id;
@@ -79,13 +84,13 @@ SentimentSourceSnapshot parse_compare_payload(const QString& source_id, const QJ
}
const auto stock = stocks.first().toObject();
- snapshot.available = !stock.isEmpty();
snapshot.buzz_score = first_numeric(stock, {"buzz_score"});
snapshot.bullish_pct = first_numeric(stock, {"bullish_pct"});
snapshot.sentiment_score = first_numeric(stock, {"sentiment_score", "sentiment"});
snapshot.activity_count = first_numeric(
stock,
{"mentions", "trade_count", "unique_posts", "source_count", "subreddit_count", "market_count", "unique_traders"});
+ snapshot.available = source_has_signal(snapshot);
return snapshot;
}
diff --git a/fincept-qt/src/services/equity/MarketSentimentSupport.h b/fincept-qt/src/services/equity/MarketSentimentSupport.h
index c85a4fc35..506b400d6 100644
--- a/fincept-qt/src/services/equity/MarketSentimentSupport.h
+++ b/fincept-qt/src/services/equity/MarketSentimentSupport.h
@@ -11,6 +11,7 @@ inline constexpr const char* kProviderId = "adanos-market-sentiment";
QStringList source_ids();
QString source_label(const QString& source_id);
+bool source_has_signal(const SentimentSourceSnapshot& snapshot);
SentimentSourceSnapshot parse_compare_payload(const QString& source_id, const QJsonObject& payload);
QString compute_source_alignment(const QVector<SentimentSourceSnapshot>& sources);
QJsonObject snapshot_to_json(const MarketSentimentSnapshot& snapshot);
diff --git a/fincept-qt/tests/MarketSentimentSupportTest.cpp b/fincept-qt/tests/MarketSentimentSupportTest.cpp
index 333f9283f..9839aa1ba 100644
--- a/fincept-qt/tests/MarketSentimentSupportTest.cpp
+++ b/fincept-qt/tests/MarketSentimentSupportTest.cpp
@@ -9,6 +9,7 @@ using fincept::services::equity::MarketSentimentSnapshot;
using fincept::services::equity::SentimentSourceSnapshot;
using fincept::services::equity::sentiment::compute_source_alignment;
using fincept::services::equity::sentiment::parse_compare_payload;
+using fincept::services::equity::sentiment::source_has_signal;
using fincept::services::equity::sentiment::snapshot_from_json;
using fincept::services::equity::sentiment::snapshot_to_json;
@@ -58,6 +59,22 @@ bool test_polymarket_activity_fallback() {
expect(snapshot.activity_count == 87.0, "trade_count should backfill activity count");
}
+bool test_placeholder_payload_is_not_available() {
+ QJsonObject stock;
+ stock["ticker"] = "MSFT";
+ stock["buzz_score"] = 0.0;
+ stock["bullish_pct"] = 0.0;
+ stock["sentiment_score"] = 0.0;
+ stock["mentions"] = 0;
+
+ QJsonObject payload;
+ payload["stocks"] = QJsonArray{stock};
+
+ const auto snapshot = parse_compare_payload("news", payload);
+ return expect(!snapshot.available, "placeholder payload should not count as available coverage") &&
+ expect(!source_has_signal(snapshot), "placeholder payload should not be treated as signal");
+}
+
bool test_alignment_and_roundtrip() {
SentimentSourceSnapshot reddit;
reddit.source_id = "reddit";
@@ -111,6 +128,9 @@ int main() {
if (!test_polymarket_activity_fallback()) {
return 1;
}
+ if (!test_placeholder_payload_is_not_available()) {
+ return 1;
+ }
if (!test_alignment_and_roundtrip()) {
return 1;
}