Skip to content

Commit

Permalink
add aiohttp test
Browse files Browse the repository at this point in the history
  • Loading branch information
ponytailer committed Aug 11, 2023
1 parent 9057734 commit b61dfcd
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ mypy==1.4.1
pre-commit==3.3.3
pytest==7.3.1
pytest-cov==4.1.0
pytest-asyncio
34 changes: 33 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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()
32 changes: 32 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import aiohttp
import requests


Expand All @@ -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,
Expand All @@ -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)
)
45 changes: 45 additions & 0 deletions tests/test_async_client.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b61dfcd

Please sign in to comment.