Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BookIn(BaseModel):

title: str
author: str
publisher: str


class BookOut(BaseModel):
Expand All @@ -34,5 +35,6 @@ class BookOut(BaseModel):
id: int
title: str
author: str
publisher: str

model_config = ConfigDict(from_attributes=True)
3 changes: 2 additions & 1 deletion repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# Create a new book
def create_book(db: Session, book: models.BookIn):
db_book = models.Book(title=book.title, author=book.author)
db_book = models.Book(title=book.title, author=book.author, publisher=book.publisher)
db.add(db_book)
db.commit()
db.refresh(db_book)
Expand All @@ -27,6 +27,7 @@ def update_book(db: Session, book_id: int, book: models.BookIn):
if db_book:
db_book.title = book.title
db_book.author = book.author
db_book.publisher = book.publisher
db.commit()
db.refresh(db_book)
return db_book
Expand Down
11 changes: 7 additions & 4 deletions test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

# Test data constants
TEST_BOOKS = [
{"title": "Carrie", "author": "Stephen King"},
{"title": "Ready Player One", "author": "Ernest Cline"},
{"title": "Carrie", "author": "Stephen King", "publisher": "Doubleday"},
{"title": "Ready Player One", "author": "Ernest Cline", "publisher": "Crown"},
]

class TestMainApp:
Expand All @@ -26,6 +26,7 @@ def test_create_book(self, test_db):
book = create_book(test_db, BookIn(**TEST_BOOKS[0]))
assert book.title == TEST_BOOKS[0]["title"]
assert book.author == TEST_BOOKS[0]["author"]
assert book.publisher == TEST_BOOKS[0]["publisher"]
assert book.id is not None

def test_get_books(self, test_db):
Expand All @@ -45,6 +46,7 @@ def test_get_book(self, test_db):
assert retrieved_book is not None
assert retrieved_book.id == created_book.id
assert retrieved_book.title == TEST_BOOKS[0]["title"]
assert retrieved_book.publisher == TEST_BOOKS[0]["publisher"]

def test_update_book(self, test_db):
"""Test updating a book"""
Expand All @@ -54,10 +56,11 @@ def test_update_book(self, test_db):
assert updated_book is not None
assert updated_book.title == TEST_BOOKS[1]["title"]
assert updated_book.author == TEST_BOOKS[1]["author"]
assert updated_book.publisher == TEST_BOOKS[1]["publisher"]

def test_delete_book(self, test_db):
"""Test deleting a book"""
book = create_book(test_db, BookIn(title="To Delete", author="Author"))
book = create_book(test_db, BookIn(title="To Delete", author="Author", publisher="Test Publisher"))
deleted_book = delete_book(test_db, book.id)

assert deleted_book is not None
Expand All @@ -67,5 +70,5 @@ def test_delete_book(self, test_db):
def test_nonexistent_operations(self, test_db):
"""Test operations on nonexistent books"""
assert get_book(test_db, 999999) is None
assert update_book(test_db, 999999, BookIn(title="Test", author="Test")) is None
assert update_book(test_db, 999999, BookIn(title="Test", author="Test", publisher="Test Publisher")) is None
assert delete_book(test_db, 999999) is None