Skip to content

Instantly share code, notes, and snippets.

@almazkun
Last active July 1, 2021 09:10
Show Gist options
  • Save almazkun/3ee0af54acf76e668bc62d3d047ccb50 to your computer and use it in GitHub Desktop.
Save almazkun/3ee0af54acf76e668bc62d3d047ccb50 to your computer and use it in GitHub Desktop.
# main/models.py
from django.core.validators import MaxValueValidator
class Product(WithSlug):
...
# all the other fields
tax_rate = models.PositiveIntegerField(
default=25,
help_text=_("Tax rate"),
validators=[MaxValueValidator(100),],
)
# all other methods
@property
def tax_amount(self):
return round(self.price * (self.tax_rate / 100))
#tests.py
class ModelsProductTest(TestCase):
def setUp(self):
self.t_objs = [
(100, 25, 25),
(50, 10, 5),
(2, 50, 1),
]
def test_tax_rate(self):
for price, tax_rate, expected in self.t_objs:
p = ProductFacroty(price=price, tax_rate=tax_rate)
self.assertEqual(expected, p.tax_amount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment