def test_selection(): # test instantiation of selections interval = alt.selection_interval(name="selec_1") assert interval.selection.type == "interval" assert interval.name == "selec_1" single = alt.selection_single(name="selec_2") assert single.selection.type == "single" assert single.name == "selec_2" multi = alt.selection_multi(name="selec_3") assert multi.selection.type == "multi" assert multi.name == "selec_3" # test adding to chart chart = alt.Chart().add_selection(single) chart = chart.add_selection(multi, interval) assert set(chart.selection.keys()) == {"selec_1", "selec_2", "selec_3"} # test logical operations assert isinstance(single & multi, alt.Selection) assert isinstance(single | multi, alt.Selection) assert isinstance(~single, alt.Selection) assert isinstance((single & multi)[0].group, alt.SelectionAnd) assert isinstance((single | multi)[0].group, alt.SelectionOr) assert isinstance((~single)[0].group, alt.SelectionNot) # 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_selection_expression(): selection = alt.selection_single(fields=['value']) assert isinstance(selection.value, alt.expr.Expression) assert selection.value.to_dict() == '{0}.value'.format(selection.name) assert isinstance(selection['value'], alt.expr.Expression) assert selection['value'].to_dict() == "{0}['value']".format(selection.name)
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.name: s.selection for s in selections} assert chart.selection == expected
def test_selection_expression(): selection = alt.selection_single(fields=["value"]) assert isinstance(selection.value, alt.expr.Expression) assert selection.value.to_dict() == "{0}.value".format(selection.name) 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()