File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
tests/http_app/routes/books Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,10 @@ Create your GitHub repository using this template (The big green `Use this templ
4141Optionally tweak name and authors in the ` pyproject.toml ` file, however the metadata
4242are not used when building the application, nor are referenced anywhere in the code.
4343
44+ Before running any commands, install ` uv ` :
45+
46+ - On Mac (using ` brew ` ): ` brew install uv `
47+
4448Using Docker:
4549
4650* ` make containers ` : Build containers
Original file line number Diff line number Diff line change 1+ from typing import Iterable
2+
13from fastapi import APIRouter , status
24from pydantic import BaseModel , ConfigDict
35
@@ -20,6 +22,29 @@ class CreateBookResponse(BaseModel):
2022 )
2123
2224
25+ class ListBooksResponse (BaseModel ):
26+ books : Iterable [dto .Book ]
27+ model_config = ConfigDict (
28+ json_schema_extra = {
29+ "example" : {
30+ "books" : [
31+ {
32+ "title" : "The Hitchhiker's Guide to the Galaxy" ,
33+ "author_name" : "Douglas Adams" ,
34+ "book_id" : 123 ,
35+ },
36+ {
37+ "title" : "Clean Architecture: "
38+ "A Craftsman's Guide to Software Structure and Design" ,
39+ "author_name" : "Robert C. 'Uncle Bob' Martin" ,
40+ "book_id" : 321 ,
41+ },
42+ ]
43+ }
44+ }
45+ )
46+
47+
2348class CreateBookRequest (BaseModel ):
2449 title : str
2550 author_name : str
@@ -45,6 +70,13 @@ class CreateBookRequest(BaseModel):
4570"""
4671
4772
73+ @router_v1 .get ("/" , status_code = status .HTTP_200_OK )
74+ async def list_books () -> ListBooksResponse :
75+ book_service = BookService ()
76+ books = await book_service .list_books ()
77+ return ListBooksResponse (books = books )
78+
79+
4880@router_v1 .post ("/" , status_code = status .HTTP_201_CREATED )
4981async def create_book (
5082 data : CreateBookRequest ,
Original file line number Diff line number Diff line change 1+ from fastapi import status
2+ from fastapi .testclient import TestClient
3+
4+
5+ async def test_list_books (testapp ):
6+ ac = TestClient (app = testapp , base_url = "http://test" )
7+ response = ac .get ("/api/books/v1/" )
8+ assert response .status_code == status .HTTP_200_OK
9+ body = response .json ()
10+ assert "books" in body
11+ assert len (body ["books" ]) == 1
12+ assert body ["books" ][0 ]["title" ] == "The Shining"
13+ assert body ["books" ][0 ]["author_name" ] == "Stephen King"
You can’t perform that action at this time.
0 commit comments