Skip to content

Commit 010827d

Browse files
ghuronclaude
andcommitted
Reorganize tests into unit/integration, add EuropePMC/Crossref/ORCID tests, fix parse() P698 handling
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 98f5b41 commit 010827d

23 files changed

Lines changed: 84 additions & 89 deletions

CLAUDE.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
## Testing
44

5-
Unit tests use Python's built-in `unittest` framework. Run with:
5+
Unit tests use Python's built-in `unittest` framework.
66

77
```
8-
python -m unittest discover tests/
8+
python -m unittest discover tests/unit/ # unit tests
9+
python -m unittest discover tests/integration/ # integration tests (hit real APIs)
910
```
11+
12+
Connector tests (`tests/integration/`) make real network calls and are treated as integration tests.

src/wdpy/connectors/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from .astro import *
2+
from .arxiv import *
23
from .exoplanet import *
34
from .simbad import *
45
from .ads import *

src/wdpy/connectors/arxiv.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def parse(self, text: str) -> bool:
88
self.patch = []
99
return True
1010
id_text = getattr(entry.find('w3:id', ns), 'text', '') or ''
11+
if self.patch and self.patch[0].mainsnak.property == 'P818' and '/abs/' not in id_text:
12+
return False
1113
arxiv_id = id_text.split('/')[-1].split('v')[0]
1214
self.add_claim('P31', 'Q13442814')
1315
if arxiv_id: self.add_claim('P953', 'https://arxiv.org/pdf/' + arxiv_id)

src/wdpy/connectors/europepmc.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class EuropePMC(SourceItem):
66
def parse(self, text: str) -> bool:
77
results = json.loads(text).get('resultList', {}).get('result', [])
88
if len(results) != 1:
9+
if self.patch and self.patch[0].mainsnak.property == 'P698':
10+
return False
911
self.patch = []
1012
return True
1113
d = results[0]

src/wdpy/source_item.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,12 @@ def extract(cls, ident: Statement) -> Optional[SourceItem]:
156156
if not (req := cls.make_request(ident)):
157157
return None
158158
url = req.full_url
159-
if not (resp := build_opener().open(req, timeout=30)):
159+
try:
160+
resp = build_opener().open(req, timeout=30)
161+
except urllib.error.HTTPError as e:
162+
resp = e
163+
except Exception as e:
164+
logging.error('Request failed for %s: %s', url, e)
160165
return None
161166
handled = cls._config.get('extract', [])
162167
with resp:

tests/integration/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from wdpy.connectors import ADS
44

55

6-
class TestADS(TestCase):
6+
class TestExtract(TestCase):
77
def test_doi_returns_result(self):
88
result = ADS.extract(Statement(Snak('P356', ('10.1088/2041-8205/763/1/L1',))))
99
self.assertEqual(result.patch[0].mainsnak.property, 'P356')

tests/integration/test_arxiv.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from unittest import TestCase
2+
3+
from wdpy import Snak, Statement
4+
from wdpy.connectors import ArxivItem
5+
6+
7+
class TestExtract(TestCase):
8+
def test_arxiv_id_returns_result(self):
9+
result = ArxivItem.extract(Statement(Snak('P818', ('1309.0951',))))
10+
self.assertEqual(result.patch[0].mainsnak.property, 'P818')
11+
12+
def test_doi_returns_result(self):
13+
result = ArxivItem.extract(Statement(Snak('P356', ('10.4171/161',))))
14+
self.assertEqual(result.patch[0].mainsnak.property, 'P356')
15+
16+
def test_nonexistent_arxiv_id_returns_none(self):
17+
self.assertIsNone(ArxivItem.extract(Statement(Snak('P818', ('X',)))))
18+
19+
def test_nonexistent_doi_returns_empty_patch(self):
20+
result = ArxivItem.extract(Statement(Snak('P356', ('10.99999/nonexistent',))))
21+
self.assertEqual(result.patch, [])

tests/integration/test_crossref.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from unittest import TestCase
2+
from wdpy import Snak, Statement
3+
from wdpy.connectors.crossref import Crossref
4+
5+
6+
class TestExtract(TestCase):
7+
def test_doi_returns_result(self):
8+
result = Crossref.extract(Statement(Snak('P356', ('10.1088/2041-8205/763/1/L1',))))
9+
self.assertEqual(result.patch[0].mainsnak.property, 'P356')
10+
11+
def test_nonexistent_doi_returns_empty_patch(self):
12+
result = Crossref.extract(Statement(Snak('P356', ('X',))))
13+
self.assertEqual(result.patch, [])

0 commit comments

Comments
 (0)