|
7 | 7 | from chartlets.components import Plot |
8 | 8 |
|
9 | 9 |
|
10 | | -class PlotTest(unittest.TestCase): |
11 | | - def setUp(self): |
12 | | - dataset = pd.DataFrame( |
| 10 | +from tests.component_test import make_base |
| 11 | + |
| 12 | + |
| 13 | +class PlotTest(make_base(Plot)): |
| 14 | + |
| 15 | + def test_is_json_serializable(self): |
| 16 | + source = pd.DataFrame( |
13 | 17 | {"x": ["A", "B", "C", "D", "E"], "a": [28, 55, 43, 91, 81]} |
14 | 18 | ) |
15 | 19 | self.chart = ( |
16 | | - alt.Chart(dataset) |
| 20 | + alt.Chart(source) |
17 | 21 | .mark_bar() |
18 | 22 | .encode( |
19 | 23 | x=alt.X("x:N", title="x"), |
20 | 24 | y=alt.Y("a:Q", title="a"), |
21 | 25 | ) |
22 | 26 | ) |
23 | 27 |
|
24 | | - def test_is_json_serializable(self): |
25 | | - |
26 | | - plot = Plot(id="plot", chart=self.chart) |
27 | | - p = plot.to_dict() |
28 | | - |
29 | | - print(p) |
30 | | - self.assertIsInstance(p, dict) |
31 | | - self.assertEqual(p["type"], "Plot") |
32 | | - self.assertIsInstance(p["chart"], dict) |
33 | | - self.assertEqual(p["chart"]["mark"], {"type": "bar"}) |
34 | | - json_text = json.dumps(p) |
35 | | - self.assertEqual("{", json_text[0]) |
36 | | - self.assertEqual("}", json_text[-1]) |
| 28 | + self.assert_is_json_serializable( |
| 29 | + self.cls(id="plot", chart=self.chart), |
| 30 | + { |
| 31 | + "chart": { |
| 32 | + "$schema": "https://vega.github.io/schema/vega-lite/v5.20.1.json", |
| 33 | + "config": { |
| 34 | + "view": {"continuousHeight": 300, "continuousWidth": 300} |
| 35 | + }, |
| 36 | + "data": {"name": "data-2780b27b376c14369bf3f449cf25f092"}, |
| 37 | + "datasets": { |
| 38 | + "data-2780b27b376c14369bf3f449cf25f092": [ |
| 39 | + {"a": 28, "x": "A"}, |
| 40 | + {"a": 55, "x": "B"}, |
| 41 | + {"a": 43, "x": "C"}, |
| 42 | + {"a": 91, "x": "D"}, |
| 43 | + {"a": 81, "x": "E"}, |
| 44 | + ] |
| 45 | + }, |
| 46 | + "encoding": { |
| 47 | + "x": {"field": "x", "title": "x", "type": "nominal"}, |
| 48 | + "y": {"field": "a", "title": "a", "type": "quantitative"}, |
| 49 | + }, |
| 50 | + "mark": {"type": "bar"}, |
| 51 | + }, |
| 52 | + "id": "plot", |
| 53 | + "type": "Plot", |
| 54 | + }, |
| 55 | + ) |
0 commit comments