From 6c5ac1d733d964c6da731eccb72515e6dec91200 Mon Sep 17 00:00:00 2001 From: ump45nose <52391318+ump45nose@users.noreply.github.com> Date: Fri, 28 Aug 2026 00:50:40 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20reject=20failed=20stopword=20dow?= =?UTF-8?q?nloads?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sdk/nexent/core/nlp/stopwords.py | 1 + test/sdk/core/nlp/test_stopwords.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 test/sdk/core/nlp/test_stopwords.py diff --git a/sdk/nexent/core/nlp/stopwords.py b/sdk/nexent/core/nlp/stopwords.py index 2432a99c0f..f02f2f14d4 100644 --- a/sdk/nexent/core/nlp/stopwords.py +++ b/sdk/nexent/core/nlp/stopwords.py @@ -13,6 +13,7 @@ def download_stopwords(url: str, save_path: str) -> bool: try: logger.info(f"Downloading stopwords: {url}") response = requests.get(url, timeout=10) + response.raise_for_status() response.encoding = 'utf-8' with open(save_path, 'w', encoding='utf-8') as f: f.write(response.text) diff --git a/test/sdk/core/nlp/test_stopwords.py b/test/sdk/core/nlp/test_stopwords.py new file mode 100644 index 0000000000..f3c6ac54e8 --- /dev/null +++ b/test/sdk/core/nlp/test_stopwords.py @@ -0,0 +1,29 @@ +import importlib.util +from pathlib import Path +from unittest.mock import Mock + +import requests + + +MODULE_PATH = ( + Path(__file__).resolve().parents[4] / "sdk" / "nexent" / "core" / "nlp" / "stopwords.py" +) +SPEC = importlib.util.spec_from_file_location("nexent_stopwords", MODULE_PATH) +assert SPEC and SPEC.loader +stopwords = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(stopwords) + + +def test_download_stopwords_rejects_http_error(monkeypatch, tmp_path): + response = Mock(text="Not Found") + response.raise_for_status.side_effect = requests.HTTPError("404 Client Error") + get = Mock(return_value=response) + monkeypatch.setattr(stopwords.requests, "get", get) + target = tmp_path / "stopwords.txt" + + assert ( + stopwords.download_stopwords("https://example.com/missing", str(target)) + is False + ) + assert not target.exists() + response.raise_for_status.assert_called_once_with()