Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions tests/_plugins/ui/_impl/test_altair_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,8 +777,13 @@ def test_get_dataframe_csv() -> None:
import polars as pl

data = "https://cdn.jsdelivr.net/npm/vega-datasets@v1.29.0/data/stocks.csv"
chart = altair_chart(alt.Chart(data).mark_point().encode(x="values:Q"))
assert isinstance(chart.dataframe, (pd.DataFrame, pl.DataFrame))
fake_payload = b"symbol,price\nMSFT,39.81\nAAPL,28.13\n"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1: The unittest.mock.patch("urllib.request.urlopen", ...) in test_get_dataframe_csv() does not intercept the actual network call. When the chart data URL ends with .csv, the implementation in _get_dataframe_from_chart calls pl.read_csv(url) (or pd.read_csv(url)), both of which handle HTTP natively — they do not use urllib.request.urlopen. The mock is a no-op, so the test still makes a real HTTP request to the CDN.

This means the test (1) depends on external network availability, (2) will fail in airgapped or offline environments, and (3) defeats the PR's goal of mocking network calls.

To fix, either use a local file path or CSV string instead of a URL, or patch polars.read_csv / pandas.read_csv directly depending on which library the test environment provides.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/_plugins/ui/_impl/test_altair_chart.py, line 780:

<comment>The `unittest.mock.patch("urllib.request.urlopen", ...)` in `test_get_dataframe_csv()` does not intercept the actual network call. When the chart data URL ends with `.csv`, the implementation in `_get_dataframe_from_chart` calls `pl.read_csv(url)` (or `pd.read_csv(url)`), both of which handle HTTP natively — they do **not** use `urllib.request.urlopen`. The mock is a no-op, so the test still makes a real HTTP request to the CDN.

This means the test (1) depends on external network availability, (2) will fail in airgapped or offline environments, and (3) defeats the PR's goal of mocking network calls.

To fix, either use a local file path or CSV string instead of a URL, or patch `polars.read_csv` / `pandas.read_csv` directly depending on which library the test environment provides.</comment>

<file context>
@@ -777,8 +777,13 @@ def test_get_dataframe_csv() -> None:
     data = "https://cdn.jsdelivr.net/npm/vega-datasets@v1.29.0/data/stocks.csv"
-    chart = altair_chart(alt.Chart(data).mark_point().encode(x="values:Q"))
-    assert isinstance(chart.dataframe, (pd.DataFrame, pl.DataFrame))
+    fake_payload = b"symbol,price\nMSFT,39.81\nAAPL,28.13\n"
+    with unittest.mock.patch(
+        "urllib.request.urlopen",
</file context>

with unittest.mock.patch(
"urllib.request.urlopen",
return_value=io.BytesIO(fake_payload),
):
chart = altair_chart(alt.Chart(data).mark_point().encode(x="values:Q"))
assert isinstance(chart.dataframe, (pd.DataFrame, pl.DataFrame))


@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
Expand All @@ -790,8 +795,18 @@ def test_get_dataframe_json() -> None:
data = (
"https://cdn.jsdelivr.net/npm/vega-datasets@v1.29.0/data/barley.json"
)
chart = altair_chart(alt.Chart(data).mark_point().encode(x="values:Q"))
assert isinstance(chart.dataframe, (pd.DataFrame, pl.DataFrame))
fake_payload = json.dumps(
[
{"yield": 27.0, "variety": "Manchuria", "site": "University Farm"},
{"yield": 48.9, "variety": "Manchuria", "site": "Waseca"},
]
).encode("utf-8")
with unittest.mock.patch(
"urllib.request.urlopen",
return_value=io.BytesIO(fake_payload),
):
chart = altair_chart(alt.Chart(data).mark_point().encode(x="values:Q"))
assert isinstance(chart.dataframe, (pd.DataFrame, pl.DataFrame))


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