class TestRemoveFromCart(unittest.TestCase): def setUp(self): self.products = [] self.cart = Cart() for i in range(1,10): product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00) product.put() product.add_stock("test_product_"+str(i),10) self.products.append("test_product_"+str(i)) p = Product.get_by_key_name("test_product_"+str(i)) self.cart.add_item(CartItem(p), random.randint(1,10)) random.shuffle(self.products) def test_totals_with_delivery(self): print '----------------- TEST RfB TOTALS --------------------' self.cart.delivery = {'price':2.5,'tax':15.0,'name':'testDelivery'} self.cart.recalc() delivery_gross = self.cart.delivery['price'] + self.cart.delivery['price'] * self.cart.delivery['tax']/100 delivery_tax = self.cart.delivery['price'] * self.cart.delivery['tax'] / 100 delivery_net = self.cart.delivery['price'] print 'DELIVERY ','price:', self.cart.delivery['price'], 'plus tax:', delivery_gross, 'tax percent:', self.cart.delivery['tax'],'price tax:', delivery_tax sub_total_tax_items = self.cart.sub_total_tax_items #TAX Items sub_total_price = self.cart.sub_total_price #GROSS Items total = self.cart.total #GROSS Items + GROSS Delivery total_without_tax = self.cart.total_without_tax #NET Items + NET Delivery total_tax = self.cart.total_tax #TAX items + TAX Delivery for i in range(3): qty = random.randint(1,10) qty=1 p = Product.get_by_key_name(self.products[i]) print self.products[i],'price:', p.price, 'plus tax:', p.price + p.price * p.tax_percent / 100,'tax percent: ',p.tax_percent, 'price tax:',p.price * p.tax_percent / 100, 'qty:',qty sub_total_tax_items -= (p.price * p.tax_percent / 100)*qty sub_total_price -= (p.price + p.price * p.tax_percent / 100)*qty total -= (p.price + p.price * p.tax_percent / 100)*qty total_without_tax -= (p.price)*qty total_tax -= (p.price * p.tax_percent / 100)*qty self.cart.remove_item(CartItem(p),qty) print 'sub_total_tax_items: ', sub_total_tax_items, ' CART sub_total_tax_items: ',self.cart.sub_total_tax_items self.assertAlmostEqual(sub_total_tax_items, self.cart.sub_total_tax_items,7, 'Error calculating sub_total_tax_items') print 'sub_total_price: ', sub_total_price, ' CART sub_total_price: ',self.cart.sub_total_price self.assertEqual(round(sub_total_price,2), round(self.cart.sub_total_price,2), 'Error calculating sub_total_price') print 'total: ', total, ' CART total: ',self.cart.total self.assertEqual(round(total,2), round(self.cart.total,2), 'Error calculating total') print 'total_without_tax: ', total_without_tax, ' CART total_without_tax: ',self.cart.total_without_tax self.assertEqual(round(total_without_tax,2), round(self.cart.total_without_tax,2), 'Error calculating total_without_tax') print 'total_tax: ', total_tax, ' CART total_tax: ',self.cart.total_tax self.assertEqual(round(total_tax,2), round(self.cart.total_tax,2), 'Error calculating total_tax') print '----------------- END TEST -----------------------'
def test_items(self): item_shorts = Item(name='shorts', price=20, description='Some nice shorts') cart4 = Cart() self.assertEqual(cart4.cart_id, 4) cart4.add_item(item_shorts, 2) shorts = cart4.get_item('shorts') self.assertEqual(shorts['num_items'], 2) self.assertEqual(shorts['item'].description, 'Some nice shorts') cart4.add_item(item_shorts, 3) self.assertEqual(shorts['num_items'], 5) total_price_shorts = cart4.get_item_total('shorts') self.assertEqual(total_price_shorts, 100)
def test_user(self): item_shorts = Item(name='shorts', price=20, description='Some nice shorts') item_tshirt = Item(name='tshirt', price=15, description='A nice t-shirt') item_vest = Item(name='vest', price=15, description='A nice vest') user = User('Mike') self.assertIsInstance(user, User) cart5 = Cart() cart5.add_item(item_shorts, 2) cart5.add_item(item_tshirt, 3) cart5.add_item(item_vest, 1) user.add_cart(cart5) retrieved_cart5 = user.get_cart_by_id(cart_id=cart5.cart_id) self.assertIsInstance(retrieved_cart5, Cart) shorts = retrieved_cart5.get_item('shorts') self.assertEqual(shorts['num_items'], 2) cart6 = Cart() cart6.add_item(item_shorts, 1) cart6.add_item(item_tshirt, 2) cart6.add_item(item_vest, 3) user.add_cart(cart6) retrieved_cart6 = user.get_cart_by_id(cart_id=cart6.cart_id) self.assertIsInstance(retrieved_cart6, Cart) vest = retrieved_cart6.get_item('vest') self.assertEqual(vest['num_items'], 3) cart5_vest = retrieved_cart5.get_item('vest') self.assertEqual(cart5_vest['num_items'], 1) self.assertFalse(retrieved_cart5.get_item('pants'))
def main(): item_shorts = Item(name='shorts', price=10, description='Some nice shorts') item_tshirt = Item(name='tshirt', price=20, description='A nice t-shirt') item_pants = Item(name='pants', price=30, description='Lovely pants') # create user user = User('Eric') # create a new cart cart = Cart() user.add_cart(cart) # Eric does some shopping cart.add_item(item_shorts, 4) cart.add_item(item_tshirt, 2) cart.add_item(item_pants, 3) # we want to display his cart retrieved_cart = user.get_cart_by_id(cart_id=cart.cart_id) print(retrieved_cart) pants = retrieved_cart.get_item('pants') print(pants) cart2 = Cart() cart2.add_item(item_shorts, 1) cart2.add_item(item_tshirt, 2) cart2.add_item(item_pants, 3) user.add_cart(cart2) retrieved_cart2 = user.get_cart_by_id(cart_id=2) tshirt = retrieved_cart2.get_item('tshirt') print(tshirt) tshirt_total = cart2.get_item_total('tshirt') print(tshirt_total)
class CartTest(unittest.TestCase): def setUp(self): self.cart = Cart() self.inv = Inventory() def tearDown(self): self.inv.close() def test_add_items_to_basket(self): # Test adding item, make sure it's in cart and inventory updates to reflect that old_qty= self.inv.view_qty('item050') self.cart.add_item('item050', 10) self.assertEqual(self.cart.basket('item050'), 10) self.assertEqual(self.inv.view_qty('item050'), old_qty-10) self.inv.update_qty('item050', 500) # Test adding more to cart than inventory contains self.cart._basket['item050'] = 0 # Temporary kluge self.cart.add_item('item050', 600) self.assertEqual(self.cart.basket('item050'), 500) self.assertEqual(self.inv.view_qty('item050'), 0) self.inv.update_qty('item050', 500) def test_remove_items_from_basket(self): self.cart.add_item('item050', 10) self.cart.remove_item('item050',5) self.assertEqual(self.cart.basket('item050'), 5) self.assertEqual(self.inv.view_qty('item050'), 495) def test_edit_items(self): # Change to a quantity greater than you already have self.cart.edit_item('item050', 50) self.cart.edit_item('item050', 70) self.assertEqual(self.cart.basket('item050'), 70) self.assertEqual(self.inv.view_qty('item050'), 430) self.cart.remove_item('item050', 70) # Add more than are in inventory self.cart.edit_item('item050', 1000) self.assertEqual(self.inv.view_qty('item050'), 0) self.assertEqual(self.cart.basket('item050'), 500) self.cart.remove_item('item050', 500) # Change to a quantity less than what you already have self.cart.add_item('item050', 100) self.cart.edit_item('item050', 50) self.assertEqual(self.inv.view_qty('item050'), 450) self.assertEqual(self.cart.basket('item050'), 50) self.cart.remove_item('item050', 50) def test_items_total(self): self.cart.add_item('item100', 100) cost = 100 * self.inv.view_price('item100') self.assertEqual(self.cart.items_total(), cost) self.cart.remove_item('item100', 100) def test_list_items(self): self.cart.add_item('item100', 100) # Test listing sorted by item name for _tuple in self.cart.list_items('item'): self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100'))) # Test listing sorted by qty for _tuple in self.cart.list_items('price'): self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100'))) # Test listing sorted by price self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100'))) self.cart.remove_item('item100', 100)
class TestAddToCart(unittest.TestCase): def setUp(self): self.products = [] for i in range(1,10): product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00) product.put() product.add_stock("test_product_"+str(i),10) self.products.append("test_product_"+str(i)) self.cart = Cart() random.shuffle(self.products) def test_price_cart(self): #3 random products in cart value = 0.0 for i in range(3): p = Product.get_by_key_name(self.products[i]) #print p.price, value += p.price self.cart.add_item(CartItem(p),1) #print value, self.cart.total_without_tax, self.cart.sub_total_tax, self.cart.sub_total_price, self.cart.total self.assertEqual(round(value,2), round(self.cart.total_without_tax,2), 'price not matching') def test_price_tax_cart(self): value_tax_included = 0.0 for i in range(3): p = Product.get_by_key_name(self.products[i]) cart_item = CartItem(p) qty = random.randint(1,10) #qty = 1 value_tax_included += cart_item.final_price*qty self.cart.add_item(cart_item, qty) #print value_tax_included, self.cart.total_without_tax, self.cart.sub_total_tax, self.cart.sub_total_price, self.cart.total self.assertEqual(round(value_tax_included,2), round(self.cart.sub_total_price,2), 'taxes not being calculated well') def test_reserved_stock(self): #3 random products in cart value = 0.0 result = 0 items_no = 0 for i in range(3): qty = random.randint(1,10) print 'product: ', self.products[i], ' to reserve: ',qty, if Product.reserve(self.products[i],qty): p = Product.get_by_key_name(self.products[i]) #print p.stock, p.reserved self.cart.add_item(CartItem(p),qty) result += qty items_no +=1 #p = Product.get_by_key_name(self.products[i]) print self.cart.total_different_items, items_no self.assertEqual(self.cart.total_different_items, items_no, 'Error in total different items') self.assertEqual(self.cart.total_items, result, 'Error in total_items') def test_totals_with_delivery(self): print '----------------- TEST AtB TOTALS --------------------' self.cart.delivery = {'price':2.5,'tax':15.0,'name':'testDelivery'} delivery_gross = self.cart.delivery['price'] + self.cart.delivery['price'] * self.cart.delivery['tax']/100 delivery_tax = self.cart.delivery['price'] * self.cart.delivery['tax'] / 100 delivery_net = self.cart.delivery['price'] print 'DELIVERY ','price:', self.cart.delivery['price'], 'plus tax:', delivery_gross, 'tax percent:', self.cart.delivery['tax'],'price tax:', delivery_tax sub_total_tax_items = 0 #TAX Items sub_total_price = 0 #GROSS Items total = delivery_gross #GROSS Items + GROSS Delivery total_without_tax = delivery_net #NET Items + NET Delivery total_tax = delivery_tax #TAX items + TAX Delivery for i in range(3): qty = random.randint(1,10) p = Product.get_by_key_name(self.products[i]) print self.products[i],'price:', p.price, 'plus tax:', p.price + p.price * p.tax_percent / 100,'tax percent: ',p.tax_percent, 'price tax:',p.price * p.tax_percent / 100, 'qty:',qty sub_total_tax_items += (p.price * p.tax_percent / 100)*qty sub_total_price += (p.price + p.price * p.tax_percent / 100)*qty total += (p.price + p.price * p.tax_percent / 100)*qty total_without_tax += (p.price)*qty total_tax += (p.price * p.tax_percent / 100)*qty self.cart.add_item(CartItem(p),qty) print 'sub_total_tax_items: ', sub_total_tax_items, ' CART sub_total_tax_items: ',self.cart.sub_total_tax_items self.assertEqual(round(sub_total_tax_items,2), round(self.cart.sub_total_tax_items,2), 'Error calculating sub_total_tax_items') print 'sub_total_price: ', sub_total_price, ' CART sub_total_price: ',self.cart.sub_total_price self.assertEqual(round(sub_total_price,2), round(self.cart.sub_total_price,2), 'Error calculating sub_total_price') print 'total: ', total, ' CART total: ',self.cart.total self.assertEqual(round(total,2), round(self.cart.total,2), 'Error calculating total') print 'total_without_tax: ', total_without_tax, ' CART total_without_tax: ',self.cart.total_without_tax self.assertEqual(round(total_without_tax,2), round(self.cart.total_without_tax,2), 'Error calculating total_without_tax') print 'total_tax: ', total_tax, ' CART total_tax: ',self.cart.total_tax self.assertEqual(round(total_tax,2), round(self.cart.total_tax,2), 'Error calculating total_tax') print '----------------- END TEST -----------------------'
def get(self, subdomain=None): #args item = self.request.get('item','-') qty = int(self.request.get('qty',1)) '''NAMESPACE CHANGE (memcache and datastore)''' namespace = namespace_manager.get_namespace() namespace_manager.set_namespace(subdomain) '''Name of the memcache''' logging.info(str(self.session)) #cookie_value = self.request.cookies.get('shopapp-cart') #logging.info(str(cookie_value)) '''logging.info(cookie_value) if not cookie_value: #random number import random, string cookie_value = ''.join(random.choice(string.ascii_uppercase + '123456789') for i in xrange(10)) self.response.set_cookie(key='shopapp-cart', value=cookie_value,max_age=900) logging.info(str(self.request.cookies.get('shopapp-cart')))''' #self.response.set_cookie(key='guille', value='el mejor',max_age=900) self.session = self.session_store.get_session(name='shopapp',max_age=None,backend='memcache') logging.info(str(self.session)) #15 mins session (900 secs), if a previous session does not exist it creates a new one #self.session = self.session_store.get_session(name='mc_session',max_age=900,backend='memcache') #logging.info(cookie_value) try: cart = self.session['cart'] logging.info('cart found in memcache :)') except: cart = Cart() logging.info('cart not found in memcache :(') #return self.response.write(str(self.session['cart'].__dict__)) '''import random for i in range(1,10): product = Product(product_id=i, key_name="test_product_"+str(i),price=round(random.uniform(1,10),2),tax_percent=18.00) product.put() product.add_stock("test_product_"+str(i),10)''' product = Product.get_by_key_name(item) if not product: return self.response.write('The product does not exist') elif product.reserve(qty): '''Checked stock''' cart.add_item(CartItem(product),qty) else: self.response.write('Not enough products in the warehouse') self.session['cart'] = cart self.session_store.save_sessions(self.response) '''NAMESPACE CHANGE''' namespace_manager.set_namespace(namespace) return self.response.write(str(cart))
class CartTest(unittest.TestCase): def setUp(self): self.cart = Cart() self.inv = Inventory() def tearDown(self): self.inv.close() def test_add_items_to_basket(self): # Test adding item, make sure it's in cart and inventory updates to reflect that old_qty = self.inv.view_qty('item050') self.cart.add_item('item050', 10) self.assertEqual(self.cart.basket('item050'), 10) self.assertEqual(self.inv.view_qty('item050'), old_qty - 10) self.inv.update_qty('item050', 500) # Test adding more to cart than inventory contains self.cart._basket['item050'] = 0 # Temporary kluge self.cart.add_item('item050', 600) self.assertEqual(self.cart.basket('item050'), 500) self.assertEqual(self.inv.view_qty('item050'), 0) self.inv.update_qty('item050', 500) def test_remove_items_from_basket(self): self.cart.add_item('item050', 10) self.cart.remove_item('item050', 5) self.assertEqual(self.cart.basket('item050'), 5) self.assertEqual(self.inv.view_qty('item050'), 495) def test_edit_items(self): # Change to a quantity greater than you already have self.cart.edit_item('item050', 50) self.cart.edit_item('item050', 70) self.assertEqual(self.cart.basket('item050'), 70) self.assertEqual(self.inv.view_qty('item050'), 430) self.cart.remove_item('item050', 70) # Add more than are in inventory self.cart.edit_item('item050', 1000) self.assertEqual(self.inv.view_qty('item050'), 0) self.assertEqual(self.cart.basket('item050'), 500) self.cart.remove_item('item050', 500) # Change to a quantity less than what you already have self.cart.add_item('item050', 100) self.cart.edit_item('item050', 50) self.assertEqual(self.inv.view_qty('item050'), 450) self.assertEqual(self.cart.basket('item050'), 50) self.cart.remove_item('item050', 50) def test_items_total(self): self.cart.add_item('item100', 100) cost = 100 * self.inv.view_price('item100') self.assertEqual(self.cart.items_total(), cost) self.cart.remove_item('item100', 100) def test_list_items(self): self.cart.add_item('item100', 100) # Test listing sorted by item name for _tuple in self.cart.list_items('item'): self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100'))) # Test listing sorted by qty for _tuple in self.cart.list_items('price'): self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100'))) # Test listing sorted by price self.assertEqual(_tuple, ('item100', 100, self.inv.view_price('item100'))) self.cart.remove_item('item100', 100)