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
3 changes: 2 additions & 1 deletion scripts/review/query_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import sys

RAW_DATA_DIR = "data/raw"
TIMEOUT_SECONDS = 10

def fetch_url(url):
req = urllib.request.Request(
url,
headers={"User-Agent": "UOGTO-Scoping-Review-Agent/1.0 (mailto:uogto@example.org)"}
)
try:
with urllib.request.urlopen(req) as response:
with urllib.request.urlopen(req, timeout=TIMEOUT_SECONDS) as response:
return response.read()
except Exception as e:
print(f"Error fetching {url}: {e}", file=sys.stderr)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_query_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ def test_query_crossref(self, mock_fetch_url):
results = query_crossref("test", limit=1)
self.assertEqual(len(results), 1)
self.assertEqual(results[0]["title"], "Crossref Test Work")

@patch('urllib.request.urlopen')
def test_fetch_url_timeout(self, mock_urlopen):
import urllib.error
import socket
# Simulate a socket timeout exception
mock_urlopen.side_effect = urllib.error.URLError(socket.timeout("timed out"))

# Test that fetch_url handles the exception and returns None
from scripts.review.query_databases import fetch_url
result = fetch_url("http://example.com")
self.assertIsNone(result)
Loading