Gestão creates coupons in the Site tab: a code, a percentage or an amount per billed metre, validity, a minimum of metres, use limits in all and per customer, one product or all, and a note; they can be paused and deleted (kept for history). The customer types the code at checkout; the server checks it when the quote is made and again at approval, discounts only the eligible items after the bands (never freight or the treatment) and keeps the coupon on the order. Tiny gets the discount, the e-mail and receipt show it, the order panel carries a badge and Números counts orders, metres, discount and money per coupon. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
39 lines
1.6 KiB
Python
39 lines
1.6 KiB
Python
"""Run from repository root: python3 -m unittest tests.test_coupons -v."""
|
|
import unittest
|
|
|
|
from app.coupons import discount_cents, eligible_items
|
|
|
|
|
|
def item(mode, billed_metres, total_cents):
|
|
return {'mode': mode, 'billed_metres': billed_metres, 'total_cents': total_cents}
|
|
|
|
|
|
class CouponTests(unittest.TestCase):
|
|
def test_percent_is_floored(self):
|
|
self.assertEqual(discount_cents('percent', 10, [item('file', '1', 1990)]), 199)
|
|
self.assertEqual(discount_cents('percent', 33, [item('file', '1', 1990)]), 656)
|
|
self.assertEqual(discount_cents('percent', 10, []), 0)
|
|
|
|
def test_per_metre_is_floored_to_the_cent(self):
|
|
items = [item('file', '2.5', 5475), item('file', '1.5', 3285)]
|
|
self.assertEqual(discount_cents('per_metre', 200, items), 800)
|
|
# 333 cents x 1.1 m = 366.3 cents: the customer gets 366.
|
|
self.assertEqual(discount_cents('per_metre', 333, [item('file', '1.1', 2409)]), 366)
|
|
|
|
def test_product_filter(self):
|
|
items = [item('file', '1', 1990), item('uv', '1', 8990)]
|
|
self.assertEqual(eligible_items(items, 'uv'), [items[1]])
|
|
self.assertEqual(eligible_items(items, None), items)
|
|
|
|
def test_never_more_than_the_items_total(self):
|
|
self.assertEqual(discount_cents('per_metre', 1000, [item('file', '2', 1500)]), 1500)
|
|
self.assertEqual(discount_cents('percent', 90, [item('file', '1', 100)]), 90)
|
|
|
|
def test_invalid_kind(self):
|
|
with self.assertRaises(ValueError):
|
|
discount_cents('other', 10, [item('file', '1', 1990)])
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|