I have the following test:
@pytest.mark.parametrize(
"raw_id", [28815543, "PMC5890441", "doi:10.1007/978-981-10-5203-3_9" "28815543"]
)
def test_can_fetch_publication(raw_id):
idr = IdReference.build(raw_id)
res = asyncio.run(fetch.summary(idr))
assert res == {
"id": "28815543",
"source": "MED",
"pmid": "28815543",
"doi": "10.1007/978-981-10-5203-3_9",
"title": "Understanding the Role of lncRNAs in Nervous System Development.",
<... snip ...>
which runs the function fetch.summary
:
@lru_cache()
@retry(requests.HTTPError, tries=5, delay=1)
@throttle(rate_limit=5, period=1.0)
async def summary(id_reference):
LOGGER.info("Fetching remote summary for %s", id_reference)
url = id_reference.external_url()
response = requests.get(url)
response.raise_for_status()
data = response.json()
assert data, "Somehow got no data"
if data.get("hitCount", 0) == 0:
raise UnknownReference(id_reference)
if data["hitCount"] == 1:
if not data["resultList"]["result"][0]:
raise UnknownReference(id_reference)
return data["resultList"]["result"][0]
<... snip ...>
IdReference is a class defined like this:
@attr.s(frozen=True, hash=True)
class IdReference(object):
namespace = attr.ib(validator=is_a(KnownServices))
external_id = attr.ib(validator=is_a(str))
< ... snip ...>
My problem comes when I try to run the test. Given the first and last elements in the parameterisation result in the same IdRefrenece object (the int gets converted to a string - external_id
in the class), the coroutine produced by is the same, and I end up with a RuntimeError: cannot reuse already awaited coroutine
Is there a way to get around this? Or am I going to have to split the parameterisation out?
I tried running the test using pytest-asyncio
and get the same error, because I think I just fundamentally have to deal with the coroutine being the 'same' somehow
Python version is 3.11, pytest 7.2.0