Skip to content

Commit 2fae310

Browse files
committed
Fix up test write_fig->from_object passthrough
1 parent 1bb6b87 commit 2fae310

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/py/kaleido/_fig_tools.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Spec(TypedDict):
6060

6161
# validation function
6262
def is_figurish(o: Any) -> TypeGuard[Figurish]:
63+
# so if data isn't in the dict things get weird.
6364
valid = hasattr(o, "to_dict") or (isinstance(o, dict) and "data" in o)
6465
if not valid:
6566
_logger.debug(

src/py/tests/test_kaleido.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import asyncio
22
import re
3-
from unittest.mock import patch
3+
from unittest.mock import AsyncMock, patch
44

55
import pytest
6-
from hypothesis import HealthCheck, Phase, given, settings
6+
from hypothesis import HealthCheck, given, settings
77
from hypothesis import strategies as st
88

99
from kaleido import Kaleido
@@ -180,18 +180,14 @@ async def test_write_fig_from_object_bare_dictionary(
180180
)
181181

182182

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()
187186

188187

189-
# Uncomment these settings after refactor.
190-
# @settings(suppress_health_check=[HealthCheck.function_scoped_fixture])
191188
@settings(
192-
phases=[Phase.generate],
193-
max_examples=1,
194189
suppress_health_check=[HealthCheck.function_scoped_fixture],
190+
max_examples=50,
195191
)
196192
@given(
197193
path=st.text(
@@ -204,41 +200,51 @@ async def test_write_fig_from_object_bare_dictionary(
204200
format_type=st.sampled_from(["png", "svg", "pdf", "html"]),
205201
topojson=st.one_of(st.none(), st.text(min_size=1, max_size=20)),
206202
)
203+
@pytest.mark.parametrize(
204+
"cancel_on_error",
205+
[
206+
True,
207+
False,
208+
],
209+
ids=("cancel_on_error", "collect_errors"),
210+
)
207211
async def test_write_fig_argument_passthrough( # noqa: PLR0913
208-
simple_figure_with_bytes,
212+
test_kaleido,
213+
cancel_on_error,
209214
tmp_path,
210215
path,
211216
width,
212217
height,
213218
format_type,
214219
topojson,
215220
):
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!")
218221
test_path = tmp_path / f"{path}.{format_type}"
219222
opts = {"format": format_type, "width": width, "height": height}
220-
223+
fig = {"data": "test"}
221224
# 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+
)
231237
# Verify write_fig_from_object was called
232238
mock_write_fig_from_object.assert_called_once()
233239

234240
# 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.
237242

238-
generator = args[0]
243+
generator = kwargs["fig_dicts"]
244+
assert kwargs["cancel_on_error"] == cancel_on_error
239245

240246
# Convert generator to list to inspect its contents
241-
generated_args_list = list(generator)
247+
generated_args_list = [v async for v in generator]
242248
assert len(generated_args_list) == 1, (
243249
"Expected generator to yield exactly one item"
244250
)
@@ -252,9 +258,7 @@ async def test_write_fig_argument_passthrough( # noqa: PLR0913
252258
assert "topojson" in generated_args, "Generated args should contain 'topojson'"
253259

254260
# 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"
258262
assert str(generated_args["path"]) == str(test_path), "Path should match"
259263
assert generated_args["opts"] == opts, "Options should match"
260264
assert generated_args["topojson"] == topojson, "Topojson should match"

0 commit comments

Comments
 (0)