Skip to content

Commit

Permalink
test: ✅ Add unit tests for SpectraFit version and Python 3.8 deprecat…
Browse files Browse the repository at this point in the history
…ion warning (#1641)

* refactor: ♻️ Improve Python 3.8 deprecation warning message format

* refactor: ♻️ Update docstrings to use 'dataframe' consistently

* test: ✅ Add unit tests for SpectraFit version and Python 3.8 deprecation warning
  • Loading branch information
Anselmoo authored Oct 14, 2024
1 parent f5555e1 commit 1d67899
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 16 deletions.
11 changes: 7 additions & 4 deletions spectrafit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
import warnings


if sys.version_info[:2] == (3, 8):
PYTHON_38_VERSION = (3, 8)

if sys.version_info[:2] == PYTHON_38_VERSION:
version_str = f"{PYTHON_38_VERSION[0]}.{PYTHON_38_VERSION[1]}"
warnings.warn(
"Support for Python 3.8 is approaching its end-of-life. "
"Please consider upgrading to Python 3.9 or newer. "
"For more details, see: "
f"Support for Python {version_str} is approaching its end-of-life."
" Please consider upgrading to Python 3.9 or newer."
" For more details, see:"
"https://devguide.python.org/versions/#end-of-life-branches.",
DeprecationWarning,
)
Expand Down
24 changes: 12 additions & 12 deletions spectrafit/plugins/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ def markdown_display(df: pd.DataFrame) -> None:


class DataFramePlot:
"""Class to plot a data frame."""
"""Class to plot a dataframe."""

def plot_2dataframes(
self,
args_plot: PlotAPI,
df_1: pd.DataFrame,
df_2: Optional[pd.DataFrame] = None,
) -> None:
"""Plot two data frames.
"""Plot two dataframes.
!!! info "About the plot"
Expand All @@ -159,14 +159,14 @@ def plot_2dataframes(
Currently, the `line_dash_map` is not working, and the dash is not
plotted. This is likely due to the columns not being labeled in the
data frame.
dataframe.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df_1 (pd.DataFrame): First data frame to plot, which will generate
df_1 (pd.DataFrame): First dataframe to plot, which will generate
a fit plot with residual plot. The ratio is 70% to 20% with
10% space in between.
df_2 (Optional[pd.DataFrame], optional): Second optional data frame to
df_2 (Optional[pd.DataFrame], optional): Second optional dataframe to
plot for comparison. In this case, the ratio between the first
and second plot will be the same. Defaults to None.
"""
Expand All @@ -186,7 +186,7 @@ def plot_2dataframes(
)

def _plot_single_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> Figure:
"""Plot a single data frame with residuals."""
"""Plot a single dataframe with residuals."""
fig = make_subplots(
rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05
)
Expand All @@ -205,7 +205,7 @@ def _plot_single_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> Figure
def _plot_two_dataframes(
self, args_plot: PlotAPI, df_1: pd.DataFrame, df_2: pd.DataFrame
) -> Figure:
"""Plot two data frames for comparison."""
"""Plot two dataframes for comparison."""
fig = make_subplots(
rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05
)
Expand Down Expand Up @@ -293,11 +293,11 @@ def _update_plot_layout(
fig.update_yaxes(title_text=yaxis_title, row=2, col=1)

def plot_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
"""Plot the data frame according to the PlotAPI arguments.
"""Plot the dataframe according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df (pd.DataFrame): Data frame to plot.
df (pd.DataFrame): Dataframe to plot.
"""
fig = px.line(df, x=args_plot.x, y=args_plot.y)
height = args_plot.size[1][0]
Expand All @@ -324,11 +324,11 @@ def plot_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
)

def plot_global_fit(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
"""Plot the global data frame according to the PlotAPI arguments.
"""Plot the global dataframe according to the PlotAPI arguments.
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df (pd.DataFrame): Data frame to plot.
df (pd.DataFrame): Dataframe to plot.
"""
num_fits = df.columns.str.startswith(ColumnNamesAPI().fit).sum()
for i in range(1, num_fits + 1):
Expand All @@ -354,7 +354,7 @@ def plot_metric(
Args:
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
df_metric (pd.DataFrame): Metric data frame to plot.
df_metric (pd.DataFrame): Metric dataframe to plot.
bar_criteria (Union[str, List[str]]): Criteria to plot as bars.
line_criteria (Union[str, List[str]]): Criteria to plot as lines.
"""
Expand Down
51 changes: 51 additions & 0 deletions spectrafit/test/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Tests for the SpectraFit __init__.py file."""

import importlib
import sys
import warnings

import spectrafit

from pytest import MonkeyPatch
from spectrafit import PYTHON_38_VERSION
from spectrafit import __version__


def test_version() -> None:
"""Test the version string."""
assert __version__ == "1.0.5"


def test_python_38_warning(monkeypatch: MonkeyPatch) -> None:
"""Test that a warning is issued for Python 3.8."""
# Set the Python version to 3.8
monkeypatch.setattr(sys, "version_info", (3, 8, 0))

version_str = f"{PYTHON_38_VERSION[0]}.{PYTHON_38_VERSION[1]}"

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
# Reload the module to trigger the warning
importlib.reload(spectrafit)

# Check that a warning was issued
assert len(w) == 1
assert issubclass(w[-1].category, DeprecationWarning)
assert (
f"Support for Python {version_str} is approaching its end-of-life."
in str(w[-1].message)
)


def test_no_warning_for_other_versions(monkeypatch: MonkeyPatch) -> None:
"""Test that no warning is issued for Python versions other than 3.8."""
# Set the Python version to 3.9
monkeypatch.setattr(sys, "version_info", (3, 9, 0))

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
# Reload the module to ensure no warning is triggered
importlib.reload(spectrafit)

# Check that no warning was issued
assert len(w) == 0

0 comments on commit 1d67899

Please sign in to comment.