Skip to content

Commit

Permalink
Merge pull request #67 from CSCI-GA-2820-SP24-003/add-status
Browse files Browse the repository at this point in the history
added status to shopcart
  • Loading branch information
carolinetfls authored Apr 2, 2024
2 parents 13132c1 + a2ec9b8 commit b4dc429
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
38 changes: 22 additions & 16 deletions service/models/shop_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
"""

# from enum import Enum
from enum import Enum
from .persistent_base import db, logger, DataValidationError, PersistentBase
from .shop_cart_item import ShopCartItem

# from decimal import Decimal


# class ShopCartStatus(Enum):
# """Enumeration of different shop cart statuses"""
class ShopCartStatus(Enum):
"""Enumeration of different shop cart statuses"""

# # An item has been added to the shop cart
# ACTIVE = 0
# # User reached last step of checkout
# PENDING = 1
# # Order was fulfilled or cart was abandoned
# INACTIVE = 3
# An item has been added to the shop cart
ACTIVE = 0
# User reached last step of checkout
PENDING = 1
# Order was fulfilled or cart was abandoned
INACTIVE = 3


class ShopCart(db.Model, PersistentBase):
Expand All @@ -33,11 +33,12 @@ class ShopCart(db.Model, PersistentBase):
user_id = db.Column(db.Integer)
name = db.Column(db.String(63))
total_price = db.Column(db.Numeric(precision=10, scale=2))
# status = db.Column(
# db.Enum(
# ShopCartStatus, nullable=False, server_default=(ShopCartStatus.ACTIVE.name)
# )
# )
status = db.Column(
db.Enum(ShopCartStatus),
nullable=False,
server_default=(ShopCartStatus.ACTIVE.name),
)

items = db.relationship("ShopCartItem", backref="shop_cart", passive_deletes=True)

def __repr__(self):
Expand All @@ -50,7 +51,7 @@ def serialize(self) -> dict:
"user_id": self.user_id,
"name": self.name,
"total_price": self.total_price,
# "status": self.status.name,
"status": self.status.name,
"items": [],
}
for item in self.items:
Expand All @@ -68,7 +69,12 @@ def deserialize(self, data):
self.user_id = data["user_id"]
self.name = data["name"]
self.total_price = data["total_price"]
# self.status = getattr(ShopCartStatus, data["status"])
# Check if the status in data is already a ShopCartStatus instance
if isinstance(data["status"], ShopCartStatus):
self.status = data["status"]
else:
# If it's not, assume it's a string and try to convert it
self.status = ShopCartStatus[data["status"]]
item_list = data.get("items")
if item_list:
for json_item in item_list:
Expand Down
9 changes: 5 additions & 4 deletions tests/factories.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Test Factory"""

import factory
from factory.fuzzy import FuzzyDecimal
from factory.fuzzy import FuzzyDecimal, FuzzyChoice
from service.models import ShopCart, ShopCartItem
from service.models.shop_cart import ShopCartStatus


# pylint: disable=too-few-public-methods
Expand All @@ -18,9 +19,9 @@ class Meta:
user_id = factory.Sequence(lambda n: n)
name = factory.Sequence(lambda n: f"sc-{n}")
total_price = FuzzyDecimal(0.00, 200.00)
# status = FuzzyChoice(
# choices=[ShopCartStatus.ACTIVE, ShopCartStatus.PENDING, ShopCartStatus.INACTIVE]
# )
status = FuzzyChoice(
choices=[ShopCartStatus.ACTIVE, ShopCartStatus.PENDING, ShopCartStatus.INACTIVE]
)

@factory.post_generation
def items(
Expand Down
2 changes: 2 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ def test_update_shopcart(self):
"total_price": Decimal(new_shopcart["total_price"])
+ 100, # Example of updating the price
# Include updates to other fields here
"status": new_shopcart["status"],
}

# Update the shopcart
Expand Down Expand Up @@ -210,6 +211,7 @@ def test_update_shop_cart_with_invalid_fields(self):
"user_id": new_shopcart["user_id"],
"name": "Updated Name",
"total_price": new_shopcart["total_price"],
"status": new_shopcart["status"],
"non_existent_field": "test",
}

Expand Down
2 changes: 2 additions & 0 deletions tests/test_shop_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def test_create_a_shop_cart(self):
"user_id": fake_cart.user_id,
"name": fake_cart.name,
"total_price": fake_cart.total_price,
"status": fake_cart.status,
}
cart = ShopCart()
cart.deserialize(fake_cart_dict)
Expand All @@ -73,6 +74,7 @@ def test_create_a_shop_cart(self):
self.assertEqual(cart.user_id, fake_cart.user_id)
self.assertEqual(cart.name, fake_cart.name)
self.assertEqual(cart.total_price, fake_cart.total_price)
self.assertEqual(cart.status, fake_cart.status)

def test_add_a_shop_cart(self):
"""It should Create a shopcart and add it to the database"""
Expand Down

0 comments on commit b4dc429

Please sign in to comment.