Skip to content

Conversation

@vikram-dagger
Copy link
Owner

No description provided.

@vikram-dagger
Copy link
Owner Author

The proposed changes in the workspace involve updating the book management system to include a price field as a float data type. Here is a detailed summary of the changes:

Changes in models.py:

  1. SQLAlchemy Model Update:

    • The Book model's price attribute has been changed from Mapped[str] using Number to Mapped[float] using Float.
  2. Pydantic Models Update:

    • Added a price: float field to the BookIn and BookOut Pydantic models, reflecting changes in input and output representations.

Changes in repositories.py:

  1. Database Operations Update:
    • Modified the create_book() function to accept price when creating a new Book.
    • Updated the update_book() function to allow updating the price of an existing book.

Changes in test_main.py:

  1. Test Data Update:

    • Test books (TEST_BOOKS) now include a price field with a default value of 9.99.
  2. Repository Tests Update:

    • Updated the book creation and update tests to include the price field to ensure accuracy in creation, retrieval, updating, and deletion processes.

These comprehensive updates integrate a pricing feature into the book management system, ensuring that all related components (models, repository functions, and tests) are appropriately modified to accommodate and test this new requirement.

Here is the code diff proposed:

diff --git a/models.py b/models.py
index f67b48b..0a528a0 100644
--- a/models.py
+++ b/models.py
@@ -1,5 +1,5 @@
 from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
-from sqlalchemy import String, Number
+from sqlalchemy import Float, String
 from pydantic import BaseModel, ConfigDict
 
 
@@ -18,7 +18,7 @@ class Book(Base):
     id: Mapped[int] = mapped_column(primary_key=True, index=True)
     title: Mapped[str] = mapped_column(String(255), index=True)
     author: Mapped[str] = mapped_column(String(255))
-    price: Mapped[str] = mapped_column(Number(255), nullable=False)
+    price: Mapped[float] = mapped_column(Float, nullable=False)
 
 # Pydantic models
 class BookIn(BaseModel):
@@ -26,6 +26,7 @@ class BookIn(BaseModel):
 
     title: str
     author: str
+    price: float
 
 
 class BookOut(BaseModel):
@@ -34,5 +35,6 @@ class BookOut(BaseModel):
     id: int
     title: str
     author: str
+    price: float
 
     model_config = ConfigDict(from_attributes=True)
diff --git a/repositories.py b/repositories.py
index f7c2ce0..9ba318e 100644
--- a/repositories.py
+++ b/repositories.py
@@ -4,7 +4,7 @@ from . import models
 
 # 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, price=book.price)
     db.add(db_book)
     db.commit()
     db.refresh(db_book)
@@ -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.price = book.price
         db.commit()
         db.refresh(db_book)
         return db_book
diff --git a/test_main.py b/test_main.py
index 20e40fa..20c2d64 100644
--- a/test_main.py
+++ b/test_main.py
@@ -5,8 +5,8 @@ from sqlalchemy import create_engine, inspect
 
 # Test data constants
 TEST_BOOKS = [
-    {"title": "Carrie", "author": "Stephen King"},
-    {"title": "Ready Player One", "author": "Ernest Cline"},
+    {"title": "Carrie", "author": "Stephen King", "price": 9.99},
+    {"title": "Ready Player One", "author": "Ernest Cline", "price": 9.99},
 ]
 
 class TestMainApp:
@@ -57,7 +57,7 @@ class TestBookRepository:
 
     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", price=9.99))
         deleted_book = delete_book(test_db, book.id)
 
         assert deleted_book is not None
@@ -67,5 +67,5 @@ class TestBookRepository:
     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", price=9.99)) is None
         assert delete_book(test_db, 999999) is None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants