1"""Tests of functionality that should work in all vegalite versions""" 2 3import pytest 4 5import pandas as pd 6 7from .. import v3, v4 8 9 10@pytest.fixture 11def basic_spec(): 12 return { 13 "data": {"url": "data.csv"}, 14 "mark": "line", 15 "encoding": { 16 "color": {"type": "nominal", "field": "color"}, 17 "x": {"type": "quantitative", "field": "xval"}, 18 "y": {"type": "ordinal", "field": "yval"}, 19 }, 20 } 21 22 23def make_final_spec(alt, basic_spec): 24 theme = alt.themes.get() 25 spec = theme() 26 spec.update(basic_spec) 27 return spec 28 29 30def make_basic_chart(alt): 31 data = pd.DataFrame( 32 { 33 "a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"], 34 "b": [28, 55, 43, 91, 81, 53, 19, 87, 52], 35 } 36 ) 37 38 return alt.Chart(data).mark_bar().encode(x="a", y="b") 39 40 41@pytest.mark.parametrize("alt", [v3, v4]) 42def test_basic_chart_to_dict(alt, basic_spec): 43 chart = ( 44 alt.Chart("data.csv") 45 .mark_line() 46 .encode(alt.X("xval:Q"), y=alt.Y("yval:O"), color="color:N") 47 ) 48 dct = chart.to_dict() 49 50 # schema should be in the top level 51 assert dct.pop("$schema").startswith("http") 52 53 # remainder of spec should match the basic spec 54 assert dct == make_final_spec(alt, basic_spec) 55 56 57@pytest.mark.parametrize("alt", [v3, v4]) 58def test_basic_chart_from_dict(alt, basic_spec): 59 chart = alt.Chart.from_dict(basic_spec) 60 dct = chart.to_dict() 61 62 # schema should be in the top level 63 assert dct.pop("$schema").startswith("http") 64 65 # remainder of spec should match the basic spec 66 assert dct == make_final_spec(alt, basic_spec) 67 68 69@pytest.mark.parametrize("alt", [v3, v4]) 70def test_theme_enable(alt, basic_spec): 71 active_theme = alt.themes.active 72 73 try: 74 alt.themes.enable("none") 75 76 chart = alt.Chart.from_dict(basic_spec) 77 dct = chart.to_dict() 78 79 # schema should be in the top level 80 assert dct.pop("$schema").startswith("http") 81 82 # remainder of spec should match the basic spec 83 # without any theme settings 84 assert dct == basic_spec 85 finally: 86 # reset the theme to its initial value 87 alt.themes.enable(active_theme) 88 89 90@pytest.mark.parametrize("alt", [v3, v4]) 91def test_max_rows(alt): 92 basic_chart = make_basic_chart(alt) 93 94 with alt.data_transformers.enable("default"): 95 basic_chart.to_dict() # this should not fail 96 97 with alt.data_transformers.enable("default", max_rows=5): 98 with pytest.raises(alt.MaxRowsError): 99 basic_chart.to_dict() # this should not fail 100