From 79944f999af650bdb65c75274b05c95e56cc96d5 Mon Sep 17 00:00:00 2001 From: Alvaro Perez Casinelli Date: Sat, 30 Mar 2024 19:22:09 +0000 Subject: [PATCH 1/2] Committing query shopcart by price --- service/models/shopcart.py | 14 ++++++++++++++ service/routes.py | 15 ++++++--------- tests/test_shopcart_model.py | 18 ++++++++++++++++++ 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/service/models/shopcart.py b/service/models/shopcart.py index 1e3227c..fb2dab2 100644 --- a/service/models/shopcart.py +++ b/service/models/shopcart.py @@ -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) diff --git a/service/routes.py b/service/routes.py index 9edcf32..cf4a0e1 100644 --- a/service/routes.py +++ b/service/routes.py @@ -145,15 +145,12 @@ 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: + 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)) diff --git a/tests/test_shopcart_model.py b/tests/test_shopcart_model.py index c9e95e4..69d6c75 100644 --- a/tests/test_shopcart_model.py +++ b/tests/test_shopcart_model.py @@ -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 From 8d0626d76e55095c22d9a6173358a430bb10706e Mon Sep 17 00:00:00 2001 From: Alvaro Perez Casinelli Date: Sun, 31 Mar 2024 04:15:15 +0000 Subject: [PATCH 2/2] Made edits to fix test coverage for query shopcart by price --- service/routes.py | 1 + tests/test_routes.py | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/service/routes.py b/service/routes.py index cf4a0e1..a63af83 100644 --- a/service/routes.py +++ b/service/routes.py @@ -148,6 +148,7 @@ def list_shopcarts(): # 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() diff --git a/tests/test_routes.py b/tests/test_routes.py index 8529809..ab55af5 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -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" ) @@ -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