Skip to content

Commit

Permalink
[IMP] product_barcode_constraint_per_company: change old method
Browse files Browse the repository at this point in the history
Update oca_dependencies file
Fix tests
  • Loading branch information
dessanhemrayev committed Jan 16, 2024
1 parent ea2cf9d commit 21f10bb
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 45 deletions.
2 changes: 1 addition & 1 deletion product_barcode_constraint_per_company/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ promote its widespread use.

This module is part of the `OCA/stock-logistics-barcode <https://github.com/OCA/stock-logistics-barcode/tree/14.0/product_barcode_constraint_per_company>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion product_barcode_constraint_per_company/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"website": "https://github.com/OCA/stock-logistics-barcode",
"license": "AGPL-3",
"depends": [
"product",
"product_variant_company",
],
"installable": True,
}
25 changes: 9 additions & 16 deletions product_barcode_constraint_per_company/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import _, fields, models
from odoo import _, models


class ProductProduct(models.Model):
_inherit = "product.product"

tmpl_company_id = fields.Many2one(
related="product_tmpl_id.company_id",
string="Company of the related Template",
store=True,
)

def _auto_init(self):
for i, constraint in enumerate(self._sql_constraints):
if constraint[0] == "barcode_uniq":
self._sql_constraints[i] = (
"barcode_uniq",
"unique(barcode, tmpl_company_id)",
_("A barcode can only be assigned to one product per company !"),
)
return super(ProductProduct, self)._auto_init()
_sql_constraints = [
# Replace constraint with same name
(
"barcode_uniq",
"unique(barcode, company_id)",
_("A barcode can only be assigned to one product per company !"),
),
]
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
* Sylvain LE GAL <https://twitter.com/legalsylvain>
* Sulivan Lominchar <slominchar@archeti.com>
* Ooops <https://ooops404.com>
* Francesco Foresti <francesco.foresti@ooops404.com>
* Cetmix <https://cetmix.com>
* Dessan Hemrayev <dessanhemrayev@gmail.com>
3 changes: 0 additions & 3 deletions product_barcode_constraint_per_company/readme/ROADMAP.rst

This file was deleted.

Empty file.
25 changes: 25 additions & 0 deletions product_barcode_constraint_per_company/tests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2023 Cetmix OÜ
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from odoo.tests import SavepointCase, tagged


@tagged("post_install", "-at_install")
class CommonProductBarcodeConstraintPerCompany(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.env = cls.env(context=dict(cls.env.context, tracking_disable=True))
cls.product_product_obj = cls.env["product.product"]
cls.res_company_obj = cls.env["res.company"]
cls.company_1 = cls.res_company_obj.create({"name": "Company 1"})
cls.company_2 = cls.res_company_obj.create({"name": "Company 2"})

@classmethod
def _create_product(cls, name, company_id):
return cls.product_product_obj.create(
{
"name": name,
"company_id": company_id,
"barcode": "978020137963",
}
)
75 changes: 51 additions & 24 deletions product_barcode_constraint_per_company/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,62 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from psycopg2 import IntegrityError

from odoo.tests.common import TransactionCase
from odoo.tests import tagged
from odoo.tools.misc import mute_logger

from .common import CommonProductBarcodeConstraintPerCompany

class TestModule(TransactionCase):
def setUp(self):
super().setUp()

self.ResCompany = self.env["res.company"]
self.ProductProduct = self.env["product.product"]
self.company_1 = self.ResCompany.create({"name": "Company 1"})
self.company_2 = self.ResCompany.create({"name": "Company 2"})

# Test Section
@tagged("post_install", "-at_install")
class TestModule(CommonProductBarcodeConstraintPerCompany):
def test_create_same_company(self):
self._create_product("Product 1", self.company_1)
"""Verifying the Existence of a Product with the Same Barcode within a Single Company"""
# Create the first product in company_1
product_1 = self._create_product("Product 1", self.company_1.id)
# Check if the company_id of the product matches self.company_1
self.assertEqual(
product_1.company_id,
self.company_1,
msg="Product company ID must be equal to ID #{company}".format(
company=self.company_1.id
),
)

# Check if the company_id of the product_tmpl_id matches self.company_1
self.assertEqual(
product_1.product_tmpl_id.company_id,
self.company_1,
msg="Product company ID must be equal to ID # {company}".format(
company=self.company_1.id
),
)

with self.assertRaises(IntegrityError), mute_logger("odoo.sql_db"):
product2 = self._create_product("Product 2", self.company_1)
product2.flush()
# Try to create a second product with the same barcode in company_1
# and expect an IntegrityError
with mute_logger("odoo.sql_db"), self.assertRaises(
IntegrityError,
msg="A barcode can only be assigned to one product per company !",
):
self._create_product("Product 2", self.company_1.id)

def test_create_different_company(self):
self._create_product("Product 1", self.company_1)
self._create_product("Product 2", self.company_2)

def _create_product(self, name, company):
vals = {
"name": name,
"company_id": company.id,
"barcode": "978020137962",
}
return self.ProductProduct.create(vals)
"""Verify creating a product with the same barcode for different companies"""
# Create the first product in company_1
product_1 = self._create_product("Product 1", self.company_1.id)

# Create the second product in company_2
product_2 = self._create_product("Product 2", self.company_2.id)

# Check that the products belong to different companies
self.assertNotEqual(
product_1.company_id,
product_2.company_id,
msg="Products should belong to different companies",
)

# Check that the products have the same barcode
self.assertEqual(
product_1.barcode,
product_2.barcode,
msg="Products should have the same barcode",
)

0 comments on commit 21f10bb

Please sign in to comment.