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
41
41
Optionally tweak name and authors in the ` pyproject.toml ` file, however the metadata
42
42
are not used when building the application, nor are referenced anywhere in the code.
43
43
44
+ Before running any commands, install ` uv ` :
45
+
46
+ - On Mac (using ` brew ` ): ` brew install uv `
47
+
44
48
Using Docker:
45
49
46
50
* ` make containers ` : Build containers
Original file line number Diff line number Diff line change
1
+ from typing import Iterable
2
+
1
3
from fastapi import APIRouter , status
2
4
from pydantic import BaseModel , ConfigDict
3
5
@@ -20,6 +22,29 @@ class CreateBookResponse(BaseModel):
20
22
)
21
23
22
24
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
+
23
48
class CreateBookRequest (BaseModel ):
24
49
title : str
25
50
author_name : str
@@ -45,6 +70,13 @@ class CreateBookRequest(BaseModel):
45
70
"""
46
71
47
72
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
+
48
80
@router_v1 .post ("/" , status_code = status .HTTP_201_CREATED )
49
81
async def create_book (
50
82
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