Skip to content

Commit a4fb902

Browse files
author
Gabriella Martin
committed
test(docs): ✅ update test to use httpx instead of grequests
1 parent 388334e commit a4fb902

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

tests/test_docs.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
from bs4 import BeautifulSoup
2-
import grequests
3-
import markdown
1+
import asyncio
42
import os
53
import unittest
4+
from typing import Union
5+
6+
import httpx
7+
import markdown
8+
from bs4 import BeautifulSoup
9+
610
from . import (
11+
DOC_NAMES,
12+
DOCS_DIR,
713
SCHEMA_NAMES,
814
all_properties,
9-
DOCS_DIR,
10-
DOC_NAMES,
1115
property_doc_name,
1216
schema_enum_registry,
1317
)
@@ -117,14 +121,12 @@ def error_msg(schema_name, value, enum):
117121
schema_name, v, enum
118122
) # noqa
119123

120-
def test_urls_in_docs(s):
121-
def exception(request, exception):
122-
return f"{request} - {exception}"
124+
def test_urls_in_docs(self):
125+
async def async_requests(urls) -> list[Union[httpx.Response, httpx.HTTPError]]:
126+
async with httpx.AsyncClient(timeout=60) as client:
127+
responses = (client.get(url) for url in urls)
128+
results = await asyncio.gather(*responses, return_exceptions=True)
123129

124-
def async_requests(urls):
125-
results = grequests.map(
126-
(grequests.get(u) for u in urls), exception_handler=exception, size=100
127-
)
128130
return results
129131

130132
urls = []
@@ -142,18 +144,21 @@ def async_requests(urls):
142144

143145
urls.append(url)
144146

145-
results = async_requests(urls)
147+
results = asyncio.run(async_requests(urls))
146148

147149
warns = []
148150
not_founds = []
149-
for resp in results:
150-
if not resp.ok:
151-
warns.append(f"failed {resp.status_code}: {resp.url}")
152-
if resp.status_code in [404]:
153-
not_founds.append(resp.url)
154-
155-
if not_founds:
156-
raise ValueError(f"URLs not found: \n {not_founds}")
151+
for response in results:
152+
if isinstance(response, httpx.HTTPError):
153+
warns.append(f"failed {response!s}: {response.request.url!s}")
154+
else:
155+
if not response.is_success:
156+
warns.append(f"failed {response.status_code}: {response.url!s}")
157+
158+
if response.status_code in (404,):
159+
not_founds.append(str(response.url))
160+
161+
assert not not_founds, f"URLs not found: \n {not_founds}"
157162

158163
print("\n=== Minor URL link warnings ===\n")
159164
for w in warns:

0 commit comments

Comments
 (0)