Skip to content

Commit

Permalink
[IMP-FIX] sale_loyalty_incompatibility: Adapt test to v16
Browse files Browse the repository at this point in the history
TT44346
  • Loading branch information
pilarvargas-tecnativa committed Nov 20, 2023
1 parent 7e3b2a6 commit b53753f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 78 deletions.
13 changes: 7 additions & 6 deletions sale_loyalty_incompatibility/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Loyalty incompatibilities
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:0a08b29a4c259fa45c1a162e2539bc400e9ecfa97b60f4fc878faca7dd3fb701
!! source digest: sha256:bd76477849b95a206f8dacf27b467418d49086fd2e8004e53a70031907ffb7e2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
Expand All @@ -17,13 +17,13 @@ Loyalty incompatibilities
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsale--promotion-lightgray.png?logo=github
:target: https://github.com/OCA/sale-promotion/tree/16.0-mig-sale_coupon_incompatibility/sale_loyalty_incompatibility
:target: https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_incompatibility
:alt: OCA/sale-promotion
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/sale-promotion-16-0-mig-sale_coupon_incompatibility/sale-promotion-16-0-mig-sale_coupon_incompatibility-sale_loyalty_incompatibility
:target: https://translation.odoo-community.org/projects/sale-promotion-16-0/sale-promotion-16-0-sale_loyalty_incompatibility
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&target_branch=16.0-mig-sale_coupon_incompatibility
:target: https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&target_branch=16.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand Down Expand Up @@ -51,7 +51,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/sale-promotion/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_loyalty_incompatibility%0Aversion:%2016.0-mig-sale_coupon_incompatibility%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_loyalty_incompatibility%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand All @@ -71,6 +71,7 @@ Contributors
* Pedro M. Baeza
* David Vidal
* Stefan Ungureanu
* Pilar Vargas

Maintainers
~~~~~~~~~~~
Expand All @@ -93,6 +94,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-chienandalu|

This module is part of the `OCA/sale-promotion <https://github.com/OCA/sale-promotion/tree/16.0-mig-sale_coupon_incompatibility/sale_loyalty_incompatibility>`_ project on GitHub.
This module is part of the `OCA/sale-promotion <https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_incompatibility>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions sale_loyalty_incompatibility/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
* Pedro M. Baeza
* David Vidal
* Stefan Ungureanu
* Pilar Vargas
9 changes: 5 additions & 4 deletions sale_loyalty_incompatibility/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ <h1 class="title">Loyalty incompatibilities</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:0a08b29a4c259fa45c1a162e2539bc400e9ecfa97b60f4fc878faca7dd3fb701
!! source digest: sha256:bd76477849b95a206f8dacf27b467418d49086fd2e8004e53a70031907ffb7e2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/sale-promotion/tree/16.0-mig-sale_coupon_incompatibility/sale_loyalty_incompatibility"><img alt="OCA/sale-promotion" src="https://img.shields.io/badge/github-OCA%2Fsale--promotion-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/sale-promotion-16-0-mig-sale_coupon_incompatibility/sale-promotion-16-0-mig-sale_coupon_incompatibility-sale_loyalty_incompatibility"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&amp;target_branch=16.0-mig-sale_coupon_incompatibility"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Production/Stable" src="https://img.shields.io/badge/maturity-Production%2FStable-green.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_incompatibility"><img alt="OCA/sale-promotion" src="https://img.shields.io/badge/github-OCA%2Fsale--promotion-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/sale-promotion-16-0/sale-promotion-16-0-sale_loyalty_incompatibility"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/sale-promotion&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module allows to set incompatibilities between promotions, so when a promotion A
is not compatible with promotion B, they can’t be applied together in a sales order.</p>
<p><strong>Table of contents</strong></p>
Expand Down Expand Up @@ -399,7 +399,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/sale-promotion/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<a class="reference external" href="https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_loyalty_incompatibility%0Aversion:%2016.0-mig-sale_coupon_incompatibility%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/sale-promotion/issues/new?body=module:%20sale_loyalty_incompatibility%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -417,6 +417,7 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<li>Pedro M. Baeza</li>
<li>David Vidal</li>
<li>Stefan Ungureanu</li>
<li>Pilar Vargas</li>
</ul>
</li>
</ul>
Expand All @@ -430,7 +431,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/chienandalu"><img alt="chienandalu" src="https://github.com/chienandalu.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/sale-promotion/tree/16.0-mig-sale_coupon_incompatibility/sale_loyalty_incompatibility">OCA/sale-promotion</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/sale-promotion/tree/16.0/sale_loyalty_incompatibility">OCA/sale-promotion</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
135 changes: 67 additions & 68 deletions sale_loyalty_incompatibility/tests/test_sale_coupon_incompatibility.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,76 @@
# 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)
.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.
self._action_apply_program(sale, self.promotion)

0 comments on commit b53753f

Please sign in to comment.