← back to Maya Width Fix

test_widthlib.py

76 lines

#!/usr/bin/env python3
"""
Unit tests for widthlib — TK-11029.
Fixtures are the EXACT distinct width strings observed in maya_catalog (DWMR-8%),
plus negative / edge cases. Run: python3 test_widthlib.py   (exit 0 = all pass).
"""
from widthlib import classify_width, is_meta_pollution, width_measurements

# (raw_width, expected_decision, expected_width_inches)  — mirrors live DB distinct values
CASES = [
    # --- SINGLE roll width (the recoverable majority) ---
    ("Approx. 36 in untrimmed (91.4 cm)",              "SINGLE", "36"),    # island/braided/ajiro-boh  (106 rows)
    ("Approx. 55 in untrimmed (139.7 cm)",             "SINGLE", "55"),    # craze                     (47 rows)
    ("Approx. 49 in untrimmed (124.5 cm)",             "SINGLE", "49"),    # natural-elements          (24 rows)
    ("Approx. 28.5 in trimmed (72.4 cm)",              "SINGLE", "28.5"),  # beadazzled*               (9 rows)
    ("Approx. 36.5 in untrimmed (92.7 cm)",            "SINGLE", "36.5"),  # serigraph-cityscape       (8 rows)
    ("Approx. 54 in untrimmed (137.2 cm)",             "SINGLE", "54"),    # entwine-tranquil/hurly    (5 rows)
    ("Approx. 38 in untrimmed (96.5 cm)",              "SINGLE", "38"),    # bouquet                   (4 rows)
    ("Approx. 52 in untrimmed (132.1 cm)",             "SINGLE", "52"),    # coco-chenille             (1 row)
    ("54” untrimmed (137.2 m), 13.5 sq ft/yd (1.25 sq m/yd)", "SINGLE", "54"),  # cape-may curly-quote (1 row)

    # --- MULTI_WIDTH (genuine 2-width patterns; NO per-SKU width -> never guess) ---
    ("27\" Width Approx. 27 in untrimmed (68.6 cm); 54\" Width Approx. 54 in untrimmed (137.2 cm)",
     "MULTI_WIDTH", None),                                                            # entwine-inlet/serene (13 rows)
    ("27\" untrimmed (68.6 cm), 6.75 sq ft/yd (0.63 sq m/yd); 54\" untrimmed (137.2 cm), 13.5 sq ft/yd (1.25 sq m/yd)",
     "MULTI_WIDTH", None),                                                            # wisping-weaves-montauk (3 rows)
    ("15\" Width Approx. 30 in untrimmed (76.2 cm); 18\" Width Approx. 36 in untrimmed (91.4 cm)",
     "MULTI_WIDTH", None),                                                            # ajiro-sunburst (1 row)

    # --- META_POLLUTION (the bug this ticket exists for) ---
    ("=device-width, initial-scale=1\">",              "META_POLLUTION", None),        # cozy-* 404s (2 rows)

    # --- EMPTY / edge ---
    (None, "EMPTY", None),
    ("", "EMPTY", None),
    ("   ", "EMPTY", None),

    # --- UNPARSEABLE (defensive: real text but no roll-width token) ---
    ("Contact rep for width", "UNPARSEABLE", None),
]


def run():
    failures = []
    for raw, exp_dec, exp_wi in CASES:
        dec, wi = classify_width(raw)
        if (dec, wi) != (exp_dec, exp_wi):
            failures.append(f"  FAIL classify_width({raw!r}) = ({dec},{wi}) expected ({exp_dec},{exp_wi})")

    # is_meta_pollution focused checks
    assert is_meta_pollution("=device-width, initial-scale=1\">") is True
    assert is_meta_pollution("Approx. 36 in untrimmed (91.4 cm)") is False
    assert is_meta_pollution("") is False
    assert is_meta_pollution(None) is False

    # measurement counting is the multi-width discriminator
    assert len(width_measurements("Approx. 36 in untrimmed (91.4 cm)")) == 1
    assert len(width_measurements("27\" untrimmed (68.6 cm); 54\" untrimmed (137.2 cm)")) == 2
    assert len(width_measurements("=device-width, initial-scale=1\">")) == 0

    # numeric normalization
    assert classify_width("Approx. 36.0 in untrimmed (x)") == ("SINGLE", "36")
    assert classify_width("Approx. 28.50 in trimmed (x)") == ("SINGLE", "28.5")

    if failures:
        print(f"{len(failures)} FAILURE(S):")
        print("\n".join(failures))
        return 1
    print(f"OK: {len(CASES)} classify cases + focused asserts all pass.")
    return 0


if __name__ == "__main__":
    import sys
    sys.exit(run())