"""Tests para spearman_corr."""

import math

from spearman_corr import spearman_corr


def test_relacion_monotonica_perfecta():
    # ys = x**2 es monotonica creciente para x>0 (no lineal): Spearman ~ 1.0
    xs = [1, 2, 3, 4, 5, 6]
    ys = [x ** 2 for x in xs]
    result = spearman_corr(xs, ys)
    assert math.isclose(result, 1.0, abs_tol=1e-9)


def test_relacion_monotonica_decreciente():
    xs = [1, 2, 3, 4, 5]
    ys = [10, 8, 6, 4, 2]
    result = spearman_corr(xs, ys)
    assert math.isclose(result, -1.0, abs_tol=1e-9)


def test_pares_con_none_se_ignoran():
    # Los pares (3, None) y (None, 99) se descartan; el resto es monotonico perfecto.
    xs = [1, 2, 3, 4, None, 5]
    ys = [1, 4, None, 16, 99, 25]
    result = spearman_corr(xs, ys)
    assert math.isclose(result, 1.0, abs_tol=1e-9)


def test_pares_con_nan_se_ignoran():
    xs = [1, 2, float("nan"), 4, 5]
    ys = [2, 4, 100, 8, 10]
    result = spearman_corr(xs, ys)
    assert math.isclose(result, 1.0, abs_tol=1e-9)


def test_menos_de_3_pares_validos_retorna_cero():
    xs = [1, 2, None]
    ys = [5, 9, 3]
    assert spearman_corr(xs, ys) == 0.0


def test_varianza_cero_retorna_cero():
    xs = [7, 7, 7, 7]
    ys = [1, 2, 3, 4]
    assert spearman_corr(xs, ys) == 0.0


def test_listas_vacias_retorna_cero():
    assert spearman_corr([], []) == 0.0


def test_resultado_es_float():
    result = spearman_corr([1, 2, 3, 4], [4, 3, 2, 1])
    assert isinstance(result, float)
