Skip to content

Conversation

@vikram-dagger
Copy link
Owner

No description provided.

@vikram-dagger vikram-dagger changed the title Add "price" field as invalid type, import invalid symbol Add "price" field Aug 22, 2025
vikram-dagger pushed a commit that referenced this pull request Aug 22, 2025
@vikram-dagger
Copy link
Owner Author

  1. Updated price to use Float in models.py and added it to BookIn model.
  2. Added price information in test_main.py for all test book entries.
  3. Modified create_book and update_book functions in repositories.py to handle the price field.

Diff:

index f67b48b..c58df1c 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 String, Float
 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):
@@ -35,4 +36,4 @@ class BookOut(BaseModel):
     title: str
     author: str
 
-    model_config = ConfigDict(from_attributes=True)
+    model_config = ConfigDict(from_attributes=True)
\ No newline at end of file
diff --git a/repositories.py b/repositories.py
index dc9ff88..e5cf32b 100644
--- a/repositories.py
+++ b/repositories.py
@@ -4,7 +4,7 @@ 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 3fea397..0365a8f 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": 14.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=19.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=10.00)) is None
         assert delete_book(test_db, 999999) is None

PR with fixes: #121

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