From b61dfcd59670b1e5d10298e62563c386f39efa51 Mon Sep 17 00:00:00 2001 From: huangsong Date: Fri, 11 Aug 2023 16:06:04 +0800 Subject: [PATCH] add aiohttp test --- requirements.txt | 1 + tests/conftest.py | 34 +++++++++++++++++++++++++++- tests/helpers.py | 32 +++++++++++++++++++++++++++ tests/test_async_client.py | 45 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 tests/test_async_client.py diff --git a/requirements.txt b/requirements.txt index ee84228..add1fc4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ mypy==1.4.1 pre-commit==3.3.3 pytest==7.3.1 pytest-cov==4.1.0 +pytest-asyncio \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index dee1b4e..659729d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,6 @@ import pytest -from pydantic_client import delete, get, post, put, RequestsClient +from pydantic_client import AIOHttpClient, delete, get, post, put, RequestsClient from tests.book import Book @@ -31,6 +31,38 @@ def delete_book(self, book_id: int) -> Book: ... +class AsyncR(AIOHttpClient): + def __init__(self): + super().__init__("http://localhost") + + @get("/books/{book_id}?query={query}") + async def get_book(self, book_id: int, query: str) -> Book: + ... + + @get("/books/{book_id}") + async def get_raw_book(self, book_id: int): + ... + + @post("/books", form_body=True) + async def create_book_form(self, book: Book) -> Book: + """ will post the form with book""" + ... + + @put("/books/{book_id}") + async def change_book(self, book_id: int, book: Book) -> Book: + """will put the json body""" + ... + + @delete("/books/{book_id}") + async def delete_book(self, book_id: int) -> Book: + ... + + @pytest.fixture def client(): yield R() + + +@pytest.fixture +def async_client(): + yield AsyncR() diff --git a/tests/helpers.py b/tests/helpers.py index 3ed7094..07efb85 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,3 +1,4 @@ +import aiohttp import requests @@ -23,6 +24,18 @@ def status_code(self): return self.code +class MockAsyncResponse(MockResponse): + async def __aenter__(self): + return self + + async def __aexit__(self, *args, **kwargs): + ... + + @property + def status(self): + return self.code + + def mock_requests( monkeypatch, response=None, @@ -40,3 +53,22 @@ def mock(*args, **kwargs): requests.Session, "request", mock_call(return_value) ) + + +def mock_aio_http( + monkeypatch, + response=None, + code=200 +): + + def mock_call(mock_return_value): + def mock(*args, **kwargs): + return MockAsyncResponse(**mock_return_value) + + return mock + + return_value = {"code": code, "response": response or {}} + monkeypatch.setattr( + aiohttp.ClientSession, "request", + mock_call(return_value) + ) diff --git a/tests/test_async_client.py b/tests/test_async_client.py new file mode 100644 index 0000000..4b4cd75 --- /dev/null +++ b/tests/test_async_client.py @@ -0,0 +1,45 @@ +import pytest + +from tests.book import Book +from tests.helpers import mock_aio_http + + +@pytest.fixture +def mock_get_book(monkeypatch): + mock_resp = {"name": "name", "age": 1} + yield mock_aio_http(monkeypatch, response=mock_resp) + + +@pytest.mark.asyncio +async def test_get(async_client, mock_get_book): + book: Book = await async_client.get_book(1, "world") + assert book.name == "name" + assert book.age == 1 + + +@pytest.mark.asyncio +async def test_get_raw(async_client, mock_get_book): + book: dict = await async_client.get_raw_book(1) + assert book["name"] == "name" + assert book["age"] == 1 + + +@pytest.mark.asyncio +async def test_post_form(async_client, mock_get_book): + book: Book = await async_client.create_book_form(Book(name="name", age=2)) + assert book.name == "name" + assert book.age == 1 + + +@pytest.mark.asyncio +async def test_put(async_client, mock_get_book): + book: Book = await async_client.change_book(1, Book(name="name", age=2)) + assert book.name == "name" + assert book.age == 1 + + +@pytest.mark.asyncio +async def test_delete(async_client, mock_get_book): + book: Book = await async_client.delete_book(1) + assert book.name == "name" + assert book.age == 1