Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Committing query shopcart by price #61

Merged
merged 2 commits into from
Mar 31, 2024
Merged
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
14 changes: 14 additions & 0 deletions service/models/shopcart.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,17 @@ def find_by_user_id(cls, user_id):
"""
logger.info("Processing carts query for the user with id: %s ...", user_id)
return cls.query.filter(cls.user_id == user_id)

@classmethod
def find_by_total_price(cls, _total_price: str) -> list:
"""Returns all Shopcarts with the given _total_price

:param _total_price: the total_price of the Shopcart you want to match
:type name: str

:return: a collection of Shopcarts with that total_price
:rtype: list

"""
logger.info("Processing total_price query for %s ...", _total_price)
return cls.query.filter(cls._total_price == _total_price)
16 changes: 7 additions & 9 deletions service/routes.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to create a test case for the line 151, so that the codecov report would stop complaining.

Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,13 @@ def list_shopcarts():

shopcarts = []

# # See if any query filters were passed in
# category = request.args.get("category")
# name = request.args.get("name")
# if category:
# shopcarts = Shopcart.find_by_category(category)
# elif name:
# shopcarts = Shopcart.find_by_name(name)
# else:
shopcarts = Shopcart.all()
# See if any query filters were passed in
_total_price = request.args.get("total_price")
if _total_price:
_total_price = float(_total_price)
shopcarts = Shopcart.find_by_total_price(_total_price)
else:
shopcarts = Shopcart.all()

results = [shopcart.serialize() for shopcart in shopcarts]
app.logger.info("Returning %d shopcarts", len(results))
Expand Down
22 changes: 22 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from service.models.persistent_base import db
from .factories import ShopcartFactory, ItemFactory


DATABASE_URI = os.getenv(
"DATABASE_URI", "postgresql+psycopg://postgres:postgres@localhost:5432/testdb"
)
Expand Down Expand Up @@ -302,6 +303,27 @@ def test_get_item_list(self):
data = resp.get_json()
self.assertNotEqual(len(data), 0)

def test_query_shopcart_list_by_total_price(self):
"""It should Query Shopcarts by total price"""
shopcarts = self._create_shopcarts(10)
test_total_price = shopcarts[0]._total_price
_total_price_shopcarts = [
shopcart
for shopcart in shopcarts
if shopcart._total_price == test_total_price
]
response = self.client.get(
BASE_URL, query_string=f"total_price={test_total_price}"
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = response.get_json()
self.assertEqual(len(data), len(_total_price_shopcarts))
# check the data just to be sure
for shopcart in data:
self.assertAlmostEqual(
float(shopcart["total_price"]), float(test_total_price), places=2
)


######################################################################
# T E S T S A D P A T H S
Expand Down
18 changes: 18 additions & 0 deletions tests/test_shopcart_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ def test_find_by_user_id(self):
self.assertEqual(same_shopcart.id, shopcart.id)
self.assertEqual(same_shopcart.user_id, shopcart.user_id)

def test_find_by_total_price(self):
"""It should Find a Shopcart by total price"""
shopcarts = ShopcartFactory.create_batch(10)
for shopcart in shopcarts:
shopcart.create()
_total_price = shopcarts[0]._total_price
count = len(
[
shopcart
for shopcart in shopcarts
if shopcart._total_price == _total_price
]
)
found = Shopcart.find_by_total_price(_total_price)
self.assertEqual(found.count(), count)
for shopcart in found:
self.assertEqual(shopcart._total_price, _total_price)


######################################################################
# T E S T S H O P C A R T S E X C E P T I O N H A N D L E R S
Expand Down
Loading