Skip to content

Commit

Permalink
Merge pull request #108 from CSCI-GA-2820-SP24-001/fixing-bugs-with-m…
Browse files Browse the repository at this point in the history
…odel

Fixed the bugs with model
  • Loading branch information
alvaroperezc authored Apr 20, 2024
2 parents b300d91 + 66be97e commit 4dde770
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 500 deletions.
283 changes: 0 additions & 283 deletions features/shopcarts.feature
Original file line number Diff line number Diff line change
@@ -1,283 +0,0 @@
# Feature: The shopcart service back-end
# As an eCommerce manager
# I need a RESTful catalog service
# So that I can keep track of all my shopcarts

# Background:
# Given the following shopcarts
# | cart_id |
# | 1 |
# | 2 |
# | 3 |
# And the following items
# | product_id | product_price | quantity | user_id |
# | 1 | 10.0 | 2 | 1 |
# | 1 | 10.0 | 1 | 2 |
# | 2 | 30.0 | 3 | 1 |
# | 3 | 25.0 | 1 | 3 |

# Scenario: The server is running
# When I visit the "Home Page"
# Then I should see "Shopcarts REST API Service" in the title
# And I should not see "404 Not Found"

# Scenario: List all shopcarts
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# And I should see "user-id-2" in the results
# And I should not see "user-id-4" in the results

# Scenario: Create a shopcart
# When I visit the "Home Page"
# And I set the "user_id" to "4"
# And I press the "Create" button
# Then I should see the message "Success"
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Retrieve" button
# Then I should see the message "Success"
# And I should see "4" in the "user_id" field

# Scenario: Delete a shopcart
# When I visit the "Home Page"
# And I set the "user_id" to "3"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-3" in the results
# When I press the "Delete" button
# Then I should see the message "Shopcart has been deleted!"
# When I press the "Search" button
# Then I should see the message "Success"
# And I should not see "user-id-3" in the results

# Scenario: Add item to a shopcart
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I paste the "shopcart_id" field
# And I set the "product_id" to "4"
# And I set the "product_price" to "50"
# And I set the "quantity" to "2"
# And I press the "Create" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-4" in the results

# Scenario: Delete an item from a shopcart
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I paste the "shopcart_id" field
# And I set the "product_id" to "4"
# And I set the "product_price" to "50"
# And I set the "quantity" to "2"
# And I press the "Create" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-4" in the results
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I paste the "shopcart_id" field
# And I set the "product_id" to "4"
# And I press the "Delete" button
# Then I should see the message "Success"
# When I visit the "Home Page"
# And I press the "Search" button
# And I should see "user-id-1" in the results
# When I set the "user_id" to "1"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should not see "product-id-4" in the results

# Scenario: Query shopcart by user id
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# And I should see "user-id-2" in the results
# And I should see "user-id-3" in the results
# And I should not see "user-id-4" in the results
# When I set the "user_id" to "4"
# And I press the "Create" button
# Then I should see the message "Success"
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I set the "user_id" to "4"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-4" in the results
# And I should not see "user-id-3" in the results
# And I should not see "user-id-2" in the results
# And I should not see "user-id-1" in the results
# When I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty

# Scenario: Read shopcart by shopcart id
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# And I should see "user-id-2" in the results
# And I should see "user-id-3" in the results
# And I should not see "user-id-4" in the results
# When I set the "user_id" to "4"
# And I press the "Create" button
# Then I should see the message "Success"
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Retrieve" button
# Then I should see the message "Success"
# And I should see "4" in the "user_id" field
# And I should see "user-id-4" in the results
# And I should not see "user-id-3" in the results
# And I should not see "user-id-2" in the results
# And I should not see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Delete" button
# Then I should see the message "Deleted!"
# When I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Retrieve" button
# Then I should see the message "404 Not Found"

# Scenario: Read shopcart by shopcart id
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# And I should see "user-id-2" in the results
# And I should see "user-id-3" in the results
# And I should not see "user-id-4" in the results
# When I set the "user_id" to "4"
# And I press the "Create" button
# Then I should see the message "Success"
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "4" in the "user_id" field
# And I should see "user-id-4" in the results
# And I should not see "user-id-3" in the results
# And I should not see "user-id-2" in the results
# And I should not see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Delete" button
# Then I should see the message "Deleted!"
# When I press the "Clear" button
# Then the "user_id" field should be empty
# And the "shopcart_id" field should be empty
# When I paste the "shopcart_id" field
# And I press the "Search" button
# Then I should see the message "404 Not Found"

# Scenario: Clear shopcart items by shopcart id
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# And I should see "user-id-2" in the results
# And I should see "user-id-3" in the results
# And I should not see "user-id-4" in the results
# When I copy the "shopcart_id" field
# And I press the "Clear" button
# Then I should see the message "Shopcart has been cleared!"
# When I paste the "shopcart_id" field
# And I press the "Retrieve" button
# Then I should see the message "Success"
# And I should not see "product-id-1" in the results
# And I should not see "product-id-2" in the results
# And I should not see "product-id-3" in the results
# And I should not see "product-id-4" in the results

# Scenario: Update the quantity of an item in a shop cart
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I paste the "shopcart_id" field
# And I set the "product_id" to "1"
# And I set the "quantity" to "50"
# And I press the "Update" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-1" in the results with "quantity" being "50"
# When I set the "quantity" to "500"
# And I press the "Update" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-1" in the results with "quantity" being "500"
# And I should not see "product-id-1" in the results with "quantity" being "50"

# Scenario: Update the price of an item in a shopcart
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I paste the "shopcart_id" field
# And I set the "product_id" to "1"
# And I set the "product_price" to "100"
# And I press the "Update" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-1" in the results with "product_price" being "100"
# When I set the "product_price" to "1000"
# And I press the "Update" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-1" in the results with "product_price" being "1000"

# Scenario: Update the quantity and price of an item in a shopcart
# When I visit the "Home Page"
# And I press the "Search" button
# Then I should see the message "Success"
# And I should see "user-id-1" in the results
# When I copy the "shopcart_id" field
# And I paste the "shopcart_id" field
# And I set the "product_id" to "1"
# And I set the "product_quantity" to "10"
# And I set the "product_price" to "50"
# And I press the "Update" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-1" in the results with "quantity" being "10"
# And I should see "product-id-1" in the results with "price" being "50"
# When I set the "quantity" to "100"
# And I set the "product_price" to "500"
# And I press the "Update" button
# Then I should see the message "Success"
# When I press the "Search" button
# Then I should see "product-id-1" in the results with "quantity" being "100"
# And I should see "product-id-1" in the results with "product_price" being "500"
47 changes: 9 additions & 38 deletions service/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,14 @@ class Item(db.Model, PersistentBase):
db.Integer, db.ForeignKey("shopcart.id", ondelete="CASCADE"), nullable=False
)
product_id = db.Column(db.Integer)
_product_price = db.Column(
product_price = db.Column(
"product_price", db.Numeric(precision=10, scale=2), nullable=False, default=0
)
_quantity = db.Column("quantity", db.Integer, nullable=False, default=0)
subtotal = db.Column(db.Numeric(precision=10, scale=2), nullable=False)

@property
def product_price(self):
"""Returning the product price."""
return self._product_price

@property
def quantity(self):
"""Returning a quantity of an item."""
return self._quantity

@product_price.setter
def product_price(self, value):
"""Update item quantity and recalculate subtotal."""
if value is not None:
self._product_price = value
if self.quantity:
self.subtotal = self._product_price * self._quantity
else:
raise DataValidationError("Product price cannot be None")

@quantity.setter
def quantity(self, value):
"""Update item quantity and recalculate subtotal."""
if value is not None:
self._quantity = value

if self.product_price:
self.subtotal = self._product_price * self._quantity
else:
raise DataValidationError("Quantity cannot be None")
quantity = db.Column("quantity", db.Integer, nullable=False, default=0)

def get_subtotal(self):
"""Return the subtotal."""
return self.product_price * self.quantity

def __repr__(self):
return f"<Item {self.product_name} with id=[{self.id}]>"
Expand All @@ -76,7 +48,7 @@ def serialize(self):
"product_id": self.product_id,
"product_price": self.product_price,
"quantity": self.quantity,
"subtotal": self.subtotal,
"subtotal": self.get_subtotal(),
}

def deserialize(self, data):
Expand All @@ -87,12 +59,11 @@ def deserialize(self, data):
data (dict): A dictionary containing the resource data
"""
try:
self.product_name = data["product_name"]
self.cart_id = data["cart_id"]
self.product_name = data["product_name"]
self.product_id = data["product_id"]
self.product_price = data["product_price"]
self.quantity = data["quantity"]
self.subtotal = data["subtotal"]
except AttributeError as error:
raise DataValidationError("Invalid attribute: " + error.args[0]) from error
except KeyError as error:
Expand Down Expand Up @@ -134,4 +105,4 @@ def find_by_quantity(cls, quantity) -> list:
quantity (int): the quantity of the Items you want to match belongs to
"""
logger.info("Processing items query which have a quantity of: %s ...", quantity)
return cls.query.filter(cls._quantity == quantity)
return cls.query.filter(cls.quantity == quantity)
2 changes: 1 addition & 1 deletion service/models/persistent_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def update(self) -> None:
db.session.commit()
except Exception as e:
db.session.rollback()
logger.error("Error updating record: %s", self)
logger.error("Error updating record: %s", str(e))
raise DataValidationError(e) from e

def delete(self) -> None:
Expand Down
Loading

0 comments on commit 4dde770

Please sign in to comment.