def test_selection(): # test instantiation of selections interval = alt.selection_interval(name="selec_1") assert interval.param.select.type == "interval" assert interval.name == "selec_1" single = alt.selection_single(name="selec_2") assert single.param.select.type == "point" assert single.name == "selec_2" multi = alt.selection_multi(name="selec_3") assert multi.param.select.type == "point" assert multi.name == "selec_3" # test adding to chart chart = alt.Chart().add_selection(single) chart = chart.add_selection(multi, interval) assert set(x.name for x in chart.params) == {"selec_1", "selec_2", "selec_3"} # test logical operations assert isinstance(single & multi, alt.SelectionPredicateComposition) assert isinstance(single | multi, alt.SelectionPredicateComposition) assert isinstance(~single, alt.SelectionPredicateComposition) assert "and" in (single & multi).to_dict().keys() assert "or" in (single | multi).to_dict().keys() assert "not" in (~single).to_dict().keys() # test that default names increment (regression for #1454) sel1 = alt.selection_single() sel2 = alt.selection_multi() sel3 = alt.selection_interval() names = {s.name for s in (sel1, sel2, sel3)} assert len(names) == 3
def test_compound_add_selections(charttype): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = charttype(base.add_selection(selection), base.add_selection(selection)) chart2 = charttype(base, base).add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
def test_add_selection(): selections = [ alt.selection_interval(), alt.selection_single(), alt.selection_multi(), ] chart = (alt.Chart().mark_point().add_selection( selections[0]).add_selection(selections[1], selections[2])) expected = [s.param for s in selections] assert chart.params == expected
def test_selection_expression(): selection = alt.selection_single(fields=["value"]) assert isinstance(selection.value, alt.SelectionExpression) assert selection.value.to_dict() == {"expr": f"{selection.name}.value"} assert isinstance(selection["value"], alt.expr.Expression) assert selection["value"].to_dict() == "{0}['value']".format( selection.name) with pytest.raises(AttributeError): selection.__magic__
def test_layer_add_selection(): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = alt.layer(base.add_selection(selection), base) chart2 = alt.layer(base, base).add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
def test_facet_add_selections(): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = base.add_selection(selection).facet("val:Q") chart2 = base.facet("val:Q").add_selection(selection) assert chart1.to_dict() == chart2.to_dict()
def test_repeat_add_selections(): base = alt.Chart("data.csv").mark_point() selection = alt.selection_single() chart1 = base.add_selection(selection).repeat(list("ABC")) chart2 = base.repeat(list("ABC")).add_selection(selection) assert chart1.to_dict() == chart2.to_dict()