-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from ponytailer/ut-dev
add ut and bugfix
- Loading branch information
Showing
7 changed files
with
121 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "pydantic-client" | ||
version = "0.1.7" | ||
version = "0.1.8" | ||
description = "Http client base pydantic, with requests or aiohttp" | ||
authors = ["ponytailer <[email protected]>"] | ||
readme = "README.md" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class Book(BaseModel): | ||
name: str | ||
age: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
|
||
from pydantic_client import delete, get, post, put, RequestsClient | ||
from tests.book import Book | ||
|
||
|
||
class R(RequestsClient): | ||
def __init__(self): | ||
super().__init__("http://localhost") | ||
|
||
@get("/books/{book_id}?query={query}") | ||
def get_book(self, book_id: int, query: str) -> Book: | ||
... | ||
|
||
@get("/books/{book_id}") | ||
def get_raw_book(self, book_id: int): | ||
... | ||
|
||
@post("/books", form_body=True) | ||
def create_book_form(self, book: Book) -> Book: | ||
""" will post the form with book""" | ||
... | ||
|
||
@put("/books/{book_id}") | ||
def change_book(self, book_id: int, book: Book) -> Book: | ||
"""will put the json body""" | ||
... | ||
|
||
@delete("/books/{book_id}") | ||
def delete_book(self, book_id: int) -> Book: | ||
... | ||
|
||
|
||
@pytest.fixture | ||
def client(): | ||
yield R() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import requests | ||
|
||
|
||
class MockResponse: | ||
|
||
def __init__(self, code, response): | ||
self.code = code | ||
self.response = response | ||
|
||
@property | ||
def ok(self): | ||
return int(self.code) == 200 | ||
|
||
def json(self): | ||
return self.response | ||
|
||
def raise_for_status(self): | ||
if not self.ok: | ||
raise | ||
|
||
@property | ||
def status_code(self): | ||
return self.code | ||
|
||
|
||
def mock_requests( | ||
monkeypatch, | ||
response=None, | ||
code=200 | ||
): | ||
|
||
def mock_call(mock_return_value): | ||
def mock(*args, **kwargs): | ||
return MockResponse(**mock_return_value) | ||
|
||
return mock | ||
|
||
return_value = {"code": code, "response": response or {}} | ||
monkeypatch.setattr( | ||
requests.Session, "request", | ||
mock_call(return_value) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,40 @@ | ||
import unittest | ||
import pytest | ||
|
||
from pydantic import BaseModel | ||
from tests.book import Book | ||
from tests.helpers import mock_requests | ||
|
||
from pydantic_client import delete, get, post, put, RequestsClient | ||
|
||
@pytest.fixture | ||
def mock_get_book(monkeypatch): | ||
mock_resp = {"name": "name", "age": 1} | ||
yield mock_requests(monkeypatch, response=mock_resp) | ||
|
||
class Book(BaseModel): | ||
name: str | ||
age: int | ||
|
||
def test_get(client, mock_get_book): | ||
book: Book = client.get_book(1, "world") | ||
assert book.name == "name" | ||
assert book.age == 1 | ||
|
||
class R(RequestsClient): | ||
def __init__(self): | ||
super().__init__("http://localhost") | ||
|
||
@get("/books/{book_id}?query={query}") | ||
def get_book(self, book_id: int, query: str) -> Book: | ||
... | ||
def test_get_raw(client, mock_get_book): | ||
book: dict = client.get_raw_book(1) | ||
assert book["name"] == "name" | ||
assert book["age"] == 1 | ||
|
||
@post("/books", form_body=True) | ||
def create_book_form(self, book: Book) -> Book: | ||
""" will post the form with book""" | ||
... | ||
|
||
@put("/books/{book_id}") | ||
def change_book(self, book_id: int, book: Book) -> Book: | ||
"""will put the json body""" | ||
... | ||
def test_post_form(client, mock_get_book): | ||
book: Book = client.create_book_form(Book(name="name", age=2)) | ||
assert book.name == "name" | ||
assert book.age == 1 | ||
|
||
@delete("/books/{book_id}") | ||
def change_book(self, book_id: int) -> Book: | ||
... | ||
|
||
def test_put(client, mock_get_book): | ||
book: Book = client.change_book(1, Book(name="name", age=2)) | ||
assert book.name == "name" | ||
assert book.age == 1 | ||
|
||
class TestClient(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.test_client = R() | ||
|
||
def tearDown(self): | ||
self.test_client = None | ||
|
||
def test_get(self): | ||
... | ||
def test_delete(client, mock_get_book): | ||
book: Book = client.delete_book(1) | ||
assert book.name == "name" | ||
assert book.age == 1 |