from pathlib import Path
import pytest
from qslib import Experiment
_TESTS_DIR = Path(__file__).parent
@pytest.fixture
def exp() -> Experiment:
return Experiment.from_file(_TESTS_DIR / "test.eds")
def test_plot_over_time_altair_basic(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
chart = exp.plot_over_time_altair()
assert isinstance(chart, alt.Chart)
assert hasattr(chart, "encoding")
assert hasattr(chart.encoding, "x")
assert hasattr(chart.encoding, "y")
assert hasattr(chart.encoding, "color")
assert chart.data is not None
assert len(chart.data) > 0
def test_plot_over_time_altair_with_samples(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
chart = exp.plot_over_time_altair(samples="Sample 1")
assert isinstance(chart, alt.Chart)
assert chart.data is not None
chart = exp.plot_over_time_altair(samples=["Sample 1", "Sample 2"])
assert isinstance(chart, alt.Chart)
assert chart.data is not None
def test_plot_over_time_altair_with_filters(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
filters = list(exp.all_filters)
if filters:
filter_str = str(filters[0])
chart = exp.plot_over_time_altair(filters=filter_str)
assert isinstance(chart, alt.Chart)
assert chart.data is not None
def test_plot_over_time_altair_duration_units(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
for units in ["hours", "minutes", "seconds"]:
chart = exp.plot_over_time_altair(duration_units=units)
assert isinstance(chart, alt.Chart)
assert chart.data is not None
assert hasattr(chart.encoding, "x")
assert hasattr(chart.encoding.x, "axis")
def test_plot_over_time_altair_legend_control(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
chart_with_legend = exp.plot_over_time_altair(show_legend=True)
assert isinstance(chart_with_legend, alt.Chart)
assert hasattr(chart_with_legend.encoding, "color")
chart_no_legend = exp.plot_over_time_altair(show_legend=False)
assert isinstance(chart_no_legend, alt.Chart)
assert hasattr(chart_no_legend.encoding, "color")
def test_plot_temperatures_altair(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
chart = exp.plot_temperatures(method="altair")
assert isinstance(chart, alt.Chart)
assert hasattr(chart, "encoding")
assert hasattr(chart.encoding, "x")
assert hasattr(chart.encoding, "y")
assert chart.data is not None
assert len(chart.data) > 0
def test_plot_temperatures_altair_with_options(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
for time_unit in ["h", "m", "s"]:
chart = exp.plot_temperatures(method="altair", time_units=time_unit)
assert isinstance(chart, alt.Chart)
assert chart.data is not None
def test_altair_error_handling(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
with pytest.raises(ValueError, match="Invalid duration_units"):
exp.plot_over_time_altair(duration_units="invalid")
with pytest.raises(ValueError, match="Invalid start_time"):
exp.plot_over_time_altair(start_time="invalid")
def test_altair_chart_data_structure(exp: Experiment) -> None:
try:
import altair as alt
import polars as pl
except ImportError:
pytest.skip("Altair or Polars not available")
chart = exp.plot_over_time_altair()
data = pl.DataFrame(chart.data)
expected_cols = ["time_since_mark_float", "processed_fluorescence", "sample", "filter_set"]
for col in expected_cols:
assert col in data.columns
assert data["time_since_mark_float"].dtype == pl.Float64
assert data["processed_fluorescence"].dtype in [pl.Float64, pl.Float32]
assert data["sample"].dtype == pl.String
assert data["filter_set"].dtype == pl.String
def test_altair_imports_and_setup(exp: Experiment) -> None:
try:
import altair as alt
except ImportError:
pytest.skip("Altair not available")
chart = exp.plot_over_time_altair()
assert isinstance(chart, alt.Chart)