Exemplo n.º 1
0
 def calculate(self, basket):
     myround = lambda x: float(int(x)) if int(x) == x else float(int(x) + 1)
     s = Scale()
     weight = s.weigh_basket(basket) / 1000
     total = D(myround(weight)) * self.charge_per_item
     return prices.Price(
         currency=basket.currency,
         excl_tax=total,
         incl_tax=total)
Exemplo n.º 2
0
    def test_returns_correct_weight_for_nonempty_basket_with_line_quantities(self):
        basket = factories.create_basket(empty=True)
        products = [
            (factories.create_product(attributes={"weight": "1"}, price=D("5.00")), 3),
            (factories.create_product(attributes={"weight": "2"}, price=D("5.00")), 4),
        ]
        for product, quantity in products:
            basket.add(product, quantity=quantity)

        scale = Scale(attribute_code="weight")
        self.assertEqual(1 * 3 + 2 * 4, scale.weigh_basket(basket))
Exemplo n.º 3
0
    def test_returns_correct_weight_for_nonempty_basket(self):
        basket = factories.create_basket(empty=True)
        products = [
            factories.create_product(attributes={"weight": "1"}, price=D("5.00")),
            factories.create_product(attributes={"weight": "2"}, price=D("5.00")),
        ]
        for product in products:
            basket.add(product)

        scale = Scale(attribute_code="weight")
        self.assertEqual(1 + 2, scale.weigh_basket(basket))
Exemplo n.º 4
0
    def test_returns_correct_weight_for_nonempty_basket(self):
        basket = factories.create_basket(empty=True)
        products = [
            factories.create_product(attributes={'weight': '1'},
                                     price=D('5.00')),
            factories.create_product(attributes={'weight': '2'},
                                     price=D('5.00'))]
        for product in products:
            basket.add(product)

        scale = Scale(attribute_code='weight')
        self.assertEqual(1 + 2, scale.weigh_basket(basket))
Exemplo n.º 5
0
    def test_returns_correct_weight_for_nonempty_basket_with_line_quantities(
            self):
        basket = factories.create_basket(empty=True)
        products = [(factories.create_product(attributes={'weight': '1'},
                                              price=D('5.00')), 3),
                    (factories.create_product(attributes={'weight': '2'},
                                              price=D('5.00')), 4)]
        for product, quantity in products:
            basket.add(product, quantity=quantity)

        scale = Scale(attribute_code='weight')
        self.assertEqual(1 * 3 + 2 * 4, scale.weigh_basket(basket))
Exemplo n.º 6
0
    def test_returns_correct_weight_for_nonempty_basket(self):
        basket = factories.create_basket(empty=True)
        products = [
            factories.create_product(attributes={'weight': '1'},
                                     price=D('5.00')),
            factories.create_product(attributes={'weight': '2'},
                                     price=D('5.00'))]
        for product in products:
            basket.add(product)

        scale = Scale(attribute_code='weight')
        self.assertEqual(1 + 2, scale.weigh_basket(basket))
Exemplo n.º 7
0
    def test_decimals(self):
        basket = factories.create_basket(empty=True)
        product = factories.create_product(attributes={'weight': '0.3'},
                                           price=D('5.00'))
        basket.add(product)

        scale = Scale(attribute_code='weight')
        self.assertEqual(D('0.3'), scale.weigh_basket(basket))

        basket.add(product)
        self.assertEqual(D('0.6'), scale.weigh_basket(basket))

        basket.add(product)
        self.assertEqual(D('0.9'), scale.weigh_basket(basket))
Exemplo n.º 8
0
    def test_decimals(self):
        basket = factories.create_basket(empty=True)
        product = factories.create_product(attributes={'weight': '0.3'},
                                           price=D('5.00'))
        basket.add(product)

        scale = Scale(attribute_code='weight')
        self.assertEqual(D('0.3'), scale.weigh_basket(basket))

        basket.add(product)
        self.assertEqual(D('0.6'), scale.weigh_basket(basket))

        basket.add(product)
        self.assertEqual(D('0.9'), scale.weigh_basket(basket))
Exemplo n.º 9
0
    def test_returns_zero_for_empty_basket(self):
        basket = Basket()

        scale = Scale(attribute_code='weight')
        self.assertEqual(0, scale.weigh_basket(basket))
Exemplo n.º 10
0
 def test_raises_exception_when_attribute_is_missing(self):
     scale = Scale(attribute_code='weight')
     p = factories.create_product()
     with self.assertRaises(ValueError):
         scale.weigh_product(p)
Exemplo n.º 11
0
 def test_uses_default_weight_when_attribute_is_missing(self):
     scale = Scale(attribute_code='weight', default_weight=0.5)
     p = factories.create_product()
     self.assertEqual(0.5, scale.weigh_product(p))
Exemplo n.º 12
0
 def test_weighs_uses_specified_attribute(self):
     scale = Scale(attribute_code='weight')
     p = factories.create_product(attributes={'weight': '1'})
     self.assertEqual(1, scale.weigh_product(p))
Exemplo n.º 13
0
    def test_returns_zero_for_empty_basket(self):
        basket = Basket()

        scale = Scale(attribute_code='weight')
        self.assertEqual(0, scale.weigh_basket(basket))
Exemplo n.º 14
0
 def test_raises_exception_when_attribute_is_missing(self):
     scale = Scale(attribute_code='weight')
     p = factories.create_product()
     with self.assertRaises(ValueError):
         scale.weigh_product(p)
Exemplo n.º 15
0
 def test_uses_default_weight_when_attribute_is_missing(self):
     scale = Scale(attribute_code='weight', default_weight=0.5)
     p = factories.create_product()
     self.assertEqual(0.5, scale.weigh_product(p))
Exemplo n.º 16
0
 def test_weighs_uses_specified_attribute(self):
     scale = Scale(attribute_code='weight')
     p = factories.create_product(attributes={'weight': '1'})
     self.assertEqual(1, scale.weigh_product(p))