Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions sdk/nexent/core/nlp/stopwords.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions test/sdk/core/nlp/test_stopwords.py
Original file line number Diff line number Diff line change
@@ -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

Check warning on line 12 in test/sdk/core/nlp/test_stopwords.py

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this composite assertion into separate assertions.

See more on https://sonarcloud.io/project/issues?id=ModelEngine-Group_nexent&issues=AaBEIrQy6ZZhABLcmick&open=AaBEIrQy6ZZhABLcmick&pullRequest=3797
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()