Files
wehub-resource-sync 7a0da7932b
OSV-Scanner (Scheduled) / scan-scheduled (push) Failing after 0s
Create Release / test-gate (push) Has been cancelled
Create Release / release-gate (push) Has been cancelled
Create Release / ci-gate (push) Has been cancelled
Create Release / version-check (push) Has been cancelled
Create Release / e2e-test-gate (push) Has been cancelled
Create Release / responsive-test-gate (push) Has been cancelled
Create Release / compat-test-gate (push) Has been cancelled
Create Release / compose-integration-gate (push) Has been cancelled
Create Release / vulture-gate (push) Has been cancelled
Create Release / build (push) Has been cancelled
Create Release / provenance (push) Has been cancelled
Create Release / prerelease-docker (push) Has been cancelled
Create Release / publish-docker (push) Has been cancelled
Create Release / create-release (push) Has been cancelled
Create Release / cleanup-changelog (push) Has been cancelled
Create Release / trigger-pypi (push) Has been cancelled
Create Release / monitor-pypi (push) Has been cancelled
Create Release / Clean up orphan prerelease tags and signatures (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-form] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-metrics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [research-workflow] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-core] (push) Has been cancelled
CodeQL Advanced / Analyze (javascript-typescript) (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [history-news] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [library] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [link-analytics] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-core] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [chat-lifecycle] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [error-benchmark] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [settings-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) (push) Has been cancelled
Docker Tests (Consolidated) / Accessibility Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Unit Tests (push) Has been cancelled
Docker Tests (Consolidated) / LLM Example Tests (push) Has been cancelled
Docker Tests (Consolidated) / Production Image Smoke Test (push) Has been cancelled
Docker Tests (Consolidated) / Infrastructure Tests (push) Has been cancelled
OSSF Scorecard / OSSF Security Scorecard Analysis (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [mobile] (push) Has been cancelled
Backwards Compatibility / Verify Encryption Constants (push) Has been cancelled
Backwards Compatibility / PyPI Version Compatibility (push) Has been cancelled
Backwards Compatibility / Database Migration Tests (push) Has been cancelled
CodeQL Advanced / Analyze (python) (push) Has been cancelled
Docker Tests (Consolidated) / detect-changes (push) Has been cancelled
Docker Tests (Consolidated) / Build Test Image (push) Has been cancelled
Docker Tests (Consolidated) / All Pytest Tests + Coverage (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [accessibility] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [api-crud] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-login] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-pages] (push) Has been cancelled
Docker Tests (Consolidated) / UI Tests (Puppeteer) [auth-register] (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:08:55 +08:00

217 lines
6.6 KiB
Python

"""Field-level schema drift detection in the OpenAlex fetcher.
A row-count floor catches the case where the whole fetch collapses,
but not the case where every row loads but a key field (``h_index``,
``cited_by_count``) has been renamed upstream. This test feeds
OpenAlex-shaped JSONL with `h_index` silently renamed to `h_idx`
and asserts that ``SchemaDriftError`` is raised instead of a
silently corrupt snapshot.
"""
from __future__ import annotations
import gzip
import io
import json
from unittest.mock import MagicMock, patch
import pytest
from local_deep_research.journal_quality.data_sources.openalex import (
OpenAlexSource,
SchemaDriftError,
)
def _make_jsonl_gz(records):
buf = io.BytesIO()
with gzip.GzipFile(fileobj=buf, mode="wb") as gz:
for rec in records:
gz.write((json.dumps(rec) + "\n").encode("utf-8"))
return buf.getvalue()
def _manifest(num_parts: int):
return {
"files": [
{"url": f"s3://openalex/data/jsonl/sources/part_{i}.gz"}
for i in range(num_parts)
]
}
def _response(*, status_code=200, content=b"", json_body=None):
resp = MagicMock()
resp.status_code = status_code
resp.raise_for_status = MagicMock()
resp.content = content
resp.json = MagicMock(return_value=json_body or {})
return resp
def test_missing_h_index_raises_schema_drift(tmp_path):
# 10010 records — enough to clear the row-count floor, but every
# one of them carries h_idx (renamed) instead of h_index.
drifted = [
{
"id": f"S{i}",
"display_name": f"J {i}",
"type": "journal",
"summary_stats": {
"h_idx": 5, # renamed from h_index
"2yr_mean_citedness": 1.2,
},
"cited_by_count": 1000,
"issn_l": None,
}
for i in range(10_010)
]
content = _make_jsonl_gz(drifted)
with patch(
"local_deep_research.security.safe_requests.safe_get_with_retries",
) as mock_get:
mock_get.side_effect = [
_response(json_body=_manifest(1)), # manifest
_response(content=content), # single partition
]
with pytest.raises(SchemaDriftError) as exc_info:
OpenAlexSource().fetch(tmp_path)
msg = str(exc_info.value)
assert "h_index present in journal sample=False" in msg
# Existing file must not have been overwritten.
assert not (tmp_path / OpenAlexSource().filename).exists()
def test_missing_cited_by_count_raises_schema_drift(tmp_path):
drifted = [
{
"id": f"S{i}",
"display_name": f"J {i}",
"type": "journal",
"summary_stats": {"h_index": 5, "2yr_mean_citedness": 1.2},
# cited_by_count silently missing
}
for i in range(10_010)
]
content = _make_jsonl_gz(drifted)
with patch(
"local_deep_research.security.safe_requests.safe_get_with_retries",
) as mock_get:
mock_get.side_effect = [
_response(json_body=_manifest(1)),
_response(content=content),
]
with pytest.raises(SchemaDriftError) as exc_info:
OpenAlexSource().fetch(tmp_path)
assert "cited_by_count present in journal sample=False" in str(
exc_info.value
)
def test_healthy_snapshot_passes(tmp_path):
healthy = [
{
"id": f"S{i}",
"display_name": f"J {i}",
"type": "journal",
"summary_stats": {"h_index": 5, "2yr_mean_citedness": 1.2},
"cited_by_count": 1000,
}
for i in range(10_010)
]
content = _make_jsonl_gz(healthy)
with patch(
"local_deep_research.security.safe_requests.safe_get_with_retries",
) as mock_get:
mock_get.side_effect = [
_response(json_body=_manifest(1)),
_response(content=content),
]
count = OpenAlexSource().fetch(tmp_path)
assert count == 10_010
assert (tmp_path / OpenAlexSource().filename).exists()
def test_missing_id_raises_schema_drift(tmp_path):
"""If the ``id`` field is renamed (e.g. to ``source_id``), every record
gets silently skipped at parse time. Without a dedicated drift check
this surfaces only as the generic row-count floor RuntimeError, hiding
the real cause. Guard that the ``SchemaDriftError`` path fires first.
"""
drifted = [
{
"source_id": f"S{i}", # renamed from id
"display_name": f"J {i}",
"type": "journal",
"summary_stats": {"h_index": 5, "2yr_mean_citedness": 1.2},
"cited_by_count": 1000,
}
for i in range(10_010)
]
content = _make_jsonl_gz(drifted)
with patch(
"local_deep_research.security.safe_requests.safe_get_with_retries",
) as mock_get:
mock_get.side_effect = [
_response(json_body=_manifest(1)),
_response(content=content),
]
with pytest.raises(SchemaDriftError) as exc_info:
OpenAlexSource().fetch(tmp_path)
msg = str(exc_info.value)
assert "none carried an 'id' field" in msg
assert not (tmp_path / OpenAlexSource().filename).exists()
def test_conference_only_snapshot_does_not_false_trigger(tmp_path):
"""Conferences and other non-journal source types legitimately lack
``h_index`` — a sample that's all conferences must not raise drift.
Journal records with ``h_index`` drive the check; conference records
are excluded from the sample.
"""
mixed = []
# 10,010 conference records without h_index (would false-trigger if
# the sample were unfiltered).
mixed.extend(
{
"id": f"C{i}",
"display_name": f"Conf {i}",
"type": "conference",
"summary_stats": {"2yr_mean_citedness": 1.2},
"cited_by_count": 500,
}
for i in range(10_010)
)
# 100 journals with h_index — enough to fill the drift sample.
mixed.extend(
{
"id": f"J{i}",
"display_name": f"Journal {i}",
"type": "journal",
"summary_stats": {"h_index": 5, "2yr_mean_citedness": 1.2},
"cited_by_count": 1000,
}
for i in range(100)
)
content = _make_jsonl_gz(mixed)
with patch(
"local_deep_research.security.safe_requests.safe_get_with_retries",
) as mock_get:
mock_get.side_effect = [
_response(json_body=_manifest(1)),
_response(content=content),
]
count = OpenAlexSource().fetch(tmp_path)
assert count == 10_110
assert (tmp_path / OpenAlexSource().filename).exists()