# Copyright 2017 Tecnativa - Vicent Cubells
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo.tests import common
from odoo.exceptions import ValidationError


class TestResPartnerRefUnique(common.SavepointCase):
    @classmethod
    def setUpClass(cls):
        super(TestResPartnerRefUnique, cls).setUpClass()
        cls.company = cls.env.ref('base.main_company')
        partner_obj = cls.env['res.partner']
        cls.partner1 = partner_obj.create({
            'name': 'Partner1',
        })
        cls.partner2 = partner_obj.create({
            'name': 'Partner2',
        })

    def test_check_ref(self):
        # Test can create/modify partners with same ref
        self.company.partner_ref_unique = 'none'
        self.partner1.ref = 'same_ref'
        self.partner2.ref = 'same_ref'
        self.assertEqual(self.partner1.ref, self.partner2.ref)
        self.partner2.ref = False

        # Test can't create/modify partner with same ref
        self.company.partner_ref_unique = 'all'
        with self.assertRaises(ValidationError):
            self.partner2.ref = 'same_ref'

        # Test can't create/modify companies with same ref
        self.company.partner_ref_unique = 'companies'
        self.partner2.ref = 'same_ref'
        self.assertEqual(self.partner1.ref, self.partner2.ref)
        self.partner2.ref = False
        self.partner1.is_company = True
        self.partner2.is_company = True
        with self.assertRaises(ValidationError):
            self.partner2.ref = 'same_ref'

@@ -398,16 +398,20 @@
         
+cls.
 partner_
@@ -462,16 +462,20 @@
 tner1 = 
+cls.
 partner_
@@ -553,16 +553,20 @@
 tner2 = 
+cls.
 partner_
@@ -890,32 +890,183 @@
 f.partner2.ref)%0A
+        # Here there shouldn't be any problem%0A        self.partner_obj.create(%7B%0A            'name': 'other',%0A            'ref': 'same_ref',%0A        %7D)%0A
         self.par
@@ -1083,17 +1083,16 @@
 = False%0A
-%0A
         
@@ -1280,16 +1280,185 @@
 me_ref'%0A
+        with self.assertRaises(ValidationError):%0A            self.partner_obj.create(%7B%0A                'name': 'other',%0A                'ref': 'same_ref',%0A            %7D)
 %0A       
@@ -1851,28 +1851,418 @@
 f.partner2.ref = 'same_ref'%0A
+        with self.assertRaises(ValidationError):%0A            self.partner_obj.create(%7B%0A                'is_company': True,%0A                'name': 'other',%0A                'ref': 'same_ref',%0A            %7D)%0A        # Here there shouldn't be any problem%0A        self.partner_obj.create(%7B%0A            'is_company': False,%0A            'name': 'other',%0A            'ref': 'same_ref',%0A        %7D)%0A

Extend tests use cases