Skip to content

Commit a982059

Browse files
committed
Improve test coverage
1 parent a15000d commit a982059

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

seaborn/_core/plot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -679,13 +679,13 @@ def theme(self, *args: dict[str, Any]) -> Plot:
679679

680680
return new
681681

682-
def save(self, fname, **kwargs) -> Plot:
682+
def save(self, loc, **kwargs) -> Plot:
683683
"""
684684
Compile the plot and write it to a buffer or file on disk.
685685
686686
Parameters
687687
----------
688-
fname : str, path, or buffer
688+
loc : str, path, or buffer
689689
Location on disk to save the figure, or a buffer to write into.
690690
kwargs
691691
Other keyword arguments are passed through to
@@ -694,7 +694,7 @@ def save(self, fname, **kwargs) -> Plot:
694694
"""
695695
# TODO expose important keyword arguments in our signature?
696696
with theme_context(self._theme_with_defaults()):
697-
self._plot().save(fname, **kwargs)
697+
self._plot().save(loc, **kwargs)
698698
return self
699699

700700
def show(self, **kwargs) -> None:

tests/_core/test_plot.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import io
2+
import xml
13
import functools
24
import itertools
35
import warnings
4-
import imghdr
56

67
import numpy as np
78
import pandas as pd
89
import matplotlib as mpl
910
import matplotlib.pyplot as plt
11+
from PIL import Image
1012

1113
import pytest
1214
from pandas.testing import assert_frame_equal, assert_series_equal
@@ -864,12 +866,18 @@ def test_theme_default(self):
864866
p = Plot().plot()
865867
assert mpl.colors.same_color(p._figure.axes[0].get_facecolor(), "#EAEAF2")
866868

867-
def test_theme(self):
869+
def test_theme_params(self):
868870

869871
color = "r"
870872
p = Plot().theme({"axes.facecolor": color}).plot()
871873
assert mpl.colors.same_color(p._figure.axes[0].get_facecolor(), color)
872874

875+
def test_theme_error(self):
876+
877+
p = Plot()
878+
with pytest.raises(TypeError, match=r"theme\(\) takes 1 positional"):
879+
p.theme("arg1", "arg2")
880+
873881
def test_move(self, long_df):
874882

875883
orig_df = long_df.copy(deep=True)
@@ -960,21 +968,31 @@ def test_show(self):
960968
if not gui_backend:
961969
assert msg
962970

963-
def test_png_representation(self):
971+
def test_png_repr(self):
964972

965973
p = Plot()
966974
data, metadata = p._repr_png_()
975+
img = Image.open(io.BytesIO(data))
967976

968977
assert not hasattr(p, "_figure")
969978
assert isinstance(data, bytes)
970-
assert imghdr.what("", data) == "png"
979+
assert img.format == "PNG"
971980
assert sorted(metadata) == ["height", "width"]
972981
# TODO test retina scaling
973982

974-
@pytest.mark.xfail(reason="Plot.save not yet implemented")
975983
def test_save(self):
976984

977-
Plot().save()
985+
buf = io.BytesIO()
986+
987+
p = Plot().save(buf)
988+
assert isinstance(p, Plot)
989+
img = Image.open(buf)
990+
assert img.format == "PNG"
991+
992+
buf = io.StringIO()
993+
Plot().save(buf, format="svg")
994+
tag = xml.etree.ElementTree.fromstring(buf.getvalue()).tag
995+
assert tag == "{http://www.w3.org/2000/svg}svg"
978996

979997
def test_on_axes(self):
980998

0 commit comments

Comments
 (0)