Skip to content

Commit f2597bc

Browse files
committed
test: Fix decorate to handle API outage
1 parent 3898d28 commit f2597bc

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

paperscraper/scholar/tests/test_scholar.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pandas as pd
33
import pytest
44
from scholarly._proxy_generator import MaxTriesExceededException
5-
5+
import functools
66

77
from paperscraper.scholar import (
88
get_and_dump_scholar_papers,
@@ -13,29 +13,34 @@
1313
logging.disable(logging.INFO)
1414

1515

16-
@pytest.fixture(autouse=True)
17-
def handle_scholar_exceptions(caplog):
18-
caplog.set_level(logging.INFO)
19-
try:
20-
yield
21-
except MaxTriesExceededException as e:
22-
logging.info(f"MaxTriesExceededException caught: {e}")
23-
pytest.skip("Skipping test due to MaxTriesExceededException")
16+
def handle_scholar_exception(func):
17+
@functools.wraps(func)
18+
def wrapper(*args, **kwargs):
19+
try:
20+
return func(*args, **kwargs)
21+
except MaxTriesExceededException as e:
22+
logging.info(f"MaxTriesExceededException caught: {e}")
23+
pytest.skip("Skipping test due to MaxTriesExceededException")
24+
25+
return wrapper
2426

2527

2628
class TestScholar:
2729

30+
@handle_scholar_exception
2831
def test_citations(self):
2932
num = get_citations_from_title("GT4SD")
3033
assert isinstance(num, int)
3134
assert num > 0
3235

36+
@handle_scholar_exception
3337
def test_dump_search(self, tmpdir):
3438
temp_dir = tmpdir.mkdir("scholar_papers")
3539
output_filepath = temp_dir.join("results.jsonl")
3640
get_and_dump_scholar_papers("GT4SD", str(output_filepath))
3741
assert output_filepath.check(file=1)
3842

43+
@handle_scholar_exception
3944
def test_basic_search(self):
4045
results = get_scholar_papers("GT4SD")
4146
assert len(results) > 0 # Ensure we get some results

0 commit comments

Comments
 (0)