Skip to content

Commit 0820b0b

Browse files
committed
Add tests for calc_fig passthrough.
1 parent d31ee8a commit 0820b0b

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

src/py/tests/test_kaleido.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ async def test_write_fig_from_object_bare_dictionary(
185185

186186

187187
@pytest.fixture(scope="function")
188-
def test_kaleido():
188+
def test_kaleido(): # speed up hypothesis test using a function fixture
189189
return Kaleido()
190190

191191

@@ -268,6 +268,65 @@ async def test_write_fig_argument_passthrough( # noqa: PLR0913
268268
assert generated_args["topojson"] == topojson, "Topojson should match"
269269

270270

271+
@settings(
272+
suppress_health_check=[HealthCheck.function_scoped_fixture],
273+
max_examples=50,
274+
)
275+
@given(
276+
width=st.integers(min_value=100, max_value=2000),
277+
height=st.integers(min_value=100, max_value=2000),
278+
format_type=st.sampled_from(["png", "svg", "pdf", "html"]),
279+
topojson=st.one_of(st.none(), st.text(min_size=1, max_size=20)),
280+
)
281+
async def test_calc_fig_argument_passthrough(
282+
test_kaleido,
283+
width,
284+
height,
285+
format_type,
286+
topojson,
287+
):
288+
opts = {"format": format_type, "width": width, "height": height}
289+
fig = {"data": "test"}
290+
# Mock write_fig_from_object to capture arguments
291+
with patch.object(
292+
Kaleido,
293+
"write_fig_from_object",
294+
new=AsyncMock(return_value=[]),
295+
) as mock_write_fig_from_object:
296+
await test_kaleido.calc_fig(
297+
fig,
298+
opts=opts,
299+
topojson=topojson,
300+
)
301+
# Verify write_fig_from_object was called
302+
mock_write_fig_from_object.assert_called_once()
303+
304+
# Extract the generator that was passed as first argument
305+
_, kwargs = mock_write_fig_from_object.call_args # not sure.
306+
307+
generator = kwargs["fig_dicts"]
308+
assert kwargs["cancel_on_error"] is True
309+
assert kwargs["_write"] is False
310+
311+
# Convert generator to list to inspect its contents
312+
generated_args_list = [v async for v in generator]
313+
assert len(generated_args_list) == 1, (
314+
"Expected generator to yield exactly one item"
315+
)
316+
317+
generated_args = generated_args_list[0]
318+
319+
# Validate that the generated arguments match what we passed to write_fig
320+
assert "fig" in generated_args, "Generated args should contain 'fig'"
321+
assert "opts" in generated_args, "Generated args should contain 'opts'"
322+
assert "topojson" in generated_args, "Generated args should contain 'topojson'"
323+
324+
# Check that the values match
325+
assert generated_args["fig"] == fig, "Figure should match"
326+
assert generated_args["opts"] == opts, "Options should match"
327+
assert generated_args["topojson"] == topojson, "Topojson should match"
328+
329+
271330
async def test_kaleido_instantiate_no_hang():
272331
"""Test that instantiating Kaleido doesn't hang."""
273332
_ = Kaleido()

0 commit comments

Comments
 (0)