Skip to content

Commit 1d67899

Browse files
authored
test: ✅ Add unit tests for SpectraFit version and Python 3.8 deprecation 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
1 parent f5555e1 commit 1d67899

File tree

3 files changed

+70
-16
lines changed

3 files changed

+70
-16
lines changed

spectrafit/__init__.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@
1515
import warnings
1616

1717

18-
if sys.version_info[:2] == (3, 8):
18+
PYTHON_38_VERSION = (3, 8)
19+
20+
if sys.version_info[:2] == PYTHON_38_VERSION:
21+
version_str = f"{PYTHON_38_VERSION[0]}.{PYTHON_38_VERSION[1]}"
1922
warnings.warn(
20-
"Support for Python 3.8 is approaching its end-of-life. "
21-
"Please consider upgrading to Python 3.9 or newer. "
22-
"For more details, see: "
23+
f"Support for Python {version_str} is approaching its end-of-life."
24+
" Please consider upgrading to Python 3.9 or newer."
25+
" For more details, see:"
2326
"https://devguide.python.org/versions/#end-of-life-branches.",
2427
DeprecationWarning,
2528
)

spectrafit/plugins/notebook.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ def markdown_display(df: pd.DataFrame) -> None:
139139

140140

141141
class DataFramePlot:
142-
"""Class to plot a data frame."""
142+
"""Class to plot a dataframe."""
143143

144144
def plot_2dataframes(
145145
self,
146146
args_plot: PlotAPI,
147147
df_1: pd.DataFrame,
148148
df_2: Optional[pd.DataFrame] = None,
149149
) -> None:
150-
"""Plot two data frames.
150+
"""Plot two dataframes.
151151
152152
!!! info "About the plot"
153153
@@ -159,14 +159,14 @@ def plot_2dataframes(
159159
160160
Currently, the `line_dash_map` is not working, and the dash is not
161161
plotted. This is likely due to the columns not being labeled in the
162-
data frame.
162+
dataframe.
163163
164164
Args:
165165
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
166-
df_1 (pd.DataFrame): First data frame to plot, which will generate
166+
df_1 (pd.DataFrame): First dataframe to plot, which will generate
167167
a fit plot with residual plot. The ratio is 70% to 20% with
168168
10% space in between.
169-
df_2 (Optional[pd.DataFrame], optional): Second optional data frame to
169+
df_2 (Optional[pd.DataFrame], optional): Second optional dataframe to
170170
plot for comparison. In this case, the ratio between the first
171171
and second plot will be the same. Defaults to None.
172172
"""
@@ -186,7 +186,7 @@ def plot_2dataframes(
186186
)
187187

188188
def _plot_single_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> Figure:
189-
"""Plot a single data frame with residuals."""
189+
"""Plot a single dataframe with residuals."""
190190
fig = make_subplots(
191191
rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05
192192
)
@@ -205,7 +205,7 @@ def _plot_single_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> Figure
205205
def _plot_two_dataframes(
206206
self, args_plot: PlotAPI, df_1: pd.DataFrame, df_2: pd.DataFrame
207207
) -> Figure:
208-
"""Plot two data frames for comparison."""
208+
"""Plot two dataframes for comparison."""
209209
fig = make_subplots(
210210
rows=2, cols=1, shared_xaxes=True, shared_yaxes=True, vertical_spacing=0.05
211211
)
@@ -293,11 +293,11 @@ def _update_plot_layout(
293293
fig.update_yaxes(title_text=yaxis_title, row=2, col=1)
294294

295295
def plot_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
296-
"""Plot the data frame according to the PlotAPI arguments.
296+
"""Plot the dataframe according to the PlotAPI arguments.
297297
298298
Args:
299299
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
300-
df (pd.DataFrame): Data frame to plot.
300+
df (pd.DataFrame): Dataframe to plot.
301301
"""
302302
fig = px.line(df, x=args_plot.x, y=args_plot.y)
303303
height = args_plot.size[1][0]
@@ -324,11 +324,11 @@ def plot_dataframe(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
324324
)
325325

326326
def plot_global_fit(self, args_plot: PlotAPI, df: pd.DataFrame) -> None:
327-
"""Plot the global data frame according to the PlotAPI arguments.
327+
"""Plot the global dataframe according to the PlotAPI arguments.
328328
329329
Args:
330330
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
331-
df (pd.DataFrame): Data frame to plot.
331+
df (pd.DataFrame): Dataframe to plot.
332332
"""
333333
num_fits = df.columns.str.startswith(ColumnNamesAPI().fit).sum()
334334
for i in range(1, num_fits + 1):
@@ -354,7 +354,7 @@ def plot_metric(
354354
355355
Args:
356356
args_plot (PlotAPI): PlotAPI object for the settings of the plot.
357-
df_metric (pd.DataFrame): Metric data frame to plot.
357+
df_metric (pd.DataFrame): Metric dataframe to plot.
358358
bar_criteria (Union[str, List[str]]): Criteria to plot as bars.
359359
line_criteria (Union[str, List[str]]): Criteria to plot as lines.
360360
"""

spectrafit/test/test_init.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Tests for the SpectraFit __init__.py file."""
2+
3+
import importlib
4+
import sys
5+
import warnings
6+
7+
import spectrafit
8+
9+
from pytest import MonkeyPatch
10+
from spectrafit import PYTHON_38_VERSION
11+
from spectrafit import __version__
12+
13+
14+
def test_version() -> None:
15+
"""Test the version string."""
16+
assert __version__ == "1.0.5"
17+
18+
19+
def test_python_38_warning(monkeypatch: MonkeyPatch) -> None:
20+
"""Test that a warning is issued for Python 3.8."""
21+
# Set the Python version to 3.8
22+
monkeypatch.setattr(sys, "version_info", (3, 8, 0))
23+
24+
version_str = f"{PYTHON_38_VERSION[0]}.{PYTHON_38_VERSION[1]}"
25+
26+
with warnings.catch_warnings(record=True) as w:
27+
warnings.simplefilter("always")
28+
# Reload the module to trigger the warning
29+
importlib.reload(spectrafit)
30+
31+
# Check that a warning was issued
32+
assert len(w) == 1
33+
assert issubclass(w[-1].category, DeprecationWarning)
34+
assert (
35+
f"Support for Python {version_str} is approaching its end-of-life."
36+
in str(w[-1].message)
37+
)
38+
39+
40+
def test_no_warning_for_other_versions(monkeypatch: MonkeyPatch) -> None:
41+
"""Test that no warning is issued for Python versions other than 3.8."""
42+
# Set the Python version to 3.9
43+
monkeypatch.setattr(sys, "version_info", (3, 9, 0))
44+
45+
with warnings.catch_warnings(record=True) as w:
46+
warnings.simplefilter("always")
47+
# Reload the module to ensure no warning is triggered
48+
importlib.reload(spectrafit)
49+
50+
# Check that no warning was issued
51+
assert len(w) == 0

0 commit comments

Comments
 (0)