"""Tests for export_as_csv."""
import pandas as pd
import pytest

from activepathways import export_as_csv


def _make_result():
    return pd.DataFrame({
        "term_id": ["T1", "T2"],
        "term_name": ["Pathway One", "Pathway Two"],
        "adjusted_p_val": [0.01, 0.04],
        "overlap": [["G1", "G2"], ["G3"]],
        "Genes_RNA": [["G1"], ["G3"]],
        "Genes_Protein": [["G2"], None],
        "evidence": [["RNA"], ["Protein"]],
    })


def test_export_creates_file(tmp_path):
    res = _make_result()
    out = tmp_path / "results.csv"
    export_as_csv(res, str(out))
    assert out.exists()


def test_export_pipe_joins_overlap(tmp_path):
    res = _make_result()
    out = tmp_path / "results.csv"
    export_as_csv(res, str(out))
    df = pd.read_csv(str(out))
    assert df["overlap"].iloc[0] == "G1|G2"
    assert df["overlap"].iloc[1] == "G3"


def test_export_pipe_joins_genes_columns(tmp_path):
    res = _make_result()
    out = tmp_path / "results.csv"
    export_as_csv(res, str(out))
    df = pd.read_csv(str(out))
    assert df["Genes_RNA"].iloc[0] == "G1"


def test_export_does_not_mutate_input(tmp_path):
    res = _make_result()
    original_overlap = list(res["overlap"].iloc[0])
    out = tmp_path / "results.csv"
    export_as_csv(res, str(out))
    assert list(res["overlap"].iloc[0]) == original_overlap


def test_export_correct_number_of_rows(tmp_path):
    res = _make_result()
    out = tmp_path / "results.csv"
    export_as_csv(res, str(out))
    df = pd.read_csv(str(out))
    assert len(df) == 2
