From 80b13d45a1c83856943ac93ec9be3077978869ae Mon Sep 17 00:00:00 2001 From: Michael Strobel Date: Mon, 31 Aug 2026 15:17:50 -0700 Subject: [PATCH] fix: remove Dash loopback render requests --- .../dashinterface.py | 7 --- test/test_dashinterface.py | 55 +++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 test/test_dashinterface.py diff --git a/metabolomics_spectrum_resolver/dashinterface.py b/metabolomics_spectrum_resolver/dashinterface.py index 9e5c869..f5d4b74 100644 --- a/metabolomics_spectrum_resolver/dashinterface.py +++ b/metabolomics_spectrum_resolver/dashinterface.py @@ -7,7 +7,6 @@ import dash_core_components as dcc import dash_html_components as html import dash_table -import requests from dash.dependencies import Input, Output, State from dash_table import DataTable, FormatTemplate from dash_table.Format import Format, Scheme @@ -776,9 +775,6 @@ def _process_usi( _, source_link, splash_key = tasks.parse_usi(usi) usi_url = f"/svg/?{urlencode(drawing_controls, quote_via=quote)}" - # Pre-fetch the spectrum plot to warm the cache. - requests.get(f"http://localhost:5000{usi_url}") - image_obj = html.Img(src=usi_url) json_button = html.A( @@ -878,9 +874,6 @@ def _process_mirror_usi( _, source_link2, splash_key2 = tasks.parse_usi(usi2) mirror_url = f"/svg/mirror/?{urlencode(drawing_controls, quote_via=quote)}" - # Pre-fetch the mirror plot to warm the cache. - requests.get(f"http://localhost:5000{mirror_url}") - image_obj = html.Img(src=mirror_url) json_button = html.A( diff --git a/test/test_dashinterface.py b/test/test_dashinterface.py new file mode 100644 index 0000000..5d59fe0 --- /dev/null +++ b/test/test_dashinterface.py @@ -0,0 +1,55 @@ +from unittest import mock + +import requests + + +USI_1 = "mzspec:GNPS:GNPS-LIBRARY:accession:CCMSLIB00005436077" +USI_2 = "mzspec:MASSBANK::accession:EA266604" + + +def test_process_usi_does_not_make_loopback_request(): + from metabolomics_spectrum_resolver import dashinterface + + drawing_controls = {"usi1": USI_1} + with mock.patch.object( + dashinterface.tasks, + "parse_usi", + return_value=(None, "https://example.org/source", "splash"), + ), mock.patch.object( + requests, + "get", + side_effect=AssertionError("Dash rendering must not make HTTP requests"), + ) as request_get: + _, image = dashinterface._process_usi(USI_1, drawing_controls) + + request_get.assert_not_called() + assert image.src == ( + "/svg/?usi1=" + "mzspec%3AGNPS%3AGNPS-LIBRARY%3Aaccession%3ACCMSLIB00005436077" + ) + + +def test_process_mirror_usi_does_not_make_loopback_request(): + from metabolomics_spectrum_resolver import dashinterface + + drawing_controls = {"usi1": USI_1, "usi2": USI_2} + with mock.patch.object( + dashinterface.tasks, + "parse_usi", + return_value=(None, "https://example.org/source", "splash"), + ) as parse_usi, mock.patch.object( + requests, + "get", + side_effect=AssertionError("Dash rendering must not make HTTP requests"), + ) as request_get: + _, image, _ = dashinterface._process_mirror_usi( + USI_1, USI_2, drawing_controls + ) + + request_get.assert_not_called() + assert parse_usi.call_count == 2 + assert image.src == ( + "/svg/mirror/?usi1=" + "mzspec%3AGNPS%3AGNPS-LIBRARY%3Aaccession%3ACCMSLIB00005436077" + "&usi2=mzspec%3AMASSBANK%3A%3Aaccession%3AEA266604" + )