"""Tests para from_csv."""

from from_csv import from_csv


def test_csv_simple_con_header():
    text = "nombre,edad\r\nAna,30\r\nBob,25"
    result = from_csv(text)
    assert len(result) == 2
    assert result[0] == {"nombre": "Ana", "edad": "30"}
    assert result[1] == {"nombre": "Bob", "edad": "25"}


def test_campos_con_escaping():
    text = 'a,b\r\n"dijo ""hola""","uno,dos"'
    result = from_csv(text)
    assert result[0]["a"] == 'dijo "hola"'
    assert result[0]["b"] == "uno,dos"


def test_sin_header_keys_generadas():
    text = "foo,bar\nbaz,qux"
    result = from_csv(text, has_header=False)
    assert result[0] == {"col_0": "foo", "col_1": "bar"}
    assert result[1] == {"col_0": "baz", "col_1": "qux"}


def test_lineas_vacias_ignoradas():
    text = "x,y\n\n1,2\n\n3,4\n"
    result = from_csv(text)
    assert len(result) == 2
    assert result[0] == {"x": "1", "y": "2"}


def test_un_solo_campo_por_fila():
    text = "valor\nhola\nmundo"
    result = from_csv(text)
    assert len(result) == 2
    assert result[0] == {"valor": "hola"}
    assert result[1] == {"valor": "mundo"}
