|
| 1 | +from fastapi.testclient import TestClient |
| 2 | +from main import app |
| 3 | + |
| 4 | +client = TestClient(app) |
| 5 | + |
| 6 | + |
| 7 | +def test_get_root(): |
| 8 | + """Test the root endpoint""" |
| 9 | + |
| 10 | + response = client.get("/") |
| 11 | + |
| 12 | + assert response.status_code == 200 |
| 13 | + |
| 14 | + |
| 15 | +def test_get_books(): |
| 16 | + """ |
| 17 | + Test the /books endpoint. |
| 18 | + Ensures it returns a list of books with status code 200. |
| 19 | + Checks that the first book has the expected title. |
| 20 | + """ |
| 21 | + response = client.get("/books") |
| 22 | + |
| 23 | + assert response.status_code == 200 |
| 24 | + assert isinstance(response.json(), list) |
| 25 | + assert response.json()[0]["title"] == "Satanás" |
| 26 | + |
| 27 | + |
| 28 | +def test_get_book_by_id(): |
| 29 | + """ |
| 30 | + Test the /books/{book_id} endpoint. |
| 31 | + Fetches book with ID 1 and verifies its book_id and author. |
| 32 | + """ |
| 33 | + response = client.get("/books/1") |
| 34 | + |
| 35 | + assert response.status_code == 200 |
| 36 | + data = response.json() |
| 37 | + assert data["book_id"] == 1 |
| 38 | + assert data["author"] == "Mario Mendoza" |
| 39 | + |
| 40 | + |
| 41 | +def test_get_book_not_found(): |
| 42 | + """ |
| 43 | + Test the /books/{book_id} endpoint with a non-existent book. |
| 44 | + Checks that the error message is returned. |
| 45 | + """ |
| 46 | + response = client.get("/books/999") |
| 47 | + |
| 48 | + assert response.status_code == 404 |
| 49 | + assert response.json()["detail"] == "Book with ID 999 not found" |
| 50 | + |
| 51 | + |
| 52 | +def test_create_book(): |
| 53 | + """ |
| 54 | + Test the POST /books endpoint. |
| 55 | + Creates a new book and verifies the returned data matches the input. |
| 56 | + """ |
| 57 | + new_book = {"title": "Delirio", "author": "Laura Restrepo", "year": 2004} |
| 58 | + |
| 59 | + response = client.post("/books", json=new_book) |
| 60 | + |
| 61 | + assert response.status_code == 201 |
| 62 | + assert response.json()["book_id"] == 5 |
| 63 | + assert response.json()["title"] == "Delirio" |
| 64 | + |
| 65 | + |
| 66 | +def test_update_book(): |
| 67 | + """ |
| 68 | + Test the PUT /books/{book_id} endpoint. |
| 69 | + Updates the title of book with ID 1 and verifies the change. |
| 70 | + """ |
| 71 | + |
| 72 | + update_data = {"title": "La melancolía de los feos"} |
| 73 | + response = client.put("/books/1", json=update_data) |
| 74 | + |
| 75 | + assert response.status_code == 200 |
| 76 | + assert response.json()["title"] == "La melancolía de los feos" |
| 77 | + |
| 78 | + |
| 79 | +def test_delete_book(): |
| 80 | + """ |
| 81 | + Test the DELETE /books/{book_id} endpoint. |
| 82 | + Deletes a book and ensures it no longer exists in the list. |
| 83 | + """ |
| 84 | + response = client.delete("/books/5") # Book ID created in test_create_book |
| 85 | + |
| 86 | + assert response.status_code == 204 |
| 87 | + |
| 88 | + # Verify that the book was actually deleted |
| 89 | + get_response = client.get("/books/5") |
| 90 | + assert get_response.status_code == 404 |
| 91 | + assert get_response.json()["detail"] == "Book with ID 5 not found" |
0 commit comments