main
py 23 lines 1.98 KB
Raw
1 from app.sector_leaderboard import build_sector_leaderboard, normalize_sector
2
3 def stock(identifier, score, sector="Technology", ticker=None, coverage=1): return {"globalInstrumentId": str(identifier), "ticker": ticker or f"T{identifier}", "sector": sector, "score": score, "evidenceCoverage": coverage}
4 def test_top_five_normalization_and_deterministic_duplicates():
5 values=[stock(i, i, " technology " if i % 2 else "Technology") for i in range(7)]
6 values += [stock("1", 99, "Technology", "WIN", 3), stock("x", 4, "Financial Services"), stock("y", 3, "Financials")]
7 result=build_sector_leaderboard(values)
8 tech=next(x for x in result["sectors"] if x["normalizedSector"] == "technology")
9 assert len(tech["stocks"]) == 5 and tech["stocks"][0]["ticker"] == "WIN" and len({x["globalInstrumentId"] for x in tech["stocks"]}) == 5
10 assert build_sector_leaderboard(reversed(values)) == result
11 def test_sector_aliases_and_empty_are_safe():
12 assert normalize_sector(" Health Care ") == ("healthcare", "Healthcare")
13 assert normalize_sector("Consumer Cyclical") == ("consumer-discretionary", "Consumer Discretionary")
14 assert build_sector_leaderboard([{"globalInstrumentId":"x","score":1}]) == {"sectors": []}
15
16 def test_provider_neutral_eligibility_and_sector_sizes():
17 values = [stock("nse", 8, "Technology", "NSE", 2), stock("sec", 7, "technology", "SEC", 1), stock("eodhd", 6, "Technology", "EOD", 1)]
18 values += [stock("f", 9, "Financial Services"), stock("g", 8, "Financials"), {"globalInstrumentId":"missing-sector", "score":99}, {"globalInstrumentId":"missing-score", "sector":"Technology"}]
19 result = build_sector_leaderboard(values)
20 technology = next(group for group in result["sectors"] if group["normalizedSector"] == "technology")
21 financials = next(group for group in result["sectors"] if group["normalizedSector"] == "financials")
22 assert [item["globalInstrumentId"] for item in technology["stocks"]] == ["nse", "sec", "eodhd"]
23 assert len(financials["stocks"]) == 2