-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
166 lines (134 loc) · 4.52 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
from fastapi import FastAPI, HTTPException
from fastapi.exceptions import HTTPException
from fastapi.responses import JSONResponse
from fastapi.testclient import TestClient
from pydantic import BaseModel, validator
from datetime import datetime
from typing import List, Dict, Optional
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
origins = [
"https://example-frontend.com",
"http://localhost:3000",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins = origins,
allow_credentials = True,
allow_methods = ["GET", "POST", "PUT", "DELETE"],
allow_headers = ["*"],
)
class Book(BaseModel):
id: int
title: str
author: str
description: Optional[str] = None
published_year: int
@validator('published_year')
def verify_year(cls, year):
current_year = datetime.now().year
if year > current_year:
raise ValueError('Invalid published year')
return year
DB : Dict[int, Book] = {}
@app.get("/")
def read_root():
return {"Hello, Library!"}
@app.post("/books", response_model=Book)
def add_book(book: Book):
if book.id in DB:
raise HTTPException(status_code=400, detail="Book already exists")
DB[book.id] = book
return book
@app.get("/books", response_model=List[Book])
def read_books(title: Optional[str] = None, author: Optional[str] = None,
published_year: Optional[int] = None):
results = list(DB.values())
if title:
results = [book for book in results if title.lower() in book.title.lower()]
if author:
results = [book for book in results if author.lower() in book.author.lower()]
if published_year:
results = [book for book in results if published_year == book.published_year]
return results
@app.get("/books/{book_id}", response_model=Book)
def read_book(book_id: int):
if book_id not in DB:
raise HTTPException(status_code=404, detail="Book not found")
return DB[book_id]
@app.put("/books/{book_id}")
def update_book(book_id: int, book: Book):
if book_id not in DB:
raise HTTPException(status_code=404, detail="Book not found")
DB[book_id] = book
return DB[book_id]
@app.delete("/books/{book_id}")
def delete_book(book_id: int):
if book_id not in DB:
raise HTTPException(status_code=404, detail="Book not found")
del DB[book_id]
return f"Book {book_id} has been deleted."
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
return JSONResponse(status_code=exc.status_code, content={"message":exc.detail})
client = TestClient(app)
def test_create_book():
new_book = {
"id": 0,
"title": "FastAPI 기초",
"author": "한승주",
"description": "test",
"published_year": 2023
}
response = client.post("/books", json=new_book)
assert response.status_code == 200
def test_create_book_fail():
duplicated_book = {
"id": 0,
"title": "FastAPI 기초",
"author": "한승주",
"description": "test",
"published_year": 2023
}
response = client.post("/books", json=duplicated_book)
assert response.status_code == 400
assert response.json() == {"message": "Book already exists"}
def test_read_book_list():
response = client.get("/books")
assert response.status_code == 200
def test_read_book():
response = client.get("/books/0")
assert response.status_code == 200
def test_read_book_fail():
response = client.get("/books/10")
assert response.status_code == 404
assert response.json() == {"message": "Book not found"}
def test_update_book():
update_book = {
"id": 0,
"title": "C언어 기초",
"author": "한승주",
"description": "test",
"published_year": 2012
}
response = client.put("/books/0", json=update_book)
assert response.status_code == 200
def test_update_book_fail():
nonexistent_book = {
"id": 100,
"title": "C언어 기초",
"author": "한승주",
"description": "test",
"published_year": 2012
}
response = client.put("/books/100", json=nonexistent_book)
assert response.status_code == 404
assert response.json() == {"message": "Book not found"}
def test_delete_book():
response = client.delete("/books/0")
assert response.status_code == 200
def test_delete_book_fail():
response = client.delete("/books/100")
assert response.status_code == 404
assert response.json() == {"message": "Book not found"}