def test_delete_an_order(self): """ Delete an Order """ order_items1 = [OrderItem(product_id=1, quantity=1, price=5.0, status="PLACED")] order = Order(customer_id=111, order_items=order_items1) order.create() self.assertEqual(len(Order.all()), 1) # delete the order and make sure it isn't in the database order.delete() self.assertEqual(len(Order.all()), 0)
def test_find_order(self): """ Find an Order by ID """ order_items1 = [OrderItem(product_id=1, quantity=1, price=5, status="PLACED")] order1 = Order(customer_id=111, order_items=order_items1) order1.create() order = Order.find(order1.id) self.assertIsNot(order, None) self.assertEqual(order.id, order1.id) self.assertEqual(order.customer_id, 111) self.assertEqual(len(order.order_items), 1) self.assertEqual(order.order_items[0].product_id, 1)
def test_update_an_order(self): """ Update an existing Order """ order_item1 = OrderItem(product_id=1, quantity=1, price=5, status="PLACED") order_items = [order_item1] order = Order(customer_id=111, order_items=order_items) order.create() self.assertTrue(order.id is not None) order.customer_id = 234 order.update() new_order = Order.find(order.id) self.assertEqual(new_order.id, order.id) self.assertEqual(new_order.customer_id, 234)
def create_orders(): """ Creates an Order This endpoint will create an Order based the data in the body that is posted """ app.logger.info("Request to create an Order") check_content_type("application/json") order = Order() order.deserialize(request.get_json()) order.create() message = order.serialize() location_url = url_for("get_orders", order_id=order.id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {"Location": location_url})
def test_create_order(self): """ Create an order with a single item in the database """ order_item = OrderItem(product_id=1, quantity=1, price=5, status="PLACED") order_items = [order_item] order = Order(customer_id=123, order_items=order_items) self.assertTrue(order is not None) self.assertEqual(order.id, None) self.assertEqual(len(order.order_items), 1) self.assertEqual(order.order_items[0].item_id, None) order.create() self.assertEqual(order.id, 1) self.assertEqual(order.customer_id, 123) self.assertEqual(len(order.order_items), 1) self.assertEqual(order.order_items[0].item_id, 1) self.assertEqual(order.order_items[0].order_id, 1)
def test_create_order_with_multiple_items(self): """ Create an order with multiple order items """ order_item1 = OrderItem(product_id=1, quantity=1, price=5, status="PLACED") order_item2 = OrderItem(product_id=2, quantity=1, price=5, status="PLACED") order_items = [order_item1, order_item2] order = Order(customer_id=123, order_items=order_items) order.create() self.assertTrue(order.id is not None) order_id = order.id self.assertEqual(order.customer_id, 123) self.assertEqual(len(order.order_items), 2) i = order.order_items[0].item_id for j in range(0, len(order.order_items)): self.assertEqual(order.order_items[j].item_id, i) self.assertEqual(order.order_items[j].order_id, order_id) i += 1