1
1
import asyncio
2
2
import re
3
- from unittest .mock import patch
3
+ from unittest .mock import AsyncMock , patch
4
4
5
5
import pytest
6
- from hypothesis import HealthCheck , Phase , given , settings
6
+ from hypothesis import HealthCheck , given , settings
7
7
from hypothesis import strategies as st
8
8
9
9
from kaleido import Kaleido
@@ -180,18 +180,14 @@ async def test_write_fig_from_object_bare_dictionary(
180
180
)
181
181
182
182
183
- # In the refactor, all figure generation methods are really just wrappers
184
- # for the most flexible, tested above, generate_fig_from_object.
185
- # So we test that one, and then test to make sure its receiving arguments
186
- # properly for the other tests.
183
+ @pytest .fixture (scope = "function" )
184
+ def test_kaleido ():
185
+ return Kaleido ()
187
186
188
187
189
- # Uncomment these settings after refactor.
190
- # @settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
191
188
@settings (
192
- phases = [Phase .generate ],
193
- max_examples = 1 ,
194
189
suppress_health_check = [HealthCheck .function_scoped_fixture ],
190
+ max_examples = 50 ,
195
191
)
196
192
@given (
197
193
path = st .text (
@@ -204,41 +200,51 @@ async def test_write_fig_from_object_bare_dictionary(
204
200
format_type = st .sampled_from (["png" , "svg" , "pdf" , "html" ]),
205
201
topojson = st .one_of (st .none (), st .text (min_size = 1 , max_size = 20 )),
206
202
)
203
+ @pytest .mark .parametrize (
204
+ "cancel_on_error" ,
205
+ [
206
+ True ,
207
+ False ,
208
+ ],
209
+ ids = ("cancel_on_error" , "collect_errors" ),
210
+ )
207
211
async def test_write_fig_argument_passthrough ( # noqa: PLR0913
208
- simple_figure_with_bytes ,
212
+ test_kaleido ,
213
+ cancel_on_error ,
209
214
tmp_path ,
210
215
path ,
211
216
width ,
212
217
height ,
213
218
format_type ,
214
219
topojson ,
215
220
):
216
- """Test that write_fig properly passes arguments to write_fig_from_object."""
217
- pytest .skip ("Remove this failure line and the comment above after the refactor!" )
218
221
test_path = tmp_path / f"{ path } .{ format_type } "
219
222
opts = {"format" : format_type , "width" : width , "height" : height }
220
-
223
+ fig = { "data" : "test" }
221
224
# Mock write_fig_from_object to capture arguments
222
- with patch .object (Kaleido , "write_fig_from_object" ) as mock_write_fig_from_object :
223
- async with Kaleido () as k :
224
- await k .write_fig (
225
- simple_figure_with_bytes ["fig" ],
226
- path = test_path ,
227
- opts = opts ,
228
- topojson = topojson ,
229
- )
230
-
225
+ with patch .object (
226
+ Kaleido ,
227
+ "write_fig_from_object" ,
228
+ new = AsyncMock (return_value = []),
229
+ ) as mock_write_fig_from_object :
230
+ await test_kaleido .write_fig (
231
+ fig ,
232
+ path = test_path ,
233
+ opts = opts ,
234
+ topojson = topojson ,
235
+ cancel_on_error = cancel_on_error ,
236
+ )
231
237
# Verify write_fig_from_object was called
232
238
mock_write_fig_from_object .assert_called_once ()
233
239
234
240
# Extract the generator that was passed as first argument
235
- args , _kwargs = mock_write_fig_from_object .call_args # not sure.
236
- assert len (args ) == 1 , "Expected exactly one argument (the generator)"
241
+ _ , kwargs = mock_write_fig_from_object .call_args # not sure.
237
242
238
- generator = args [0 ]
243
+ generator = kwargs ["fig_dicts" ]
244
+ assert kwargs ["cancel_on_error" ] == cancel_on_error
239
245
240
246
# Convert generator to list to inspect its contents
241
- generated_args_list = list ( generator )
247
+ generated_args_list = [ v async for v in generator ]
242
248
assert len (generated_args_list ) == 1 , (
243
249
"Expected generator to yield exactly one item"
244
250
)
@@ -252,9 +258,7 @@ async def test_write_fig_argument_passthrough( # noqa: PLR0913
252
258
assert "topojson" in generated_args , "Generated args should contain 'topojson'"
253
259
254
260
# Check that the values match
255
- assert generated_args ["fig" ] == simple_figure_with_bytes ["fig" ], (
256
- "Figure should match"
257
- )
261
+ assert generated_args ["fig" ] == fig , "Figure should match"
258
262
assert str (generated_args ["path" ]) == str (test_path ), "Path should match"
259
263
assert generated_args ["opts" ] == opts , "Options should match"
260
264
assert generated_args ["topojson" ] == topojson , "Topojson should match"
0 commit comments