Пример #1
0
 def test_buy_hammer_get_nails_example(self):
     """Test the example as described in the docs
     /signals.html#an-example-of-post-add-to-basket"""
     # make the products
     hammer = SimpleProduct(
         title="Hammer",
         price="4.99")
     hammer.save()
     nails = SimpleProduct(
         title="Bag of 500 Nails",
         price="1.99")
     nails.save()
     #
     def free_nails_with_every_hammer(sender,
                     instance,
                     basketitem,
                     **kwargs):
         if basketitem.content_object.title == 'Hammer':
             nails = SimpleProduct.objects.get(title="Bag of 500 Nails")
             instance.add(nails,
                         price=Decimal("0.00"),
                         description="Free nails with every hammer",
                         silent=True,
                         locked=True)
     # connect signal
     post_add_to_basket.connect(free_nails_with_every_hammer,
         dispatch_uid='free_nails')
     # basket has zero
     self.assertEqual(0, self.basket.total_items())
     # add hammer
     basketitem = self.basket.add(hammer)
     # now have two items
     self.assertEqual(2, self.basket.total_items())
     # 1st: hammer, 1 , 4.99, locked=False
     self.assertEqual(
         self.basket.basketitem_set.all()[0].description,
         "Hammer")
     self.assertEqual(1, self.basket.basketitem_set.filter(
                 description="Hammer").count())
     self.assertEqual(
         self.basket.basketitem_set.all()[0].price,
         Decimal("4.99"))
     self.assertEqual(
         self.basket.basketitem_set.all()[0].locked,
         False)
     # 2nd: free nails, 1 , 0.00, locked=True
     self.assertEqual(
         self.basket.basketitem_set.all()[1].description,
         "Free nails with every hammer")
     self.assertEqual(1, self.basket.basketitem_set.filter(
                 description="Free nails with every hammer").count())
     self.assertEqual(
         self.basket.basketitem_set.all()[1].price,
         Decimal("0.00"))
     self.assertEqual(
         self.basket.basketitem_set.all()[1].locked,
         True)
     # disconnect
     post_add_to_basket.disconnect(dispatch_uid='free_nails')
Пример #2
0
 def test_pre_add_to_basket_kwargs(self):
     """Test pre_add_to_basket is sent"""
     # disconnect the post add to baskets 
     post_add_to_basket.disconnect(dispatch_uid='post_test_rec')
     # were using a global
     global test_dict
     # connect the signal
     pre_add_to_basket.connect(test_receiver, dispatch_uid='pre_test_rec')
     # test_dict is empty
     self.assertEqual(test_dict.keys(), [])
     # add a product to the basket
     basketitem = self.basket.add(self.item, quantity=2)
     # test_dict now has all the right keys
     self.assertEqual(test_dict['signal'], pre_add_to_basket)
     self.assertEqual(test_dict['sender'], Basket)
     self.assertEqual(test_dict['instance'], self.basket)
     self.assertEqual(test_dict['item'].title, self.item.title)
     self.assertEqual(test_dict['quantity'], 2)
Пример #3
0
 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')