Skip to content

Commit c6ed59f

Browse files
authored
test: mock network request in test_chart_with_url_data (#9209)
The test was flaky because it hit vega.github.io on every run; replace the live fetch with a mocked urllib.request.urlopen response.
1 parent 7ec3efc commit c6ed59f

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

tests/_plugins/ui/_impl/test_altair_chart.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io
66
import json
77
import sys
8+
import unittest.mock
89
from contextlib import redirect_stderr
910
from typing import TYPE_CHECKING, Any
1011
from unittest.mock import Mock
@@ -1463,9 +1464,21 @@ def test_chart_with_url_data():
14631464
.encode(x="Horsepower:Q", y="Miles_per_Gallon:Q")
14641465
)
14651466

1466-
marimo_chart = altair_chart(chart)
1467-
assert isinstance(marimo_chart.dataframe, pl.DataFrame)
1468-
assert len(marimo_chart.dataframe) > 0
1467+
fake_payload = json.dumps(
1468+
[
1469+
{"Horsepower": 130, "Miles_per_Gallon": 18.0},
1470+
{"Horsepower": 165, "Miles_per_Gallon": 15.0},
1471+
{"Horsepower": 150, "Miles_per_Gallon": 18.0},
1472+
]
1473+
).encode("utf-8")
1474+
1475+
with unittest.mock.patch(
1476+
"urllib.request.urlopen",
1477+
return_value=io.BytesIO(fake_payload),
1478+
):
1479+
marimo_chart = altair_chart(chart)
1480+
assert isinstance(marimo_chart.dataframe, pl.DataFrame)
1481+
assert len(marimo_chart.dataframe) > 0
14691482

14701483

14711484
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")

0 commit comments

Comments
 (0)