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_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_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_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_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(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_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_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_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_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 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)
def post(self): """ Create a Shopcart """ logger.info("Request to create a shopcart") check_content_type("application/json") logger.debug('Payload = %s', api.payload) shopcart = None if 'user_id' in api.payload: shopcart = Shopcart.find_by_user(api.payload['user_id']).first() if shopcart is None: shopcart = Shopcart() shopcart.deserialize(api.payload) shopcart.create() logger.info("Shopcart with ID [%s] created.", shopcart.id) location_url = api.url_for(ShopcartResource, shopcart_id=shopcart.id, _external=True) return shopcart.serialize(), status.HTTP_201_CREATED, {"Location": location_url}