def test_deserialize_an_order(self): """ Deserialization of an Order Item""" data = {"product_id": 1, "quantity": 1, "price": 5.0, "status": "PLACED"} order_item = OrderItem() order_item.deserialize(data) self.assertNotEqual(order_item, None) self.assertEqual(order_item.item_id, None) self.assertEqual(order_item.product_id, 1) self.assertEqual(order_item.quantity, 1) self.assertEqual(order_item.price, 5) self.assertEqual(order_item.status, "PLACED")
def test_serialize_an_order_item(self): """ Serialization of an Order Item """ order_item = OrderItem(product_id=1, quantity=1, price=5, status="PLACED") data = order_item.serialize() self.assertNotEqual(data, None) self.assertIn("item_id", data) self.assertEqual(data["item_id"], None) self.assertIn("product_id", data) self.assertEqual(data["product_id"], 1) self.assertIn("quantity", data) self.assertEqual(data["quantity"], 1) self.assertIn("price", data) self.assertEqual(data["price"], 5) self.assertIn("status", data) self.assertEqual(data["status"], "PLACED")
def test_update_an_order_not_exists(self): """ Update a non-existing Order """ order_item1 = OrderItem(product_id=1, quantity=1, price=5, status="PLACED") order_items = [order_item1] order = Order(id=1234567, customer_id=111, order_items=order_items) order.update() self.assertRaises(DataValidationError)
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
def test_init_order(self): """ Initialize an order and assert that it exists """ order_items = [OrderItem(product_id=1, quantity=1, price=5, status="PLACED")] order = Order(customer_id=123, order_items=order_items) self.assertTrue(order is not None) self.assertEqual(order.id, None) self.assertEqual(order.customer_id, 123) self.assertEqual(len(order.order_items), 1)
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_init_order(self): """ Create an order item and assert that it exists """ order_item = OrderItem(product_id=1, quantity=1, price=5, status="PLACED", order_id=1) self.assertTrue(order_item is not None) self.assertEqual(order_item.item_id, None) self.assertEqual(order_item.product_id, 1) self.assertEqual(order_item.price, 5) self.assertEqual(order_item.status, "PLACED") self.assertEqual(order_item.order_id, 1)
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 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_serialize_an_order(self): """ Serialization of an Order """ date = datetime.now order_item = 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_item, order_item2] order = Order(customer_id=123, created_date=date, order_items=order_items) data = order.serialize() self.assertNotEqual(data, None) self.assertIn("id", data) self.assertEqual(data["id"], None) self.assertIn("customer_id", data) self.assertEqual(data["customer_id"], 123) self.assertIn("created_date", data) self.assertEqual(data["created_date"], date) self.assertIn("order_items", data) self.assertEqual(data["order_items"], [order_item.serialize(), order_item2.serialize()])
def test_deserialize_bad_data_with_wrong_status_not_in_list(self): """ Deserialization of bad order item data with status as None """ data = {"product_id": 1, "quantity": 1, "price": 5.0, "status": "None"} order = OrderItem() self.assertRaises(DataValidationError, order.deserialize, data)
def test_create_order_with_no_customer_id(self): """ Create an order with no customer id """ order_item = OrderItem(product_id=1, quantity=1, price=5, status="PLACED") order_items = [order_item] order = Order(order_items=order_items) self.assertRaises(DataValidationError, order.create)
def test_repr_with_no_item_id(self): """ Create an order item and test it's repr with no item id """ order_item = OrderItem(product_id=1, quantity=1, price=5, status="PLACED", order_id=1) self.assertEqual(order_item.__repr__(), "<OrderItem None>")
def test_deserialize_bad_data(self): """ Deserialization of bad order item data """ data = "this is not a dictionary" order = OrderItem() self.assertRaises(DataValidationError, order.deserialize, data)
def test_deserialize_bad_data_with_keys_missing(self): """ Deserialization of bad order item data with few keys missing """ data = {"product_id": 1, "status": "PLACED"} order = OrderItem() self.assertRaises(DataValidationError, order.deserialize, data)
def test_deserialize_bad_data_with_wrong_price(self): """ Deserialization of bad order item data with price not as float/int """ data = {"product_id": 1, "quantity": 1, "price": "5.0", "status": "PLACED"} order = OrderItem() self.assertRaises(DataValidationError, order.deserialize, data)
def test_repr(self): """ Create an order item and test it's repr """ order_item = OrderItem(item_id=1, product_id=1, quantity=1, price=5, status="PLACED", order_id=1) self.assertEqual(order_item.__repr__(), "<OrderItem 1>")