-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
122 lines (98 loc) · 3.51 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
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
from pydantic import BaseModel, validator
from typing import List, Optional
from datetime import datetime
app = FastAPI()
class Book(BaseModel):
id: int
title: str
author: str
description: Optional[str]
published_year: int
@validator("published_year")
def checkFutureDate(cls, publishedDate):
currentDate = datetime.now().year
if publishedDate > currentDate :
raise ValueError("Published Year is Later than Present Year!")
return publishedDate
bookDB: List[Book] = []
@app.post("/books", response_model=Book)
async def createBook(book: Book):
if book.id in [single.id for single in fakeDB]:
raise HTTPException(status_code=400, detail="Book Already Exists!")
bookDB.append(book)
return book
@app.get("/books", response_model=List[Book])
async def printBookList():
return bookDB
@app.get("/books/{book_id}", response_model=Book)
async def printSingleBook(book_id: int):
for book in bookDB:
if book.id == book_id:
return book
raise HTTPException(status_code=404, detail="Book Not Found")
@app.put("/books/{book_id}", response_model=Book)
async def updateBook(book_id: int, book: Book):
for i, book in enumerate(bookDB):
if book.id == book_id:
bookDB[i] = book
return book
raise HTTPException(status_code=404, detail="Book Not Found")
@app.delete("/books/{book_id}")
async def removeBook(book_id: int):
for i, book in enumerate(bookDB):
del bookDB[i]
return {"message": "Selected Book Deleted!"}
raise HTTPException(status_code=404, detail="Book Not Found")
@app.exception_handler(HTTPException)
async def httpExceptionHandler(request, execution):
return JSONResponse(status_code=execution.status_code, content={"message": execution.detail})
client = TestClient(app)
def printBookTest():
response = client.get("/books")
assert response.status_code == 200
assert response.json() == {"message": "Book Not Found"}
def noBookTest():
response = client.get("/books/100")
assert response.status_code == 404
assert response.json() == {"message": "Book Not Found"}
def createBookTest():
new_book = {
"id": 142,
"title": "Hello",
"author": "Han Hojin",
"description": "This is a Test.",
"published_year": 2024
}
def createWrongBookTest():
existing_book = fakeDB[0]
response = client.post("/books", json=existing_book)
assert response.status_code == 400
def updateBookTest():
take_book = {
"id": fakeDB[0].id,
"title": "Goodbye",
"author": "Ham Hojin",
"description": "This is a Modified Test.",
"published_year": 2004
}
response = client.put(f"/books/{take_book['id']}", json=take_book)
assert response.status_code == 200
assert response.json() == take_book
def updateWrongBookTest():
test_id = 1428
response = client.put(f"/books/{test_id}", json={"id": test_id})
assert response.status_code == 404
def removeBookTest():
removed_book = fakeDB[0]
response = client.delete(f"/books/{removed_book.id}")
assert response.status_code == 200
assert response.json == {"message": "Selected Book Deleted!"}
def removeWrongBookTest():
remove_wrong_book = 1429
response = client.delete(f"/books/{remove_wrong_book}")
assert response.status_code == 404
@app.get("/")
async def returnRootMessage():
return {"message": "Hello, Library!"}