Skip to content

Commit

Permalink
Add some test
Browse files Browse the repository at this point in the history
  • Loading branch information
Zybulon committed Feb 10, 2024
1 parent 6a8031a commit 62fe3f4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/matplotlib/tests/test_backend_gtk3.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import os
from matplotlib import pyplot as plt

import pytest
from unittest import mock

import gi
try:
# :raises ValueError: If module/version is already loaded, already
# required, or unavailable.
gi.require_version("Gtk", "3.0")
except ValueError as e:

Check warning on line 12 in lib/matplotlib/tests/test_backend_gtk3.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_backend_gtk3.py#L12

Added line #L12 was not covered by tests
# in this case we want to re-raise as ImportError so the
# auto-backend selection logic correctly skips.
raise ImportError(e) from e

Check warning on line 15 in lib/matplotlib/tests/test_backend_gtk3.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_backend_gtk3.py#L15

Added line #L15 was not covered by tests

from gi.repository import Gtk

pytest.importorskip("matplotlib.backends.backend_gtk3agg")

Expand Down Expand Up @@ -49,3 +62,20 @@ def receive(event):
fig.canvas.mpl_connect("draw_event", send)
fig.canvas.mpl_connect("key_press_event", receive)
plt.show()


@pytest.mark.backend("gtk3agg", skip_on_importerror=True)
def test_save_figure_return():
fig, ax = plt.subplots()
ax.imshow([[1]])
with mock.patch("gi.repository.Gtk.FileFilter") as fileFilter:
filt = fileFilter.return_value
filt.get_name.return_value = "Portable Network Graphics"
with mock.patch("gi.repository.Gtk.FileChooserDialog") as dialogChooser:
dialog = dialogChooser.return_value
dialog.get_filter.return_value = filt
dialog.get_filename.return_value = "foobar.png"
dialog.run.return_value = Gtk.ResponseType.OK
fname = fig.canvas.manager.toolbar.save_figure()
os.remove("foobar.png")
assert fname == "foobar.png"
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_backend_macosx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest
from unittest import mock

import matplotlib as mpl
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -44,3 +45,14 @@ def new_choose_save_file(title, directory, filename):
# Check the savefig.directory rcParam got updated because
# we added a subdirectory "test"
assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"


@pytest.mark.backend('macosx')

Check warning on line 50 in lib/matplotlib/tests/test_backend_macosx.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_backend_macosx.py#L50

Added line #L50 was not covered by tests
def test_save_figure_return():
fig, ax = plt.subplots()
ax.imshow([[1]])
prop = "matplotlib.backends._macosx.choose_save_file"

Check warning on line 54 in lib/matplotlib/tests/test_backend_macosx.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_backend_macosx.py#L52-L54

Added lines #L52 - L54 were not covered by tests
with mock.patch(prop, return_value="foobar.png"):
fname = fig.canvas.manager.toolbar.save_figure()
os.remove("foobar.png")
assert fname == "foobar.png"

Check warning on line 58 in lib/matplotlib/tests/test_backend_macosx.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/tests/test_backend_macosx.py#L56-L58

Added lines #L56 - L58 were not covered by tests
11 changes: 11 additions & 0 deletions lib/matplotlib/tests/test_backend_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,17 @@ def test_figureoptions():
fig.canvas.manager.toolbar.edit_parameters()


@pytest.mark.backend('QtAgg', skip_on_importerror=True)
def test_save_figure_return():
fig, ax = plt.subplots()
ax.imshow([[1]])
prop = "matplotlib.backends.qt_compat.QtWidgets.QFileDialog.getSaveFileName"
with mock.patch(prop, return_value=("foobar.png", None)):
fname = fig.canvas.manager.toolbar.save_figure()
os.remove("foobar.png")
assert fname == "foobar.png"


@pytest.mark.backend('QtAgg', skip_on_importerror=True)
def test_figureoptions_with_datetime_axes():
fig, ax = plt.subplots()
Expand Down

0 comments on commit 62fe3f4

Please sign in to comment.