Skip to content

Commit 02d5149

Browse files
committed
Add coverage tests
1 parent 9a72a85 commit 02d5149

File tree

4 files changed

+24
-31
lines changed

4 files changed

+24
-31
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
branch = true
33
source =
44
mplaltair
5+
omit = *tests*
56

67
[report]
78
exclude_lines =

mplaltair/__init__.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
import matplotlib
22
import altair
33

4+
from ._convert import _convert
45

5-
# TODO rename this?
6-
def convert(encoding, *, figure=None):
6+
def convert(chart):
77
"""Convert an altair encoding to a Matplotlib figure
88
99
1010
Parameters
1111
----------
12-
encoding
13-
The Altair encoding of the plot.
14-
15-
figure : matplotib.figure.Figure, optional
16-
# TODO: generalize this to 'thing that supports gridspec slicing?
12+
chart
13+
The Altair chart object generated by Altair
1714
1815
Returns
1916
-------
20-
figure : matplotlib.figure.Figure
21-
The Figure with all artists in it (ready to be saved or shown)
22-
23-
mapping : dict
24-
Mapping from parts of the encoding to the Matplotlib artists. This is
25-
for later customization.
26-
17+
mapping: dict
18+
Mapping from parts of the encoding to the Matplotlib artists.
19+
This is for later customization
2720
2821
"""
29-
if figure is None:
30-
from matplotlib import pyplot as plt
31-
figure = plt.figure()
32-
33-
mapping = {}
34-
35-
return figure, mapping
22+
return _convert(chart)

mplaltair/_convert.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _process_color(dtype, data):
4040
raise NotImplementedError
4141
elif dtype == 'ordinal':
4242
return ('c', data)
43-
elif dtype == 'temporal':
43+
else: # temporal
4444
return ('c', data)
4545

4646

@@ -93,7 +93,7 @@ def _process_stroke(dtype, data):
9393
'stroke': _process_stroke,
9494
}
9595

96-
def convert(chart):
96+
def _convert(chart):
9797
"""Convert an altair encoding to a Matplotlib figure
9898
9999
@@ -123,7 +123,7 @@ def convert(chart):
123123
if dtype == 'temporal':
124124
try:
125125
data = mdates.date2num(data) # Convert dates to Matplotlib dates
126-
except ValueError:
126+
except AttributeError:
127127
raise
128128
mapping[_mappings[channel](dtype, data)[0]] = _mappings[channel](dtype, data)[1]
129129

mplaltair/tests/test_convert.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import matplotlib.pyplot as plt
66
import matplotlib.dates as mdates
77

8-
from .._convert import convert
8+
from mplaltair import convert
99

1010

1111
df = pd.DataFrame({
@@ -32,6 +32,11 @@ def test_invalid_encodings():
3232
with pytest.raises(ValueError):
3333
convert(chart_spec)
3434

35+
@pytest.mark.xfail(raises=AttributeError)
36+
def test_invalid_temporal():
37+
chart = alt.Chart(df).mark_point().encode(alt.X('quant:T'))
38+
convert(chart)
39+
3540
@pytest.mark.parametrize('channel', ['quant', 'ord', 'nom'])
3641
def test_convert_x_success(channel):
3742
chart_spec = alt.Chart(df).encode(x=channel).mark_point()
@@ -77,9 +82,9 @@ def test_convert_x2_y2_fail_temporal(column):
7782
chart = alt.Chart(df).mark_point().encode(alt.X2(column), alt.Y2(column))
7883
convert(chart)
7984

80-
@pytest.mark.parametrize('channel', ['quant', 'ord'])
81-
def test_convert_color_success(channel):
82-
chart_spec = alt.Chart(df).encode(color=channel).mark_point()
85+
@pytest.mark.parametrize('channel,dtype', [('quant','quantitative'), ('ord','ordinal')])
86+
def test_convert_color_success(channel, dtype):
87+
chart_spec = alt.Chart(df).encode(color=alt.Color(field=channel, type=dtype)).mark_point()
8388
mapping = convert(chart_spec)
8489
assert list(mapping['c']) == list(df[channel].values)
8590

@@ -151,9 +156,9 @@ def test_convert_opacity_fail_temporal(column):
151156
chart = alt.Chart(df).mark_point().encode(alt.Opacity(column))
152157
convert(chart)
153158

154-
@pytest.mark.parametrize('channel', ['quant', 'ord'])
155-
def test_convert_size_success(channel):
156-
chart_spec = alt.Chart(df).encode(size=channel).mark_point()
159+
@pytest.mark.parametrize('channel,dtype', [('quant','quantitative'), ('ord', 'ordinal')])
160+
def test_convert_size_success(channel, dtype):
161+
chart_spec = alt.Chart(df).encode(size=alt.Size(field=channel, type=dtype)).mark_point()
157162
mapping = convert(chart_spec)
158163
assert list(mapping['s']) == list(df[channel].values)
159164

0 commit comments

Comments
 (0)