Exemplo n.º 1
0
 def validate(self, data):
     data = super(StoreTransactionItemSerializer, self).validate(data)
     try:
         validate_item(data)
     except TransactionException as e:
         raise ValidationError(str(e))
     return data
Exemplo n.º 2
0
 def validate(self, data):
     data = super(StoreTransactionItemSerializer, self).validate(data)
     try:
         validate_item(data)
     except TransactionException as e:
         raise ValidationError(str(e))
     return data
Exemplo n.º 3
0
    def test_validate_item(self):
        """ Make sure that item validation function works """
        item_tpl = {
            "item_id": self.items[0].id,
            "variant_id": None,
            "amount": 1
        }

        # Make sure too large or zero amount fails, but small enough does not
        validate_item({**item_tpl, **{"amount": 5}})
        with self.assertRaises(TransactionException):
            self.assertRaises(validate_item({**item_tpl, **{"amount": 6}}), TransactionException)
            self.assertRaises(validate_item({**item_tpl, **{"amount": 0}}), TransactionException)

        # Make sure item id checking works
        validate_item({**item_tpl, **{"item_id": self.items[0].id}})
        with self.assertRaises(TransactionException):
            validate_item({**item_tpl, **{"item_id": self.items[4].id}})  # available is False
            validate_item({**item_tpl, **{"item_id": self.items[3].id}})  # Max is 0

        # Make sure variant id checking works
        validate_item({**item_tpl, **{"item_id": self.items[1].id, "variant_id": self.variants[1][0].id}})
        validate_item({**item_tpl, **{"item_id": self.items[1].id, "variant_id": self.variants[1][4].id}})
        with self.assertRaises(TransactionException):
            # Fail if variant does not belong to the product
            validate_item({**item_tpl, **{"item_id": self.items[1].id, "variant_id": self.variants[2][0].id}})
Exemplo n.º 4
0
    def test_validate_item(self):
        """ Make sure that item validation function works """
        item_tpl = {
            "item_id": self.items[0].id,
            "variant_id": None,
            "amount": 1
        }

        # Make sure too large or zero amount fails, but small enough does not
        validate_item({**item_tpl, **{"amount": 5}})
        with self.assertRaises(TransactionException):
            self.assertRaises(validate_item({
                **item_tpl,
                **{
                    "amount": 6
                }
            }), TransactionException)
            self.assertRaises(validate_item({
                **item_tpl,
                **{
                    "amount": 0
                }
            }), TransactionException)

        # Make sure item id checking works
        validate_item({**item_tpl, **{"item_id": self.items[0].id}})
        with self.assertRaises(TransactionException):
            validate_item({
                **item_tpl,
                **{
                    "item_id": self.items[4].id
                }
            })  # available is False
            validate_item({
                **item_tpl,
                **{
                    "item_id": self.items[3].id
                }
            })  # Max is 0

        # Make sure variant id checking works
        validate_item({
            **item_tpl,
            **{
                "item_id": self.items[1].id,
                "variant_id": self.variants[1][0].id
            }
        })
        validate_item({
            **item_tpl,
            **{
                "item_id": self.items[1].id,
                "variant_id": self.variants[1][4].id
            }
        })
        with self.assertRaises(TransactionException):
            # Fail if variant does not belong to the product
            validate_item({
                **item_tpl,
                **{
                    "item_id": self.items[1].id,
                    "variant_id": self.variants[2][0].id
                }
            })