def test_buy_one_get_one_free_example(self): """Test as described in the docs /signals.html#an-example-of-pre-add-to-basket""" # make the product hammer = TestSimpleProduct( title="Hammer", price="4.99") hammer.save() # define the receiver def example_buy_one_get_one_free(sender, instance, item, quantity, **kwargs): # insert a hammer, with title 'buy one get one free' and a price # of 0.00 if item.name == "Hammer": instance.add(hammer, price="0.00", description="Free Hammer! Buy One Get One Free", locked=True, silent=True) # connect it pre_add_to_basket.connect(example_buy_one_get_one_free, dispatch_uid='bogof') # our basket has no items self.assertEqual(0, self.basket.total_items()) # add one hammer, quantity of one assumed by add method basketitem = self.basket.add(hammer) # now we have two basketitems self.assertEqual(2, self.basket.total_items()) # The first is the free hammer self.assertEqual( self.basket.basketitem_set.all()[0].description, "Free Hammer! Buy One Get One Free") self.assertEqual( self.basket.basketitem_set.all()[0].price, Decimal("0.00")) # it's locked so the user can't mess with it self.assertEqual( self.basket.basketitem_set.all()[0].locked, True) # second is the paid for hammer self.assertEqual( self.basket.basketitem_set.all()[1].description, "Hammer") self.assertEqual( self.basket.basketitem_set.all()[1].price, Decimal("4.99")) self.assertEqual( self.basket.basketitem_set.all()[1].locked, False) # disconnect it pre_add_to_basket.disconnect('bogof')
def test_do_not_add(self): # make the products hammer = TestSimpleProduct( title="Hammer", price="4.99") hammer.save() # crude receiver def do_not_add(sender,instance, **kwargs): return {'do_not_add' : True} # connect it pre_add_to_basket.connect(do_not_add, dispatch_uid='do_not_add') # add to basket - None basketitem = self.basket.add(hammer) self.assertEqual(0, self.basket.total_items()) # disconnect it pre_add_to_basket.disconnect(dispatch_uid='do_not_add') # add to basket now and it will go in basketitem = self.basket.add(hammer) self.assertEqual(1, self.basket.total_items())
def test_pre_add_to_basket_multiple_receivers(self): """Attach multiple receivers and make sure that there is no bork""" # define three listeners def modify_description(sender, instance, **kwargs): return { 'description' : 'Ho ho ho and a bottle of rum' } def modify_quantity(sender, instance, **kwargs): return { 'quantity' : 2 } def modify_price(sender, instance, **kwargs): return { 'price' : Decimal('0.01') } # connect them pre_add_to_basket.connect(modify_description, dispatch_uid='md') pre_add_to_basket.connect(modify_quantity, dispatch_uid='mq') pre_add_to_basket.connect(modify_price, dispatch_uid='mp') # no basketitems to start with self.assertEqual(0, BasketItem.objects.count()) # add to 1 to basket and we should end up with.. basketitem = self.basket.add(self.item, quantity=1) # 2 items self.assertEqual(2, BasketItem.objects.all()[0].quantity) # costing 0.02 self.assertEqual(self.basket.total_price(), Decimal('0.02')) # disconnect pre_add_to_basket.disconnect(dispatch_uid='md') pre_add_to_basket.disconnect(dispatch_uid='mq') pre_add_to_basket.disconnect(dispatch_uid='mp')
def tearDown(self): global test_dict test_dict = {} # disconnect signals pre_add_to_basket.disconnect(dispatch_uid='pre_test_rec') post_add_to_basket.disconnect(dispatch_uid='post_test_rec')