Skip to content

Commit

Permalink
Merge pull request #180 from Eficent/11.0-fix-product_multi_aen
Browse files Browse the repository at this point in the history
[11.0][FIX] product_multi_aen: error on search multiple product references
  • Loading branch information
pedrobaeza committed May 27, 2019
2 parents 10a92eb + 8b34557 commit b45987a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 35 deletions.
2 changes: 1 addition & 1 deletion product_multi_ean/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{
'name': 'Multiple EAN13 on products',
'version': '11.0.1.0.0',
'version': '11.0.1.0.1',
'license': 'AGPL-3',
'author': "Camptocamp, "
"Trey, "
Expand Down
24 changes: 12 additions & 12 deletions product_multi_ean/models/product_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,16 @@ def _prepare_ean13_vals(self):

@api.model
def search(self, domain, *args, **kwargs):
if list(filter(lambda x: x[0] == 'barcode', domain)):
ean_operator = list(
filter(lambda x: x[0] == 'barcode', domain)
)[0][1]
ean_value = list(
filter(lambda x: x[0] == 'barcode', domain)
)[0][2]

eans = self.env['product.ean13'].search(
[('name', ean_operator, ean_value)])
domain = list(filter(lambda x: x[0] != 'barcode', domain))
domain += [('ean13_ids', 'in', eans.ids)]
for sub_domain in list(filter(lambda x: x[0] == 'barcode', domain)):
domain = self._get_ean13_domain(sub_domain, domain)
return super(ProductProduct, self).search(domain, *args, **kwargs)

def _get_ean13_domain(self, sub_domain, domain):
ean_operator = sub_domain[1]
ean_value = sub_domain[2]
eans = self.env['product.ean13'].search(
[('name', ean_operator, ean_value)])
domain = [('ean13_ids', 'in', eans.ids)
if x[0] == 'barcode' and x[2] == ean_value
else x for x in domain]
return domain
59 changes: 37 additions & 22 deletions product_multi_ean/tests/test_product_multi_ean.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,44 +9,59 @@
class TestProductMultiEan(TransactionCase):
def setUp(self):
super(TestProductMultiEan, self).setUp()
self.product = self.env['product.product'].create({
'name': 'Test product',
# Product 1
self.product = self.env['product.product']
self.product_1 = self.product.create({
'name': 'Test product 1',
})
self.valid_ean = '1234567890128'
self.valid_ean2 = '0123456789012'
self.valid_ean_1 = '1234567890128'
self.valid_ean2_1 = '0123456789012'
# Product 2
self.product_2 = self.product.create({
'name': 'Test product 2',
})
self.valid_ean_2 = '9780471117094'
self.valid_ean2_2 = '4006381333931'

def test_set_main_ean(self):
self.product.barcode = self.valid_ean
self.assertEqual(len(self.product.ean13_ids), 1)
self.assertEqual(self.product.ean13_ids.name, self.product.barcode)
self.product_1.barcode = self.valid_ean_1
self.assertEqual(len(self.product_1.ean13_ids), 1)
self.assertEqual(self.product_1.ean13_ids.name, self.product_1.barcode)

def test_set_incorrect_ean(self):
with self.assertRaises(Exception):
self.product.barcode = '1234567890123'
self.product_1.barcode = '1234567890123'
with self.assertRaises(Exception):
self.product.ean13_ids = [(0, 0, {'name': '1234567890123'})]
self.product.barcode = self.valid_ean
self.product_1.ean13_ids = [(0, 0, {'name': '1234567890123'})]
self.product_1.barcode = self.valid_ean_1
# Insert duplicated EAN13
with self.assertRaises(Exception):
self.product.ean13_ids = [(0, 0, {'name': self.valid_ean})]
self.product_1.ean13_ids = [(0, 0, {'name': self.valid_ean_1})]

def test_post_init_hook(self):
self.env.cr.execute("""
UPDATE product_product
SET barcode = %s
WHERE id = %s""", (self.valid_ean, self.product.id))
WHERE id = %s""", (self.valid_ean_1, self.product_1.id))
post_init_hook(self.env.cr, self.registry)
self.product.refresh()
self.assertEqual(len(self.product.ean13_ids), 1)
self.assertEqual(self.product.ean13_ids.name, self.valid_ean)
self.product_1.refresh()
self.assertEqual(len(self.product_1.ean13_ids), 1)
self.assertEqual(self.product_1.ean13_ids.name, self.valid_ean_1)

def test_search(self):
self.product.ean13_ids = [
(0, 0, {'name': self.valid_ean}),
(0, 0, {'name': self.valid_ean2})]
products = self.product.search([('barcode', '=', self.valid_ean)])
self.product_1.ean13_ids = [
(0, 0, {'name': self.valid_ean_1}),
(0, 0, {'name': self.valid_ean2_1})]
self.product_2.ean13_ids = [
(0, 0, {'name': self.valid_ean_2}),
(0, 0, {'name': self.valid_ean2_2})]
products = self.product.search([('barcode', '=', self.valid_ean_1)])
self.assertEqual(len(products), 1)
self.assertEqual(products, self.product)
products = self.product.search([('barcode', '=', self.valid_ean2)])
self.assertEqual(products, self.product_1)
products = self.product.search([('barcode', '=', self.valid_ean2_1)])
self.assertEqual(len(products), 1)
self.assertEqual(products, self.product)
self.assertEqual(products, self.product_1)
products = self.product.search(
['|', ('barcode', '=', self.valid_ean2_1),
('barcode', '=', self.valid_ean2_2)])
self.assertEqual(len(products), 2)

0 comments on commit b45987a

Please sign in to comment.