"""The registries in config/ (retailers, Wikipedia articles, retail series) must follow
their contracts, so anyone can add a retailer, an article or a series by adding a row
without breaking the pipeline. The event calendar has its own tests (test_event_calendar.py)."""
from __future__ import annotations
import re
import pytest
from common.contracts import load_retail_series, load_retailers, load_wiki_articles
from common.study import MARKET_TICKER
TICKER = re.compile(r"^[A-Z0-9][A-Z0-9.\-]*$")
COUNTRY = re.compile(r"^[A-Z]{2}$")
SNAKE_CASE = re.compile(r"^[a-z][a-z0-9_]*$")
def duplicates(values) -> set:
values = list(values)
return {v for v in values if values.count(v) > 1}
def test_retailer_tickers_are_unique_yahoo_symbols():
tickers = [r["ticker"] for r in load_retailers()]
assert not duplicates(tickers), f"duplicate ticker: {duplicates(tickers)}"
bad = [t for t in tickers if not TICKER.match(t)]
assert not bad, f"tickers must be Yahoo Finance symbols in capitals: {bad}"
def test_retailer_columns_use_known_values():
for r in load_retailers():
assert r["company"].strip(), f"{r['ticker']}: company is empty"
assert COUNTRY.match(r["country"]), f"{r['ticker']}: country must be a two-letter code like US or CA"
assert r["retailer_type"] in {"online", "store"}, f"{r['ticker']}: retailer_type must be online or store"
assert r["in_scope"] in {"yes", "no"}, f"{r['ticker']}: in_scope must be yes or no"
def test_market_index_is_not_listed_as_a_retailer():
assert MARKET_TICKER not in {r["ticker"] for r in load_retailers()}, (
f"{MARKET_TICKER} is the market index (common.study.MARKET_TICKER); remove it from config/retailers.csv"
)
def test_some_retailers_are_in_scope():
assert load_retailers(in_scope_only=True)
def test_every_wiki_article_belongs_to_a_listed_retailer():
tickers = {r["ticker"] for r in load_retailers()}
unknown = {a["ticker"] for a in load_wiki_articles()} - tickers
assert not unknown, f"config/wiki_articles.csv uses tickers missing from config/retailers.csv: {unknown}"
def test_every_in_scope_retailer_has_a_wiki_article():
covered = {a["ticker"] for a in load_wiki_articles()}
missing = {r["ticker"] for r in load_retailers(in_scope_only=True)} - covered
assert not missing, f"in-scope retailers with no row in config/wiki_articles.csv: {missing}"
def test_wiki_titles_are_unique_and_use_underscores():
titles = [a["article"] for a in load_wiki_articles()]
assert not duplicates(titles), f"duplicate article: {duplicates(titles)}"
assert not [t for t in titles if " " in t or not t], "titles use underscores instead of spaces"
def test_in_scope_articles_skip_candidate_retailers():
in_scope = {r["ticker"] for r in load_retailers(in_scope_only=True)}
assert {a["ticker"] for a in load_wiki_articles(in_scope_only=True)} <= in_scope
def test_retail_series_ids_and_categories_are_unique():
series = load_retail_series()
assert series
assert not duplicates(s["series_id"] for s in series)
assert not duplicates(s["category"] for s in series)
assert all(SNAKE_CASE.match(s["category"]) for s in series), "categories must be snake_case"
def test_loaders_reject_a_file_whose_columns_do_not_match_the_contract(tmp_path):
wrong = tmp_path / "retailers.csv"
wrong.write_text("ticker,company,exchange\nWMT,Walmart,NYSE\n")
with pytest.raises(ValueError, match="retailers"):
load_retailers(wrong)
def test_loaders_read_files_saved_by_excel(tmp_path):
excel = tmp_path / "retail_series.csv"
excel.write_bytes(b"\xef\xbb\xbfseries_id,category,notes\r\nRSXFS,all_retail,\r\n")
assert load_retail_series(excel) == [{"series_id": "RSXFS", "category": "all_retail", "notes": ""}]