# Test the fasta reader can be converted to a polars dataframe

from pathlib import Path

import pytest

from biobear import GTFReader
import importlib

DATA = Path(__file__).parent / "data"


@pytest.mark.skipif(
    not importlib.util.find_spec("polars"), reason="polars not installed"
)
def test_gtf_reader_to_polars():
    reader = GTFReader(DATA / "test.gtf")
    df = reader.to_polars()

    assert len(df) == 77


@pytest.mark.skipif(
    not importlib.util.find_spec("pandas"), reason="pandas not installed"
)
def test_gtf_reader_to_pandas():
    reader = GTFReader(DATA / "test.gtf")
    df = reader.to_pandas()

    assert len(df) == 77


@pytest.mark.skipif(
    not importlib.util.find_spec("polars"), reason="polars not installed"
)
def test_gtf_attr_struct():
    import polars as pl

    reader = GTFReader(DATA / "test.gtf")
    df = reader.to_polars()

    dtype = df.select(pl.col("attributes")).dtypes[0]

    key_field = pl.Field("key", pl.Utf8)
    value_field = pl.Field("value", pl.Utf8)
    assert dtype == pl.List(pl.Struct([key_field, value_field]))


def test_gtf_reader_to_scanner():
    reader = GTFReader(DATA / "test.gtf")
    scanner = reader.to_arrow_scanner()

    assert scanner.count_rows() == 77


@pytest.mark.skipif(
    not importlib.util.find_spec("polars"), reason="polars not installed"
)
def test_gtf_reader_gz():
    reader = GTFReader(DATA / "test.gtf.gz")
    df = reader.to_polars()

    assert len(df) == 77


def test_gtf_reader_gz_to_scanner():
    reader = GTFReader(DATA / "test.gtf.gz")
    scanner = reader.to_arrow_scanner()

    assert scanner.count_rows() == 77
