File size: 2,602 Bytes
ba2f5d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""Tests of functionality that should work in all vegalite versions"""

import pytest

import pandas as pd

from .. import v3, v4


@pytest.fixture
def basic_spec():
    return {
        "data": {"url": "data.csv"},
        "mark": "line",
        "encoding": {
            "color": {"type": "nominal", "field": "color"},
            "x": {"type": "quantitative", "field": "xval"},
            "y": {"type": "ordinal", "field": "yval"},
        },
    }


def make_final_spec(alt, basic_spec):
    theme = alt.themes.get()
    spec = theme()
    spec.update(basic_spec)
    return spec


def make_basic_chart(alt):
    data = pd.DataFrame(
        {
            "a": ["A", "B", "C", "D", "E", "F", "G", "H", "I"],
            "b": [28, 55, 43, 91, 81, 53, 19, 87, 52],
        }
    )

    return alt.Chart(data).mark_bar().encode(x="a", y="b")


@pytest.mark.parametrize("alt", [v3, v4])
def test_basic_chart_to_dict(alt, basic_spec):
    chart = (
        alt.Chart("data.csv")
        .mark_line()
        .encode(alt.X("xval:Q"), y=alt.Y("yval:O"), color="color:N")
    )
    dct = chart.to_dict()

    # schema should be in the top level
    assert dct.pop("$schema").startswith("http")

    # remainder of spec should match the basic spec
    assert dct == make_final_spec(alt, basic_spec)


@pytest.mark.parametrize("alt", [v3, v4])
def test_basic_chart_from_dict(alt, basic_spec):
    chart = alt.Chart.from_dict(basic_spec)
    dct = chart.to_dict()

    # schema should be in the top level
    assert dct.pop("$schema").startswith("http")

    # remainder of spec should match the basic spec
    assert dct == make_final_spec(alt, basic_spec)


@pytest.mark.parametrize("alt", [v3, v4])
def test_theme_enable(alt, basic_spec):
    active_theme = alt.themes.active

    try:
        alt.themes.enable("none")

        chart = alt.Chart.from_dict(basic_spec)
        dct = chart.to_dict()

        # schema should be in the top level
        assert dct.pop("$schema").startswith("http")

        # remainder of spec should match the basic spec
        # without any theme settings
        assert dct == basic_spec
    finally:
        # reset the theme to its initial value
        alt.themes.enable(active_theme)


@pytest.mark.parametrize("alt", [v3, v4])
def test_max_rows(alt):
    basic_chart = make_basic_chart(alt)

    with alt.data_transformers.enable("default"):
        basic_chart.to_dict()  # this should not fail

    with alt.data_transformers.enable("default", max_rows=5):
        with pytest.raises(alt.MaxRowsError):
            basic_chart.to_dict()  # this should not fail