0

I have this function iam trying to test whether if discount amount is greater than the invoice amount then a validation error should be raised. Iam stuck in actually triggering the exception for it to be raised in the test. See below

class Invoice(models.Model):
    @property   
    def inv_amount(self):
        amount = Sum(quantity * price)
        return amount

    @property
    def discount_amount(self):
        amount = Sum(discount)
        return amount  

    def validate_amount(self):
        if self.discount_amount > self.inv_amount:
            raise ValidationError("discount amount cannot be greater")

def_test_validate_amount_greater_than_discount():
    with pytest.raises(ValidationError) as e:
       #Trigger the exception

1 Answers1

0

To trigger the exception you simply should call i.validate_amount() (being i an Invoice object with wrong values) inside your with.

Also, you can use e to check if that one is your expected exception.

Edit (based on the comments of your question):

You can't set a property, but you can set the values quantity, price and discount to make it fail, assuming that those are part of the class Invoice