|
| 1 | +from odoo.tests.common import TransactionCase, tagged |
| 2 | +from odoo.exceptions import ValidationError |
| 3 | + |
| 4 | + |
| 5 | +@tagged("post_install", "-at_install") |
| 6 | +class TestProductribbon(TransactionCase): |
| 7 | + |
| 8 | + def setUp(self): |
| 9 | + super().setUp() |
| 10 | + self.website = self.env["website"].search([], limit=1) |
| 11 | + self.product = self.env["product.template"].search([], limit=1) |
| 12 | + self.christmas_pricelist = self.env["product.pricelist"].search( |
| 13 | + [("name", "=", "Christmas")], limit=1 |
| 14 | + ) |
| 15 | + self.christmas_pricelist.write({"selectable": True}) |
| 16 | + self.prices = self.product._get_sales_prices(self.website) |
| 17 | + |
| 18 | + def _create_ribbon(self, name, assign): |
| 19 | + return self.env["product.ribbon"].create({ |
| 20 | + "name": name, |
| 21 | + "assign": assign, |
| 22 | + }) |
| 23 | + |
| 24 | + def test_sale_ribbon_is_assigned_when_discount_applies(self): |
| 25 | + ribbon = self._create_ribbon("Sale", "sale") |
| 26 | + self.product._get_ribbon(self.prices) |
| 27 | + |
| 28 | + self.assertEqual( |
| 29 | + self.product.website_ribbon_id.id, |
| 30 | + ribbon.id, |
| 31 | + "Expected Sale ribbon to be assigned when pricelist discount is active." |
| 32 | + ) |
| 33 | + |
| 34 | + def test_out_of_stock_ribbon_priority_over_sale(self): |
| 35 | + self.product.write({ |
| 36 | + "qty_available": 0, |
| 37 | + "allow_out_of_stock_order": False |
| 38 | + }) |
| 39 | + |
| 40 | + out_ribbon = self._create_ribbon("Out of Stock", "out_of_stock") |
| 41 | + self._create_ribbon("Sale", "sale") |
| 42 | + self.product._get_ribbon(self.prices) |
| 43 | + |
| 44 | + self.assertEqual( |
| 45 | + self.product.website_ribbon_id.id, |
| 46 | + out_ribbon.id, |
| 47 | + "Out of Stock ribbon should take priority over Sale ribbon." |
| 48 | + ) |
| 49 | + |
| 50 | + def test_validation_error_on_duplicate_ribbon_assign(self): |
| 51 | + self._create_ribbon("Sale Discount", "sale") |
| 52 | + with self.assertRaises(ValidationError, msg="Duplicate 'sale' ribbon should raise ValidationError"): |
| 53 | + self._create_ribbon("Sale Offer", "sale") |
| 54 | + |
| 55 | + def test_manual_ribbon_should_override_automatic(self): |
| 56 | + manual = self._create_ribbon("Sold Out", "manual") |
| 57 | + self.product.write({"website_ribbon_id": manual.id}) |
| 58 | + self._create_ribbon("New", "new") |
| 59 | + |
| 60 | + self.product._get_ribbon(self.prices) |
| 61 | + |
| 62 | + self.assertEqual( |
| 63 | + self.product.website_ribbon_id.id, |
| 64 | + manual.id, |
| 65 | + "Manual ribbon must not be replaced by any automatic ribbon." |
| 66 | + ) |
| 67 | + |
| 68 | + def test_out_of_stock_respects_allow_out_of_stock_flag(self): |
| 69 | + ribbon = self._create_ribbon("Out of Stock", "out_of_stock") |
| 70 | + |
| 71 | + # Case 1: Should apply |
| 72 | + self.product.write({"qty_available": 0, "allow_out_of_stock_order": False}) |
| 73 | + self.product._get_ribbon(self.prices) |
| 74 | + self.assertEqual( |
| 75 | + self.product.website_ribbon_id.id, |
| 76 | + ribbon.id, |
| 77 | + "Expected Out of Stock ribbon when allow_out_of_stock_order is False." |
| 78 | + ) |
| 79 | + |
| 80 | + # Case 2: Should NOT apply |
| 81 | + self.product.write({"allow_out_of_stock_order": True}) |
| 82 | + self.product._get_ribbon(self.prices) |
| 83 | + self.assertNotEqual( |
| 84 | + self.product.website_ribbon_id.id, |
| 85 | + ribbon.id, |
| 86 | + "Out of Stock ribbon should not be shown when allow_out_of_stock_order is True." |
| 87 | + ) |
0 commit comments