"""Tests para cramers_v."""

import os
import random
import sys

sys.path.insert(0, os.path.dirname(__file__))

from cramers_v import cramers_v


def test_perfect_association_is_near_one():
    a = ["x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y", "z"]
    b = list(a)  # b == a -> asociacion perfecta
    v = cramers_v(a, b)
    assert v > 0.95
    assert v <= 1.0


def test_independent_columns_low_value():
    rng = random.Random(42)
    cats = ["a", "b", "c", "d"]
    a = [rng.choice(cats) for _ in range(2000)]
    b = [rng.choice(cats) for _ in range(2000)]
    v = cramers_v(a, b)
    assert 0.0 <= v < 0.5


def test_single_category_returns_zero():
    a = ["only"] * 10  # <2 categorias en a
    b = ["x", "y", "x", "y", "x", "y", "x", "y", "x", "y"]
    assert cramers_v(a, b) == 0.0


def test_fewer_than_two_pairs_returns_zero():
    assert cramers_v([], []) == 0.0
    assert cramers_v(["a"], ["b"]) == 0.0


def test_none_pairs_are_discarded():
    a = ["x", None, "y", "x", None, "y", "x", "y"]
    b = ["x", "z", "y", "x", "z", "y", None, "y"]
    v = cramers_v(a, b)
    assert isinstance(v, float)
    assert 0.0 <= v <= 1.0


def test_always_returns_float_never_none():
    assert isinstance(cramers_v(["a", "b"], ["a", "b"]), float)
    assert isinstance(cramers_v([None], [None]), float)


def test_derived_column_high_association():
    rng = random.Random(7)
    a = [rng.choice(["red", "green", "blue"]) for _ in range(600)]
    # b derivada de a (mapeo deterministico) -> alta asociacion.
    mapping = {"red": "hot", "green": "cool", "blue": "cool"}
    b = [mapping[x] for x in a]
    v = cramers_v(a, b)
    assert v > 0.5
