def test_update_shopcart_quantity(self): """ Update a Shopcart quantity """ # Add test product in database test_product = dict(user_id=1, product_id=1, quantity=5, price=12.00) data = json.dumps(test_product) resp = self.app.post('/shopcarts', data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # make it chekc if the quantity is number shopcart = Shopcart.find(1, 1) test_error = dict(user_id=1, product_id=1, quantity='a', price=12.00) data_error = json.dumps(test_error) resp = self.app.put('/shopcarts/{uid}/product/{pid}'.format( uid=shopcart.user_id, pid=shopcart.product_id), data=data_error, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) # make it chekc if the quantity is more than 0 shopcart = Shopcart.find(1, 1) test_error = dict(user_id=1, product_id=1, quantity=0, price=12.00) data_error = json.dumps(test_error) resp = self.app.put('/shopcarts/{uid}/product/{pid}'.format( uid=shopcart.user_id, pid=shopcart.product_id), data=data_error, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST) # update the test product quantity shopcart = Shopcart.find(1, 1) resp = self.app.put('/shopcarts/{uid}/product/{pid}'.format( uid=shopcart.user_id, pid=shopcart.product_id), data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) #Check quantity is updated to 3 new_json = json.loads(resp.data) self.assertEqual(new_json['quantity'], 5) resp = self.app.put('/shopcarts/999/product/999', data=data, content_type='application/json') self.assertRaises(NotFound)
def test_find_shopcart_entry(self): """ Find a Shopcart by user_id and product_id """ Shopcart(user_id=1, product_id=1, quantity=1, price=12.00).save() entry = Shopcart(user_id=1, product_id=2, quantity=1, price=15.00) entry.save() shopcart = Shopcart.find(entry.user_id, entry.product_id) self.assertIsNot(shopcart, None) self.assertEqual(shopcart.user_id, 1) self.assertEqual(shopcart.product_id, 2) self.assertEqual(shopcart.quantity, 1) self.assertEqual(shopcart.price, 15.00)
def test_update_a_shopcart_entry(self): """ Update a Shopcart entry """ shopcart = Shopcart(user_id=999, product_id=999, quantity=999, price=999.99) shopcart.save() self.assertEqual(shopcart.user_id, 999) # Change it an save it shopcart.quantity =888 shopcart.save() self.assertEqual(shopcart.user_id, 999) self.assertEqual(shopcart.product_id, 999) # Fetch it back and make sure the id hasn't changed # but the data did change item = Shopcart.find(999,999) self.assertEqual(item.quantity, 888)
def test_delete_product(self): """ Delete product in Shopcart """ # Add test product in database test_product = dict(user_id=1, product_id=1, quantity=1, price=12.00) data = json.dumps(test_product) resp = self.app.post('/shopcarts', data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Delet the test product shopcart = Shopcart.find(1, 1) resp = self.app.delete('/shopcarts/{uid}/product/{pid}'.format( uid=shopcart.user_id, pid=shopcart.product_id)) self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT)
def test_get_shopcart_product_info(self): """ Query quantity and price of a product shopcart by user_id and product_id """ # Add test product in database test_product = dict(user_id=10, product_id=1, quantity=1, price=12.00) data = json.dumps(test_product) resp = self.app.post('/shopcarts', data=data, content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Fetch info of product shopcart = Shopcart.find(10, 1) resp = self.app.get('/shopcarts/{}/product/{}'.format( shopcart.user_id, shopcart.product_id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) ans = json.loads(resp.data) self.assertEqual(ans['quantity'], shopcart.quantity) self.assertEqual(ans['price'], shopcart.price) resp = self.app.get('/shopcarts/999/product/999', content_type='application/json') self.assertRaises(NotFound)