|
7 | 7 | import os |
8 | 8 | import warnings |
9 | 9 |
|
| 10 | +import json |
| 11 | + |
10 | 12 | from branca.element import Element |
11 | 13 |
|
12 | 14 | import folium |
@@ -114,94 +116,43 @@ def test_color_line(): |
114 | 116 | m._repr_html_() |
115 | 117 |
|
116 | 118 |
|
117 | | -def test_get_vegalite_major_version(): |
118 | | - spec_v2 = {'$schema': 'https://vega.github.io/schema/vega-lite/v2.6.0.json', |
119 | | - 'config': {'view': {'height': 300, 'width': 400}}, |
120 | | - 'data': {'name': 'data-aac17e868e23f98b5e0830d45504be45'}, |
121 | | - 'datasets': {'data-aac17e868e23f98b5e0830d45504be45': [{'folium usage': 0, |
122 | | - 'happiness': 1.0}, |
123 | | - {'folium usage': 1, |
124 | | - 'happiness': 2.718281828459045}, |
125 | | - {'folium usage': 2, |
126 | | - 'happiness': 7.38905609893065}, |
127 | | - {'folium usage': 3, |
128 | | - 'happiness': 20.085536923187668}, |
129 | | - {'folium usage': 4, |
130 | | - 'happiness': 54.598150033144236}, |
131 | | - {'folium usage': 5, |
132 | | - 'happiness': 148.4131591025766}, |
133 | | - {'folium usage': 6, |
134 | | - 'happiness': 403.4287934927351}, |
135 | | - {'folium usage': 7, |
136 | | - 'happiness': 1096.6331584284585}, |
137 | | - {'folium usage': 8, |
138 | | - 'happiness': 2980.9579870417283}, |
139 | | - {'folium usage': 9, |
140 | | - 'happiness': 8103.083927575384}]}, |
141 | | - 'encoding': {'x': {'field': 'folium usage', 'type': 'quantitative'}, |
142 | | - 'y': {'field': 'happiness', 'type': 'quantitative'}}, |
143 | | - 'mark': 'point'} |
144 | | - |
145 | | - vegalite_v2 = folium.features.VegaLite(spec_v2) |
146 | | - |
147 | | - assert vegalite_v2._get_vegalite_major_versions(spec_v2) == '2' |
148 | | - |
149 | | - spec_v1 = {'$schema': 'https://vega.github.io/schema/vega-lite/v1.3.1.json', |
150 | | - 'data': {'values': [{'folium usage': 0, 'happiness': 1.0}, |
151 | | - {'folium usage': 1, 'happiness': 2.718281828459045}, |
152 | | - {'folium usage': 2, 'happiness': 7.38905609893065}, |
153 | | - {'folium usage': 3, 'happiness': 20.085536923187668}, |
154 | | - {'folium usage': 4, 'happiness': 54.598150033144236}, |
155 | | - {'folium usage': 5, 'happiness': 148.4131591025766}, |
156 | | - {'folium usage': 6, 'happiness': 403.4287934927351}, |
157 | | - {'folium usage': 7, 'happiness': 1096.6331584284585}, |
158 | | - {'folium usage': 8, 'happiness': 2980.9579870417283}, |
159 | | - {'folium usage': 9, 'happiness': 8103.083927575384}]}, |
160 | | - 'encoding': {'x': {'field': 'folium usage', 'type': 'quantitative'}, |
161 | | - 'y': {'field': 'happiness', 'type': 'quantitative'}}, |
162 | | - 'height': 300, |
163 | | - 'mark': 'point', |
164 | | - 'width': 400} |
165 | | - |
166 | | - vegalite_v1 = folium.features.VegaLite(spec_v1) |
167 | | - |
168 | | - assert vegalite_v1._get_vegalite_major_versions(spec_v1) == '1' |
169 | | - |
170 | | - spec_no_version = { |
171 | | - 'config': { |
172 | | - 'view': {'height': 300, 'width': 400}}, |
173 | | - 'data': {'name': 'data-aac17e868e23f98b5e0830d45504be45'}, |
174 | | - 'datasets': { |
175 | | - 'data-aac17e868e23f98b5e0830d45504be45': [ |
176 | | - {'folium usage': 0, |
177 | | - 'happiness': 1.0}, |
178 | | - {'folium usage': 1, |
179 | | - 'happiness': 2.718281828459045}, |
180 | | - {'folium usage': 2, |
181 | | - 'happiness': 7.38905609893065}, |
182 | | - {'folium usage': 3, |
183 | | - 'happiness': 20.085536923187668}, |
184 | | - {'folium usage': 4, |
185 | | - 'happiness': 54.598150033144236}, |
186 | | - {'folium usage': 5, |
187 | | - 'happiness': 148.4131591025766}, |
188 | | - {'folium usage': 6, |
189 | | - 'happiness': 403.4287934927351}, |
190 | | - {'folium usage': 7, |
191 | | - 'happiness': 1096.6331584284585}, |
192 | | - {'folium usage': 8, |
193 | | - 'happiness': 2980.9579870417283}, |
194 | | - {'folium usage': 9, |
195 | | - 'happiness': 8103.083927575384}]}, |
196 | | - 'encoding': {'x': {'field': 'folium usage', 'type': 'quantitative'}, |
197 | | - 'y': {'field': 'happiness', 'type': 'quantitative'}}, |
198 | | - 'mark': 'point' |
199 | | - } |
200 | | - |
201 | | - vegalite_no_version = folium.features.VegaLite(spec_no_version) |
202 | | - |
203 | | - assert vegalite_no_version._get_vegalite_major_versions(spec_no_version) is None |
204 | | - |
| 119 | +@pytest.fixture |
| 120 | +def vegalite_spec(version): |
| 121 | + file_version = 'v1' if version == 1 else 'vlater' |
| 122 | + file = os.path.join(rootpath, 'vegalite_data', f'vegalite_{file_version}.json') |
| 123 | + |
| 124 | + if not os.path.exists(file): |
| 125 | + raise FileNotFoundError(f'The vegalite data {file} does not exist.') |
| 126 | + |
| 127 | + with open(file, 'r') as f: |
| 128 | + spec = json.load(f) |
| 129 | + |
| 130 | + if version is None or '$schema' in spec: |
| 131 | + return spec |
| 132 | + |
| 133 | + # Sample versions that might show up |
| 134 | + schema_version = { |
| 135 | + 2: 'v2.6.0', |
| 136 | + 3: 'v3.6.0', |
| 137 | + 4: 'v4.6.0', |
| 138 | + 5: 'v5.1.0' |
| 139 | + }[version] |
| 140 | + spec['$schema'] = f'https://vega.github.io/schema/vega-lite/{schema_version}.json' |
| 141 | + |
| 142 | + return spec |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.parametrize( |
| 146 | + 'version', |
| 147 | + [1, 2, 3, 4, 5, None] |
| 148 | +) |
| 149 | +def test_vegalite_major_version(vegalite_spec, version): |
| 150 | + vegalite = folium.features.VegaLite(vegalite_spec) |
| 151 | + |
| 152 | + if version is None: |
| 153 | + assert vegalite.vegalite_major_version is None |
| 154 | + else: |
| 155 | + assert vegalite.vegalite_major_version == version |
205 | 156 |
|
206 | 157 | # GeoJsonTooltip GeometryCollection |
207 | 158 | def test_geojson_tooltip(): |
|
0 commit comments