From 48dfb8e6171c0aa9d3a46264bfffc30077eec550 Mon Sep 17 00:00:00 2001 From: Gabriella Martin Date: Mon, 29 Apr 2024 15:45:10 +0100 Subject: [PATCH] test(docs): :white_check_mark: update test to use httpx instead of grequests --- tests/test_docs.py | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/tests/test_docs.py b/tests/test_docs.py index 7cbc72d5..84bd3883 100644 --- a/tests/test_docs.py +++ b/tests/test_docs.py @@ -1,13 +1,16 @@ -from bs4 import BeautifulSoup -import grequests -import markdown +import asyncio import os import unittest + +import httpx +import markdown +from bs4 import BeautifulSoup + from . import ( + DOC_NAMES, + DOCS_DIR, SCHEMA_NAMES, all_properties, - DOCS_DIR, - DOC_NAMES, property_doc_name, schema_enum_registry, ) @@ -117,14 +120,12 @@ def error_msg(schema_name, value, enum): schema_name, v, enum ) # noqa - def test_urls_in_docs(s): - def exception(request, exception): - return f"{request} - {exception}" + def test_urls_in_docs(self): + async def async_requests(urls): + async with httpx.AsyncClient(timeout=60) as client: + responses = (client.get(url) for url in urls) + results = await asyncio.gather(*responses, return_exceptions=True) - def async_requests(urls): - results = grequests.map( - (grequests.get(u) for u in urls), exception_handler=exception, size=100 - ) return results urls = [] @@ -142,18 +143,21 @@ def async_requests(urls): urls.append(url) - results = async_requests(urls) + results = asyncio.run(async_requests(urls)) warns = [] not_founds = [] - for resp in results: - if not resp.ok: - warns.append(f"failed {resp.status_code}: {resp.url}") - if resp.status_code in [404]: - not_founds.append(resp.url) - - if not_founds: - raise ValueError(f"URLs not found: \n {not_founds}") + for response in results: + if isinstance(response, httpx.HTTPError): + warns.append(f"failed {response!s}: {response.request.url!s}") + else: + if not response.is_success: + warns.append(f"failed {response.status_code}: {response.url!s}") + + if response.status_code in (404,): + not_founds.append(str(response.url)) + + assert not not_founds, f"URLs not found: \n {not_founds}" print("\n=== Minor URL link warnings ===\n") for w in warns: