def get(self): """ Returns all of the ShopcartItems """ logger.info('Request to list ShopcartItems...') args = shopcart_item_args.parse_args() if args['sku']: logger.info('Find by sku') shopcart_items = ShopcartItem.find_by_sku(args['sku']) elif args['name']: logger.info('Find by name') shopcart_items = ShopcartItem.find_by_name(args['name']) elif args['price']: logger.info('Find by price') shopcart_items = ShopcartItem.find_by_price(args['price']) elif args['amount']: logger.info('Find by amount') shopcart_items = ShopcartItem.find_by_amount(args['amount']) else: logger.info('Find all') shopcart_items = ShopcartItem.all() results = [shopcart_item.serialize() for shopcart_item in shopcart_items] logger.info('[%s] Shopcart Items returned', len(results)) return results, status.HTTP_200_OK
def test_find_by_shopcart_id_multiple(self): """ Find Shopcart Items by Shopcart id for Shopcart with multiple items """ shopcart = Shopcart().deserialize({"user_id": 12345}) shopcart.create() ShopcartItem(id=1, sid=shopcart.id, sku=3, name="obj 1", price=4, amount=5).create() ShopcartItem(id=6, sid=shopcart.id, sku=7, name="obj 2", price=8, amount=9).create() items_queried = ShopcartItem.find_by_shopcartid(shopcart.id) self.assertEqual(len(items_queried), 2)
def test_find_by_amount(self): """ Find Shopcart Items by amount """ shopcart = Shopcart().deserialize({"user_id": 12345}) shopcart.create() ShopcartItem(sid=shopcart.id, sku=101, name="printer", price=101.29, amount=1).create() ShopcartItem(sid=shopcart.id, sku=201, name="printer", price=101.29, amount=10).create() shopcart_items = ShopcartItem.find_by_amount(10) self.assertEqual(len(shopcart_items), 1) self.assertEqual(shopcart_items[0].amount, 10)
def test_update_a_shopcart_item_without_id(self): """ Update a shopcart item """ shopcart = Shopcart(user_id=12345) shopcart.create() shopcart_item = ShopcartItem(sid=shopcart.id, sku=5000, name="soap", price=2.23, amount=3) shopcart_item.create() # Change it an update it shopcart_item.id = None self.assertRaises(DataValidationError, shopcart_item.update)
def test_find_by_sku_and_sid(self): """ Find Shopcart Items by shopcart id and sku id """ shopcart_1 = Shopcart().deserialize({"user_id": 12345}) shopcart_1.create() shopcart_2 = Shopcart().deserialize({"user_id": 1235}) shopcart_2.create() sku = 3 ShopcartItem(id=1, sid=shopcart_1.id, sku=sku, name="obj 1", price=8, amount=5).create() ShopcartItem(id=6, sid=shopcart_2.id, sku=sku, name="obj 1", price=8, amount=9).create() item_queried = ShopcartItem.find_by_sku_and_sid(sku, shopcart_1.id) self.assertEqual(item_queried.amount, 5)
def test_find_by_sku(self): """ Find Shopcart Items by sku """ shopcart_1 = Shopcart().deserialize({"user_id": 12345}) shopcart_1.create() shopcart_2 = Shopcart().deserialize({"user_id": 12345}) shopcart_2.create() ShopcartItem(sid=shopcart_1.id, sku=101, name="printer", price=101.29, amount=1).create() ShopcartItem(sid=shopcart_2.id, sku=101, name="printer", price=101.29, amount=10).create() ShopcartItem(sid=shopcart_1.id, sku=201, name="printer", price=101.29, amount=1).create() shopcart_items = ShopcartItem.find_by_sku(101) self.assertEqual(len(shopcart_items), 2) self.assertEqual(shopcart_items[0].sku, 101)
def test_deserialize_a_shopcart_item(self): """ Test deserialization of a ShopcartItem """ data = {"id": 1, "sid": 202, "sku": 101, "name": "printer", "price": 101.29, "amount": 1} shopcart_item = ShopcartItem() shopcart_item.deserialize(data) self.assertNotEqual(shopcart_item, None) self.assertEqual(shopcart_item.id, 1) self.assertEqual(shopcart_item.sid, 202) self.assertEqual(shopcart_item.sku, 101) self.assertEqual(shopcart_item.name, "printer") self.assertEqual(shopcart_item.price, 101.29) self.assertEqual(shopcart_item.amount, 1)
def test_find_by_shopcart_id(self): """ Find Shopcart Items by Shopcart id for Shopcart with single item """ shopcart = Shopcart().deserialize({"user_id": 12345}) shopcart.create() data = {"id": 1, "sid": shopcart.id, "sku": 150, "name": "test obj1", "price": 100, "amount": 1} ShopcartItem(id=data["id"], sid=data["sid"], sku=data["sku"], name=data["name"], price=data["price"], amount=data["amount"]).create() item_queried = ShopcartItem.find_by_shopcartid(data["sid"])[0] self.assertEqual(item_queried.id, data["id"]) self.assertEqual(item_queried.sid, data["sid"]) self.assertEqual(item_queried.sku, data["sku"]) self.assertEqual(item_queried.name, data["name"]) self.assertEqual(item_queried.price, data["price"]) self.assertEqual(item_queried.amount, data["amount"])
def test_delete_shopcart_items(self): """Delete a Shopcart Item""" shopcart = self._create_shopcarts(1)[0] shopcart_id = shopcart.id self.assertEqual(len(ShopcartItem.all()), 0) shopcart_item = self._create_shopcart_items(1, shopcart_id)[0] self.assertEqual(len(ShopcartItem.all()), 1) resp = self.app.delete("/api/shopcarts/{}/items/{}".format( shopcart_id, shopcart_item.id), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) self.assertEqual(len(ShopcartItem.all()), 0)
def put(self, shopcart_id, item_id): """ Update a Shopcart item This endpoint will update a Shopcart item based the body that is posted """ logger.info("Request to update Shopcart item with id: %s", item_id) check_content_type("application/json") shopcart_item = ShopcartItem.find(item_id) if shopcart_item is None or shopcart_item.sid != shopcart_id: logger.info( "Shopcart item with ID [%s] not found in shopcart [%s].", item_id, shopcart_id ) api.abort( status.HTTP_404_NOT_FOUND, "Shopcart item with id '{}' was not found.".format(item_id) ) data = api.payload data["sid"] = shopcart_id data["id"] = item_id shopcart_item.deserialize(data) shopcart_item.update() logger.info("Shopcart item with ID [%s] updated.", shopcart_item.id) return shopcart_item.serialize(), status.HTTP_200_OK
def test_place_order(self): """ Test sending shopcart order """ test_shopcart = ShopcartFactory() resp = self.app.post("/api/shopcarts", json={"user_id": test_shopcart.user_id}, content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_201_CREATED) shopcart_id = resp.json["id"] shopcart_items = self._create_shopcart_items(10, shopcart_id) test_sku = shopcart_items[0].sku sku_shopcart_items = [ shopcart_item for shopcart_item in shopcart_items if shopcart_item.sku == test_sku ] resp = self.app.get("/api/shopcarts/items", query_string="sku={}".format(test_sku)) self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() self.assertEqual(len(data), len(sku_shopcart_items)) resp = self.app.put( "/api/shopcarts/{}/place-order".format(shopcart_id), content_type="application/json", ) self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) self.assertEqual(len(Shopcart.all()), 0) self.assertEqual(len(ShopcartItem.all()), 0)
def test_delete_a_shopcart_item(self): """Delete a shopcart item""" shopcart = Shopcart(user_id=12345) shopcart.create() self.assertEqual(shopcart.id, 1) self.assertEqual(len(ShopcartItem.all()), 0) shopcart_item = ShopcartItem(sid=1, sku=5000, name="soap", price=2.23, amount=3) shopcart_item.create() self.assertEqual(shopcart_item.id, 1) self.assertEqual(len(ShopcartItem.all()), 1) shopcart_item.delete() self.assertEqual(len(ShopcartItem.all()), 0)
def test_shopcart_item_creation_using_add(self): """ Create a shopcart item and add it to the database using add method""" shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) shopcart = Shopcart(user_id=12345) self.assertTrue(shopcart is not None) self.assertEqual(shopcart.id, None) shopcart.create() self.assertEqual(shopcart.id, 1) shopcart_item = ShopcartItem(sid=shopcart.id, sku=5000, name="soap", price=2.23, amount=3) shopcart_item.add() self.assertTrue(shopcart_item is not None) self.assertEqual(shopcart_item.sid, 1) self.assertEqual(shopcart_item.sku, 5000) self.assertEqual(shopcart_item.name, "soap") self.assertEqual(shopcart_item.price, 2.23) self.assertEqual(shopcart_item.amount, 3)
def test_serialize_a_shopcart_item(self): """ Test serialization of a ShopcartItem """ shopcart_item = ShopcartItem(sid=100, sku=5000, name="soap", price=2.23, amount=3) data = shopcart_item.serialize() self.assertNotEqual(data, None) self.assertIn("id", data) self.assertEqual(data["id"], None) self.assertIn("sid", data) self.assertEqual(data["sid"], 100) self.assertIn("sku", data) self.assertEqual(data["sku"], 5000) self.assertIn("name", data) self.assertEqual(data["name"], "soap") self.assertIn("price", data) self.assertEqual(data["price"], 2.23) self.assertIn("amount", data) self.assertEqual(data["amount"], 3)
def put(self, shopcart_id): """ Place Order for a Shopcart This endpoint will place an order for a Shopcart based the id specified in the path """ logger.info('Request to place order for Shopcart with id: %s', shopcart_id) shopcart = Shopcart.find(shopcart_id) if not shopcart: logger.info("Shopcart with ID [%s] is does not exist.", shopcart_id) api.abort( status.HTTP_404_NOT_FOUND, "Shopcart with ID [%s] is does not exist." % shopcart_id ) shopcart_items = ShopcartItem.find_by_shopcartid(shopcart_id) if shopcart_items is None or len(shopcart_items) == 0: logger.info("Shopcart with ID [%s] is empty.", shopcart_id) api.abort( status.HTTP_404_NOT_FOUND, "Shopcart with ID [%s] is empty." % shopcart_id ) shopcart_items_list = [item.serialize() for item in shopcart_items] # once we have the list of shopcart items we can send in JSON format to the orders team #add the order status as PLACED for a new order order_items= [] for item in shopcart_items_list: order_item = {} order_item["item_id"] = int(item["id"]) order_item["product_id"] = int(item["sku"]) order_item["quantity"] = int(item["amount"]) order_item["price"] = item["price"] order_item["status"] = "PLACED" order_items.append(order_item) order = { "customer_id": int(shopcart.serialize()["user_id"]), "order_items": order_items, } payload = json.dumps(order) headers = {'content-type': 'application/json'} res = requests.post( ORDER_ENDPOINT, data=payload, headers=headers ) logger.info('Put Order response %d %s', res.status_code, res.text) if res.status_code != 201: api.abort( status.HTTP_400_BAD_REQUEST, "Unable to place order for shopcart [%s]." % shopcart_id ) shopcart.delete() logger.info('Shopcart with id: %s has been deleted', shopcart_id) return make_response("", status.HTTP_204_NO_CONTENT)
def test_create_a_shopcart_item(self): """ Create a ShopcartItem and assert that it exists """ shopcart_item = ShopcartItem(sid=100, sku=5000, name="soap", price=2.23, amount=3) self.assertTrue(shopcart_item is not None) self.assertEqual(shopcart_item.id, None) self.assertEqual(shopcart_item.sid, 100) self.assertEqual(shopcart_item.sku, 5000) self.assertEqual(shopcart_item.name, "soap") self.assertEqual(shopcart_item.price, 2.23) self.assertEqual(shopcart_item.amount, 3)
def test_delete_shopcart(self): """Delete a Shopcart and everything in it""" self.assertEqual(len(Shopcart.all()), 0) shopcart = self._create_shopcarts(1)[0] self.assertEqual(len(Shopcart.all()), 1) self.assertEqual(len(ShopcartItem.all()), 0) self._create_shopcart_items(5, shopcart.id) self.assertEqual(len(ShopcartItem.all()), 5) resp = self.app.delete("/shopcarts/{}".format(shopcart.id), content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) self.assertEqual(len(Shopcart.all()), 0) self.assertEqual(len(ShopcartItem.all()), 0)
def get(self, shopcart_id): """ Get information of a shopcart This endpoint will return items in the shop cart """ logger.info("Request to get items in a shopcart") shopcart_items = ShopcartItem.find_by_shopcartid(shopcart_id) result = [item.serialize() for item in shopcart_items] logger.info("Fetched items for Shopcart with ID [%s].", shopcart_id) return result, status.HTTP_200_OK
def test_add_a_shopcart_item(self): """ Create a shopcart item and add it to the database """ shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) shopcart = Shopcart(user_id=12345) self.assertTrue(shopcart is not None) self.assertEqual(shopcart.id, None) shopcart.create() self.assertEqual(shopcart.id, 1) shopcart_item = ShopcartItem(sid=100, sku=5000, name="soap", price=2.23, amount=3) self.assertRaises(DataValidationError, shopcart_item.create) shopcart_item = ShopcartItem(sid=1, sku=5000, name="soap", price=2.23, amount=3) self.assertTrue(shopcart_item is not None) self.assertEqual(shopcart_item.id, None) self.assertEqual(shopcart_item.sid, 1) self.assertEqual(shopcart_item.sku, 5000) self.assertEqual(shopcart_item.name, "soap") self.assertEqual(shopcart_item.price, 2.23) self.assertEqual(shopcart_item.amount, 3)
def test_add_shopcart_item_with_not_existing_cart(self): """ Test using add shopcart_item method when shopcart doesnt exists""" shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) shopcart = Shopcart(user_id=12345) self.assertTrue(shopcart is not None) self.assertEqual(shopcart.id, None) shopcart.create() self.assertEqual(shopcart.id, 1) shopcart_item = ShopcartItem(sid=1000, sku=5000, name="soap", price=2.23, amount=3) self.assertRaises(DataValidationError, shopcart_item.add)
def test_update_a_shopcart_item(self): """ Update a shopcart item """ shopcart = Shopcart(user_id=12345) shopcart.create() shopcart_item = ShopcartItem(sid=shopcart.id, sku=5000, name="soap", price=2.23, amount=3) shopcart_item.create() self.assertEqual(shopcart_item.id, 1) # Change it an update it shopcart_item.name = "soap" shopcart_item.update() self.assertEqual(shopcart_item.id, 1) # Fetch it back and make sure the id hasn't changed # but the data did change shopcart_item = ShopcartItem.all() self.assertEqual(len(shopcart_item), 1) self.assertEqual(shopcart_item[0].name, "soap")
def delete(self, shopcart_id, item_id): """ Delete a ShopcartItem This endpoint will delete a ShopcartItem based the id specified in the path """ logger.info( 'Request to delete ShopcartItem with id: %s from Shopcart %s', item_id, shopcart_id ) check_content_type("application/json") shopcart_item = ShopcartItem.find(item_id) if shopcart_item is not None and shopcart_item.sid == shopcart_id: shopcart_item.delete() logger.info('ShopcartItem with id: %s has been deleted', item_id) return "", status.HTTP_204_NO_CONTENT
def get(self, shopcart_id): """ Gets information about a Shopcart This endpoint will get information about a shopcart """ logger.info("Request to get information of a shopcart") shopcart = Shopcart.find(shopcart_id) if shopcart is None: logger.info("Shopcart with ID [%s] not found.", shopcart_id) api.abort( status.HTTP_404_NOT_FOUND, "Shopcart with id '{}' was not found.".format(shopcart_id) ) shopcart_items = ShopcartItem.find_by_shopcartid(shopcart_id) response = shopcart.serialize() response["items"] = [item.serialize() for item in shopcart_items] logger.info("Shopcart with ID [%s] fetched.", shopcart.id) return response, status.HTTP_200_OK
def get(self, shopcart_id, item_id): """ Get a shopcart item This endpoint will return an item in the shop cart """ logger.info("Request to get an item in a shopcart") check_content_type("application/json") shopcart_item = ShopcartItem.find(item_id) if shopcart_item is None or shopcart_item.sid != shopcart_id: logger.info( "Shopcart item with ID [%s] not found in shopcart [%s].", item_id, shopcart_id ) api.abort( status.HTTP_404_NOT_FOUND, "Shopcart item with ID [%s] not found in shopcart [%s]." % (item_id, shopcart_id) ) logger.info("Fetched shopcart item with ID [%s].", item_id) return shopcart_item.serialize(), status.HTTP_200_OK
def post(self, shopcart_id): """ Create a new Shopcart Item """ logger.info("Request to create a shopcart item") check_content_type("application/json") shopcart_item = ShopcartItem() data = request.get_json() if "id" in data: data.pop("id") data["sid"] = shopcart_id shopcart_item.deserialize(data) shopcart_item.add() location_url = api.url_for(ShopcartItemResource, shopcart_id=shopcart_item.sid, item_id=shopcart_item.id, _external=True) logger.info("ShopcartItem with ID [%s] created.", shopcart_item.id) return shopcart_item, status.HTTP_201_CREATED, {"Location": location_url}
def test_deserialize_shopcart_item_value_error(self): """ Test deserialization of bad data with value error for a ShopcartItem """ data = {"sid": 123, "sku": 13435, "price": 2.2, "name": "test item", "amount": 0} shopcart_item = ShopcartItem() self.assertRaises(DataValidationError, shopcart_item.deserialize, data)
def init_db(): """ Initialies the SQLAlchemy app """ Shopcart.init_db(app) ShopcartItem.init_db(app) logger.info("Database has been initialized!")
def test_deserialize_shopcart_item_key_error(self): """ Test deserialization of bad data with key error for a ShopcartItem """ data = {"id": 1} shopcart_item = ShopcartItem() self.assertRaises(DataValidationError, shopcart_item.deserialize, data)
def test_delete_a_shopcart(self): """Delete a shopcart and everything in it""" self.assertEqual(len(Shopcart.all()), 0) shopcart = Shopcart(user_id=12345) shopcart.create() self.assertEqual(shopcart.id, 1) self.assertEqual(len(Shopcart.all()), 1) self.assertEqual(len(ShopcartItem.all()), 0) shopcart_item = ShopcartItem(sid=1, sku=5000, name="soap", price=2.23, amount=3) shopcart_item.create() self.assertEqual(shopcart_item.id, 1) shopcart_item = ShopcartItem(sid=1, sku=5001, name="shampoo", price=3.77, amount=1) shopcart_item.create() self.assertEqual(shopcart_item.id, 2) self.assertEqual(len(ShopcartItem.all()), 2) shopcart.delete() self.assertEqual(len(ShopcartItem.all()), 0) self.assertEqual(len(Shopcart.all()), 0)
def test_find_by_shopcart_id_with_no_items(self): """ Find Shopcart Items by empty Shopcart """ item_queried = ShopcartItem.find_by_shopcartid(10) self.assertEqual(len(item_queried), 0)