Skip to content

Commit

Permalink
conditional check for schutz plot -- does not warn on ubuntu
Browse files Browse the repository at this point in the history
  • Loading branch information
jGaboardi committed Sep 3, 2024
1 parent 3d3fc37 commit e6db5a4
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions inequality/tests/test_schutz.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import os
import platform

import matplotlib.pyplot as plt
import pandas as pd
import pytest
from inequality.schutz import Schutz

NOT_LINUX = platform.system() != "Linux"


@pytest.fixture
def example_dataframe():
data = {"NAME": ["A", "B", "C", "D", "E"], "Y": [1000, 2000, 1500, 3000, 2500]}
return pd.DataFrame(data)


@pytest.fixture
def noninteractive_warning():
return pytest.warns(
UserWarning,
match="FigureCanvasAgg is non-interactive, and thus cannot be shown",
)
def plot_warning_helper(schutz_obj):
if NOT_LINUX:
with pytest.warns(
UserWarning,
match="FigureCanvasAgg is non-interactive, and thus cannot be shown",
):
schutz_obj.plot()
else:
schutz_obj.plot()


def test_schutz_distance(example_dataframe):
Expand All @@ -40,25 +46,23 @@ def test_schutz_coefficient(example_dataframe):
assert pytest.approx(schutz_obj.coefficient, 0.1) == expected_coefficient


def test_schutz_plot_runs_without_errors(example_dataframe, noninteractive_warning):
def test_schutz_plot_runs_without_errors(example_dataframe):
schutz_obj = Schutz(example_dataframe, "Y")
try:
with noninteractive_warning:
schutz_obj.plot()
plot_warning_helper(schutz_obj)
except Exception as e:
pytest.fail(f"Plotting failed: {e}")


def test_schutz_plot_output(example_dataframe, tmpdir, noninteractive_warning):
def test_schutz_plot_output(example_dataframe, tmpdir):
"""Test if the plot output matches the expected result by saving
the plot and comparing it."""
schutz_obj = Schutz(example_dataframe, "Y")

# Save the plot to a temporary directory
plot_file = os.path.join(tmpdir, "schutz_plot.png")
plt.figure()
with noninteractive_warning:
schutz_obj.plot()
plot_warning_helper(schutz_obj)
plt.savefig(plot_file)
plt.close()

Expand Down

0 comments on commit e6db5a4

Please sign in to comment.