-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMP-FIX] sale_loyalty_incompatibility: Adapt test to v16
TT44346
- Loading branch information
1 parent
d4f063e
commit 0e68f48
Showing
6 changed files
with
89 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ | |
* Pedro M. Baeza | ||
* David Vidal | ||
* Stefan Ungureanu | ||
* Pilar Vargas |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 68 additions & 68 deletions
136
sale_loyalty_incompatibility/tests/test_sale_coupon_incompatibility.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,77 @@ | ||
# Copyright 2023 Tecnativa - Stefan Ungureanu | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
from odoo.exceptions import UserError | ||
from odoo.exceptions import ValidationError | ||
from odoo.tests import Form | ||
|
||
from odoo.addons.sale_loyalty.tests.common import TestSaleCouponCommon | ||
from odoo.addons.loyalty_incompatibility.tests.test_loyalty_incompatibility import ( | ||
LoyaltyIncompatibilityCase, | ||
) | ||
|
||
|
||
class TestLoyaltyIncompatibility(TestSaleCouponCommon): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls.promotion_1 = cls.code_promotion_program | ||
cls.promotion_2 = cls.code_promotion_program.copy() | ||
cls.promotion_2.rule_ids.write({"product_ids": cls.product_C}) | ||
cls.env["loyalty.generate.wizard"].with_context( | ||
active_id=cls.promotion_1 | ||
).create( | ||
{ | ||
"mode": "selected", | ||
"customer_ids": cls.steve, | ||
"points_granted": 1, | ||
} | ||
).generate_coupons() | ||
cls.coupon_promotion_1 = cls.promotion_1.coupon_ids | ||
cls.env["loyalty.generate.wizard"].with_context( | ||
active_id=cls.promotion_2 | ||
).create( | ||
{ | ||
"mode": "selected", | ||
"customer_ids": cls.steve, | ||
"points_granted": 1, | ||
} | ||
).generate_coupons() | ||
cls.coupon_promotion_2 = cls.promotion_2.coupon_ids | ||
cls.order = cls.empty_order | ||
sale_form = Form(cls.order) | ||
class TestSaleLoyaltyIncompatibility(LoyaltyIncompatibilityCase): | ||
def _action_apply_program(self, sale, program): | ||
sale._update_programs_and_rewards() | ||
wizard = ( | ||
self.env["sale.loyalty.reward.wizard"] | ||
.with_context(active_id=sale.id) | ||
.create({"selected_reward_id": program.reward_ids.id}) | ||
) | ||
wizard.action_apply() | ||
|
||
def _create_sale(self, partner, salesman=False): | ||
"""Helper method to create sales in the test cases""" | ||
sale_form = Form(self.env["sale.order"]) | ||
sale_form.partner_id = partner | ||
if salesman: | ||
sale_form.user_id = salesman | ||
with sale_form.order_line.new() as line_form: | ||
line_form.product_id = cls.product_A | ||
line_form.name = "1 Product A" | ||
line_form.product_uom = cls.uom_unit | ||
line_form.product_uom_qty = 1.0 | ||
sale_form.save() | ||
line_form.product_id = self.product_a | ||
line_form.product_uom_qty = 1 | ||
return sale_form.save() | ||
|
||
def test_01_program_incompatibilities(self): | ||
"""A program with no code and customer application limit won't be applied | ||
once the limit is reached""" | ||
# First we test the regular behavior | ||
# Test basic case for coupon program with code | ||
self.code_promotion_program.reward_ids.reward_type = "discount" | ||
self.code_promotion_program.reward_ids.discount = 10 | ||
def _apply_promo_code(self, order, code, no_reward_fail=True): | ||
status = order._try_apply_code(code) | ||
if "error" in status: | ||
raise ValidationError(status["error"]) | ||
coupons = self.env["loyalty.card"] | ||
rewards = self.env["loyalty.reward"] | ||
for coupon, coupon_rewards in status.items(): | ||
coupons |= coupon | ||
rewards |= coupon_rewards | ||
if len(coupons) == 1 and len(rewards) == 1: | ||
status = order._apply_program_reward(rewards, coupons) | ||
|
||
self._apply_promo_code(self.order, self.coupon_promotion_1.code) | ||
self.assertEqual(len(self.order.order_line.ids), 2) | ||
# Set some incompatibilities between promotion_1 and promotion_2 | ||
# First validate that's inverted process works fine | ||
self.promotion_1.incompatible_promotion_ids = self.promotion_2 | ||
self.assertEqual(self.promotion_2.incompatible_promotion_ids, self.promotion_1) | ||
# Add product C for try apply booth coupon programs same time | ||
sale_form = Form(self.order) | ||
with sale_form.order_line.new() as line_form: | ||
line_form.product_id = self.product_C | ||
line_form.name = "1 Product C" | ||
line_form.product_uom = self.uom_unit | ||
line_form.product_uom_qty = 1.0 | ||
sale_form.save() | ||
# Second we will try to apply two programs with incompatibility between themselves | ||
# The coupon program isn't compatible anymore | ||
with self.assertRaises(UserError): | ||
self._apply_promo_code(self.order, self.coupon_promotion_2.code) | ||
# Remove last coupon from promotion_1 applied | ||
self.order.order_line.filtered("is_reward_line").unlink() | ||
# Remove the incompatibility from either promotion | ||
self.promotion_2.incompatible_promotion_ids = False | ||
self.assertFalse(self.promotion_1.incompatible_promotion_ids) | ||
# We can now apply it without restrictions | ||
self._apply_promo_code(self.order, self.coupon_promotion_2.code) | ||
def test_01_program_without_incompatibilities(self): | ||
"""When there are no incompatibilities, both coupons and promotions can be applied | ||
to the same sales order.""" | ||
# Create coupons for the coupon program | ||
self.env["loyalty.generate.wizard"].with_context( | ||
active_id=self.coupon_program_without_incompatibility.id | ||
).create({"coupon_qty": 1}).generate_coupons() | ||
coupon = self.coupon_program_without_incompatibility.coupon_ids | ||
sale = self._create_sale(self.partner) | ||
# Apply promotion and coupon | ||
self._action_apply_program(sale, self.promotion) | ||
self._apply_promo_code(sale, coupon.code) | ||
self.assertTrue(len(sale.order_line.filtered("is_reward_line")) == 2) | ||
|
||
def test_02_program_with_incompatibilities(self): | ||
"""When an incompatible promotion is set up, the incompatible promotion cannot be | ||
applied with the coupons.""" | ||
# Create coupons for the coupon program | ||
self.env["loyalty.generate.wizard"].with_context( | ||
active_id=self.coupon_program_with_incompatibility.id | ||
).create({"coupon_qty": 1}).generate_coupons() | ||
coupon = self.coupon_program_with_incompatibility.coupon_ids | ||
sale = self._create_sale(self.partner) | ||
# Apply promotion | ||
self._action_apply_program(sale, self.promotion) | ||
# The coupon cannot be applied. | ||
with self.assertRaises(ValidationError): | ||
self._apply_promo_code(sale, coupon.code) | ||
# Remove reward from incompatible promotion to be able to apply the coupon | ||
sale.order_line.filtered("is_reward_line").unlink() | ||
self._apply_promo_code(sale, coupon.code) | ||
# Once the coupon has been applied the promotion cannot be applied. | ||
with self.assertRaises(ValidationError): | ||
self._action_apply_program(sale, self.promotion) |