def test_find_shopcart(self): """ Find a Shopcart by id """ data = {"id": 1, "user_id": 120,} Shopcart(id=1, user_id=120).create() shopcart_queried = Shopcart.find(1) self.assertEqual(data["id"], shopcart_queried.id) self.assertEqual(data["user_id"], shopcart_queried.user_id)
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_delete_shopcart_item(self): """ Delete an shopcarts item """ shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) item = self._create_item() shopcart = self._create_shopcart(items=[item]) shopcart.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(shopcart.id, 1) shopcarts = Shopcart.all() self.assertEqual(len(shopcarts), 1) # Fetch it back shopcarts = Shopcart.find(shopcart.id) item = shopcart.items_list[0] item.delete() shopcart.save() # Fetch it back again shopcart = Shopcart.find(shopcart.id) self.assertEqual(len(shopcart.items_list), 0)
def delete(self, shopcart_id): """ Delete a Shopcart This endpoint will delete a Shopcart based the id specified in the path """ logger.info('Request to delete Shopcart with id: %s', shopcart_id) item = Shopcart.find(shopcart_id) if item: item.delete() logger.info('Shopcart with id: %s has been deleted', shopcart_id) return make_response("", status.HTTP_204_NO_CONTENT)
def test_add_item_to_shopcart(self): """ Create a shopcart with an item and add it to the database """ shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) shopcart = self._create_shopcart() item = self._create_item() shopcart.items_list.append(item) shopcart.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(shopcart.id, 1) shopcarts = Shopcart.all() self.assertEqual(len(shopcarts), 1) new_shopcart = Shopcart.find(shopcart.id) self.assertEqual(shopcart.items_list[0].item_name, item.item_name) item2 = self._create_item() shopcart.items_list.append(item2) shopcart.save() new_shopcart = Shopcart.find(shopcart.id) self.assertEqual(len(shopcart.items_list), 2) self.assertEqual(shopcart.items_list[1].item_name, item2.item_name)
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 test_find_shopcart_with_non_existing_shopcart(self): """ Find a Shopcart that doesn't exist """ shopcart_queried = Shopcart.find(1) self.assertIsNone(shopcart_queried)