Skip to content

Commit

Permalink
Add test for layer properties (#2811)
Browse files Browse the repository at this point in the history
* Add test for layer properties

* Update api.py

Previous version worked for numbers but not for objects.
  • Loading branch information
ChristopherDavisUCI committed Jan 7, 2023
1 parent 6778e3c commit ecf726d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions altair/vegalite/v5/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3042,14 +3042,14 @@ def remove_prop(subchart, prop):
pass
if len(values) == 0:
pass
elif all(v is values[0] for v in values[1:]):
elif all(v == values[0] for v in values[1:]):
output_dict[prop] = values[0]
else:
raise ValueError(f"There are inconsistent values {values} for {prop}")
else:
# Top level has this prop; subchart props must be either
# Undefined or identical to proceed.
if all(c[prop] is Undefined or c[prop] is chart[prop] for c in subcharts):
if all(c[prop] is Undefined or c[prop] == chart[prop] for c in subcharts):
output_dict[prop] = chart[prop]
else:
raise ValueError(f"There are inconsistent values {values} for {prop}")
Expand Down
23 changes: 23 additions & 0 deletions tests/vegalite/v5/tests/test_layer_props.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pytest

import altair.vegalite.v5 as alt


def test_layer_props():
"""Beginning in Vega-Lite v5, the properties "height" and "width" were no longer allowed in a subchart within a LayerChart. We check here that these are moved to the top level by Altair."""
base = alt.Chart().mark_point()

# Allowed
base.properties(width=100) + base
base.properties(width=100) + base.properties(height=200)
base.properties(width=100) + base.properties(height=200, width=100)

# Not allowed
with pytest.raises(ValueError, match="inconsistent"):
base.properties(width=100) + base.properties(width=200)

# Check that the resulting LayerChart has the correct properties.
c = base.properties(width=100) + base.properties(height=200, width=100)
assert isinstance(c, alt.LayerChart)
assert c.width == 100
assert c.height == 200

0 comments on commit ecf726d

Please sign in to comment.