Download tests/test_market_data_normalizer.py from polyHedra/PolyHedra: direct link, hf CLI and curl.
- Browser
- Download file 3.69 kB
-
https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_market_data_normalizer.py
- Command line
-
hf download hf://spaces/polyHedra/PolyHedra/tests/test_market_data_normalizer.py
-
curl -L -o test_market_data_normalizer.py https://huggingface.co/spaces/polyHedra/PolyHedra/resolve/main/tests/test_market_data_normalizer.py
3.69 kB
| """Existing Finnhub payloads normalize into strict V8 market artifacts.""" | |
| from datetime import datetime, timezone | |
| from polyhedra.services.market_data_normalizer import ( | |
| normalize_finnhub_market_data, render_market_data_markdown, | |
| ) | |
| AT = datetime(2026, 9, 12, 12, tzinfo=timezone.utc) | |
| def payload(): | |
| return { | |
| "TIME_SERIES_DAILY_ADJUSTED": {"Time Series (Daily)": { | |
| "2026-09-10": { | |
| "1. open": "100", "2. high": "110", "3. low": "95", | |
| "4. close": "105", "5. adjusted close": "104.5", "6. volume": "0", | |
| }, | |
| }}, | |
| "_OHLCV_provider": "finnhub", | |
| "SMA": {"Technical Analysis: SMA": {"2026-09-10": {"SMA": "101"}}}, | |
| "EMA": {"Technical Analysis: EMA": {"2026-09-10": {"EMA": "102"}}}, | |
| "RSI": {"Technical Analysis: RSI": {"2026-09-10": {"RSI": "55"}}}, | |
| "MACD": {"Technical Analysis: MACD": {"2026-09-10": { | |
| "MACD": "1", "MACD_Signal": "0.7", "MACD_Hist": "0.3", | |
| }}}, | |
| "NEWS_SENTIMENT": {"feed": [{ | |
| "time_published": "20260910T140000", "title": "Market update", | |
| "source": "Test Wire", "url": "https://example.com/story", | |
| "overall_sentiment_score": 0.0, "overall_sentiment_label": "Neutral", | |
| }]}, | |
| "EARNINGS": {"quarterlyEarnings": []}, | |
| } | |
| def normalize(ticker="SPY", data=None): | |
| return normalize_finnhub_market_data( | |
| ticker=ticker, payload=data or payload(), run_id="run1", session_id="session1", | |
| retrieved_at=AT, | |
| ) | |
| def test_normalizes_prices_indicators_and_unknown_finnhub_sentiment(): | |
| artifact = normalize() | |
| assert artifact.status == "COMPLETE" | |
| assert artifact.ohlcv[0].volume == 0 | |
| assert artifact.indicators.points[0].rsi_14 == 55 | |
| assert artifact.news[0].provider_sentiment_score is None | |
| assert artifact.provenance[0].provider == "finnhub" | |
| assert artifact.provenance[3].availability == "NOT_APPLICABLE" | |
| assert artifact.market_date.isoformat() == "2026-09-10" | |
| def test_missing_provider_components_are_explicit_not_empty_success(): | |
| artifact = normalize(data={"_CANDLE_error": "rate limited", "_NEWS_error": "timeout"}) | |
| assert artifact.status == "UNAVAILABLE" | |
| assert artifact.ohlcv == [] | |
| assert {issue.component for issue in artifact.quality_issues} == { | |
| "OHLCV", "INDICATORS", "NEWS", | |
| } | |
| assert {item.availability for item in artifact.provenance} == { | |
| "UNAVAILABLE", "NOT_APPLICABLE", | |
| } | |
| def test_general_equity_preserves_negative_earnings_and_empty_news(): | |
| data = payload() | |
| data["NEWS_SENTIMENT"] = {"feed": []} | |
| data["EARNINGS"] = {"quarterlyEarnings": [{ | |
| "fiscalDateEnding": "2026-06-30", "reportedEPS": "-1.0", | |
| "estimatedEPS": "", "surprise": "", "surprisePercentage": "", | |
| }]} | |
| artifact = normalize("AAPL", data) | |
| assert artifact.earnings[0].actual_eps == -1 | |
| assert artifact.earnings[0].estimated_eps is None | |
| assert artifact.provenance[2].availability == "AVAILABLE" | |
| assert artifact.provenance[2].record_count == 0 | |
| def test_invalid_rows_are_excluded_and_visible_in_markdown(): | |
| data = payload() | |
| data["TIME_SERIES_DAILY_ADJUSTED"]["Time Series (Daily)"]["2026-09-11"] = { | |
| "1. open": "nan", "2. high": "110", "3. low": "95", | |
| "4. close": "105", "5. adjusted close": "105", "6. volume": "42", | |
| } | |
| artifact = normalize(data=data) | |
| assert len(artifact.ohlcv) == 1 | |
| assert any(issue.code == "INVALID_PROVIDER_RECORD" for issue in artifact.quality_issues) | |
| rendered = render_market_data_markdown(artifact) | |
| assert "INVALID_PROVIDER_RECORD" in rendered | |
| assert "2026-09-10 | 100.0 | 110.0" in rendered | |