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_remove_all(self): """Remove all shopcart items""" Shopcart(product_id=3, customer_id=10, quantity=2, price=5.0, text="pen", state=1).save() Shopcart(product_id=4, customer_id=12, quantity=2, price=5.0, text="paper", state=1).save() self.assertEqual(len(Shopcart.all()),2) Shopcart.remove_all() self.assertEqual(len(Shopcart.all()),0)
def test_delete(self): """ Delete a item """ item = Shopcart(product_id = 3, customer_id = 4) item.save() self.assertEqual(len(Shopcart.all()) , 1) # delete item and make sure it isn't in the database item.delete() self.assertEqual(len(Shopcart.all()), 0)
def test_add_a_shopcart(self): """ Create a shopcart and add it to 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) shopcarts = Shopcart.all() self.assertEqual(len(shopcarts), 1)
def test_delete_an_shopcart(self): """ Delete an shopcart from the database """ shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) shopcart = self._create_shopcart() 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) shopcart = shopcarts[0] shopcart.delete() shopcarts = Shopcart.all() self.assertEqual(len(shopcarts), 0)
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_all_shopcarts(self): """ Get all Shopcarts""" count = 5 for i in range(1, count + 1): Shopcart(id=i, user_id=(i + 1)).create() shopcarts_queried = Shopcart.all() self.assertEqual(len(shopcarts_queried), count)
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("/api/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 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 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_find_by_item_name(self): """ Find by item name """ shopcarts = Shopcart.all() self.assertEqual(shopcarts, []) shopcart = self._create_shopcart() item = self._create_item() shopcart.items_list.append(item) shopcart.create() # Fetch it back by name same_item = Item.find_by_item_name(item.item_name)[0] self.assertEqual(same_item.id, item.id) self.assertEqual(same_item.item_name, item.item_name) self.assertEqual(same_item.item_quantity, item.item_quantity) self.assertEqual(same_item.item_price, item.item_price)
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): """ Returns all of the Shopcarts """ logger.info('Request to list Shopcarts...') args = shopcart_args.parse_args() if args['user_id']: logger.info('Find by user') shopcarts = Shopcart.find_by_user(args['user_id']) else: logger.info('Find all') shopcarts = Shopcart.all() results = [shopcart.serialize() for shopcart in shopcarts] logger.info('[%s] Shopcarts returned', len(results)) return results, status.HTTP_200_OK
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_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)