From f93395abd422205de77ddba243c17a15dcb71f9d Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Sat, 25 Jun 2016 12:02:51 +0200 Subject: [PATCH] Convert YAML tests to unit-tests, with several improvements Allow simultaneous use of country_field and partner_field in field definition --- base_phone/__openerp__.py | 1 - base_phone/fields.py | 3 +- base_phone/models/res_partner.py | 7 ++-- base_phone/test/phonenum.yml | 50 ---------------------------- base_phone/tests/__init__.py | 3 ++ base_phone/tests/test_phone.py | 55 +++++++++++++++++++++++++++++++ crm_phone/__init__.py | 19 ----------- crm_phone/__openerp__.py | 1 - crm_phone/crm_phone.py | 6 ++-- crm_phone/test/phonenum.yml | 41 ----------------------- crm_phone/tests/__init__.py | 3 ++ crm_phone/tests/test_crm_phone.py | 46 ++++++++++++++++++++++++++ 12 files changed, 115 insertions(+), 120 deletions(-) delete mode 100644 base_phone/test/phonenum.yml create mode 100644 base_phone/tests/__init__.py create mode 100644 base_phone/tests/test_phone.py delete mode 100644 crm_phone/test/phonenum.yml create mode 100644 crm_phone/tests/__init__.py create mode 100644 crm_phone/tests/test_crm_phone.py diff --git a/base_phone/__openerp__.py b/base_phone/__openerp__.py index 57a7943..7a1eef0 100644 --- a/base_phone/__openerp__.py +++ b/base_phone/__openerp__.py @@ -41,7 +41,6 @@ 'web_phone.xml', ], 'qweb': ['static/src/xml/*.xml'], - 'test': ['test/phonenum.yml'], 'images': [], 'installable': True, } diff --git a/base_phone/fields.py b/base_phone/fields.py index 95541f9..0e3d409 100644 --- a/base_phone/fields.py +++ b/base_phone/fields.py @@ -45,7 +45,6 @@ class Phone(fields.Char): res_parse = phonenumbers.parse(res) res = phonenumbers.format_number( res_parse, phonenumbers.PhoneNumberFormat.INTERNATIONAL) - # print "after parse+intl res=", res except: pass # print 'cache value', res @@ -82,7 +81,7 @@ def convert_all_phone_fields(self, vals, fields_to_convert): country = self.env['res.country'].browse(vals[country_key]) else: country = self[country_key] - elif partner_key: + if partner_key and not country: if partner_key in loc_vals: partner = self.env['res.partner'].browse(vals[partner_key]) else: diff --git a/base_phone/models/res_partner.py b/base_phone/models/res_partner.py index 5e71f91..aa56956 100644 --- a/base_phone/models/res_partner.py +++ b/base_phone/models/res_partner.py @@ -11,9 +11,10 @@ class ResPartner(models.Model): _inherit = 'res.partner' _phone_name_sequence = 10 - phone = fields.Phone(country_field='country_id', partner_field=None) - mobile = fields.Phone(country_field='country_id', partner_field=None) - fax = fields.Phone(country_field='country_id', partner_field=None) + phone = fields.Phone(country_field='country_id', partner_field='parent_id') + mobile = fields.Phone( + country_field='country_id', partner_field='parent_id') + fax = fields.Phone(country_field='country_id', partner_field='parent_id') @api.multi def name_get(self): diff --git a/base_phone/test/phonenum.yml b/base_phone/test/phonenum.yml deleted file mode 100644 index ac8726c..0000000 --- a/base_phone/test/phonenum.yml +++ /dev/null @@ -1,50 +0,0 @@ -- - Write country = FR for the main company -- - !record {model: res.company, id: base.main_company}: - country_id: base.fr -- - Write french phone numbers in national format -- - !record {model: res.partner, id: partner1}: - name: Pierre Paillet - mobile: 06 42 77 42 66 - fax: (0) 1 45 42 12 42 -- - Write swiss phone numbers in international format -- - !record {model: res.partner, id: partner2}: - name: Joël Grand-Guillaume - parent_id: base.res_partner_12 - phone: +41 21 619 10 10 - mobile: +41 79 606 42 42 -- - Write invalid phone number -- - !record {model: res.partner, id: partner3}: - name: Jean Badphone - phone: 42 -- - Check that valid phone numbers have been converted to E.164 -- - !python {model: res.partner}: | - partner1 = self.browse(cr, uid, ref('partner1'), context=context) - assert partner1.mobile == '+33 6 42 77 42 66', 'Mobile number not written in E.164 format (partner1)' - assert partner1.fax == '+33 1 45 42 12 42', 'Fax number not written in E.164 format (partner1)' - partner2 = self.browse(cr, uid, ref('partner2'), context=context) - assert partner2.phone == '+41 21 619 10 10', 'Phone number not written in E.164 format (partner2)' - assert partner2.mobile == '+41 79 606 42 42', 'Mobile number not written in E.164 format (partner2)' -- - Check that invalid phone numbers are kept unchanged -- - !python {model: res.partner}: | - partner3 = self.browse(cr, uid, ref('partner3'), context=context) - assert partner3.phone == '42', 'Invalid phone numbers should not be changed' -- - Get name from phone number -- - !python {model: phone.common}: | - name = self.get_name_from_phone_number(cr, uid, '0642774266') - assert name == 'Pierre Paillet', 'Wrong result for get_name_from_phone_number' - name2 = self.get_name_from_phone_number(cr, uid, '0041216191010') - assert name2 == u'Joël Grand-Guillaume (Camptocamp)', 'Wrong result for get_name_from_phone_number (partner2)' diff --git a/base_phone/tests/__init__.py b/base_phone/tests/__init__.py new file mode 100644 index 0000000..03639ac --- /dev/null +++ b/base_phone/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_phone diff --git a/base_phone/tests/test_phone.py b/base_phone/tests/test_phone.py new file mode 100644 index 0000000..9986890 --- /dev/null +++ b/base_phone/tests/test_phone.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# © 2016 Akretion France (Alexis de Lattre ) + +from openerp.tests.common import TransactionCase + + +class TestPhone(TransactionCase): + + def test_phone(self): + company = self.env.ref('base.main_company') + company.country_id = self.env.ref('base.fr').id + rpo = self.env['res.partner'] + # Create an existing partner without country + partner1 = rpo.create({ + 'name': u'Pierre Paillet', + 'phone': '04-72-08-87-32', + 'mobile': '06.42.77.42.66', + 'fax': '(0) 1 45 42 12 42', + }) + self.assertEquals(partner1.phone, '+33 4 72 08 87 32') + self.assertEquals(partner1.mobile, '+33 6 42 77 42 66') + self.assertEquals(partner1.fax, '+33 1 45 42 12 42') + # Create a partner with country + self.env.ref('base.res_partner_12').country_id =\ + self.env.ref('base.ch').id + partner2 = rpo.create({ + 'name': u'Joël Grand-Guillaume', + 'parent_id': self.env.ref('base.res_partner_12').id, + 'use_parent_address': True, + 'phone': '(0) 21 619 10 10', + 'mobile': '(0) 79 606 42 42', + }) + self.assertEquals(partner2.country_id, self.env.ref('base.ch')) + self.assertEquals(partner2.phone, '+41 21 619 10 10') + self.assertEquals(partner2.mobile, '+41 79 606 42 42') + # Write on an existing partner + agrolait = self.env.ref('base.res_partner_2') + self.assertEquals(agrolait.country_id, self.env.ref('base.be')) + agrolait.write({'phone': '(0) 2 391 43 74'}) + self.assertEquals(agrolait.phone, '+32 2 391 43 74') + # Write on an existing partner with country at the same time + agrolait.write({ + 'fax': '04 72 89 32 43', + 'country_id': self.env.ref('base.fr').id, + }) + self.assertEquals(agrolait.fax, '+33 4 72 89 32 43') + # Write an invalid phone number + partner2.fax = '42' + self.assertEquals(partner2.fax, '42') + # Test get_name_from_phone_number + pco = self.env['phone.common'] + name = pco.get_name_from_phone_number('0642774266') + self.assertEquals(name, 'Pierre Paillet') + name2 = pco.get_name_from_phone_number('0041216191010') + self.assertEquals(name2, u'Joël Grand-Guillaume (Camptocamp)') diff --git a/crm_phone/__init__.py b/crm_phone/__init__.py index 73e0cb1..1ec0331 100644 --- a/crm_phone/__init__.py +++ b/crm_phone/__init__.py @@ -1,23 +1,4 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# CRM Phone module for Odoo/OpenERP -# Copyright (C) 2014 Alexis de Lattre -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as -# published by the Free Software Foundation, either version 3 of the -# License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . -# -############################################################################## from . import crm_phone from . import wizard diff --git a/crm_phone/__openerp__.py b/crm_phone/__openerp__.py index bd6ea0a..9cb7bb7 100644 --- a/crm_phone/__openerp__.py +++ b/crm_phone/__openerp__.py @@ -36,7 +36,6 @@ for any help or question about this module. 'wizard/create_crm_phonecall_view.xml', ], 'demo': ['demo/crm_phonecall.xml'], - 'test': ['test/phonenum.yml'], 'installable': True, 'auto_install': True, } diff --git a/crm_phone/crm_phone.py b/crm_phone/crm_phone.py index 387fa1e..599c563 100644 --- a/crm_phone/crm_phone.py +++ b/crm_phone/crm_phone.py @@ -10,9 +10,9 @@ class CrmLead(models.Model): _inherit = 'crm.lead' _phone_name_sequence = 20 - phone = Phone(country_field='country_id') - mobile = Phone(country_field='country_id') - fax = Phone(country_field='country_id') + phone = Phone(country_field='country_id', partner_field='partner_id') + mobile = Phone(country_field='country_id', partner_field='partner_id') + fax = Phone(country_field='country_id', partner_field='partner_id') phonecall_ids = fields.One2many( 'crm.phonecall', 'opportunity_id', string='Phone Calls') phonecall_count = fields.Integer( diff --git a/crm_phone/test/phonenum.yml b/crm_phone/test/phonenum.yml deleted file mode 100644 index b64074b..0000000 --- a/crm_phone/test/phonenum.yml +++ /dev/null @@ -1,41 +0,0 @@ -- - Write french phone numbers in national format -- - !record {model: crm.lead, id: lead1}: - name: Jacques Toufaux - mobile: 06 42 77 42 77 - fax: (0) 1 45 44 42 43 - country_id: base.fr -- - Write swiss phone numbers in national format -- - !record {model: crm.lead, id: lead2}: - name: Michel Content - country_id: base.ch - phone: 04 31 23 45 67 -- - Create a german lead -- - !record {model: crm.lead, id: lead3}: - name: Angela Strasse - country_id: base.de -- - Check that valid phone numbers have been converted to E.164 -- - !python {model: crm.lead}: | - lead1 = self.browse(cr, uid, ref('lead1'), context=context) - assert lead1.mobile == '+33 6 42 77 42 77', 'Mobile number not written in E.164 format (lead1)' - assert lead1.fax == '+33 1 45 44 42 43', 'Fax number not written in E.164 format (lead1)' - lead2 = self.browse(cr, uid, ref('lead2'), context=context) - assert lead2.phone == '+41 43 123 45 67', 'Phone number not written in E.164 format (lead2)' - self.write(cr, uid, ref('lead3'), {'phone': '08912345678'}) - lead3 = self.browse(cr, uid, ref('lead3'), context=context) - assert lead3.phone == '+49 89 12345678', 'Phone number not written in E.164 format (lead3)' -- - Get name from phone number -- - !python {model: phone.common}: | - name = self.get_name_from_phone_number(cr, uid, '0642774277') - assert name == 'Jacques Toufaux', 'Wrong result for get_name_from_phone_number (lead1)' - name2 = self.get_name_from_phone_number(cr, uid, '0041431234567') - assert name2 == 'Michel Content', 'Wrong result for get_name_from_phone_number (lead2)' diff --git a/crm_phone/tests/__init__.py b/crm_phone/tests/__init__.py new file mode 100644 index 0000000..27ddc49 --- /dev/null +++ b/crm_phone/tests/__init__.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +from . import test_crm_phone diff --git a/crm_phone/tests/test_crm_phone.py b/crm_phone/tests/test_crm_phone.py new file mode 100644 index 0000000..2bda8dc --- /dev/null +++ b/crm_phone/tests/test_crm_phone.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +# © 2016 Akretion France (Alexis de Lattre ) + + +from openerp.tests.common import TransactionCase + + +class TestCRMPhone(TransactionCase): + + def test_crm_phone(self): + clo = self.env['crm.lead'] + lead1 = clo.create({ + 'name': 'The super deal of the year !', + 'partner_name': 'Ford', + 'contact_name': 'Jacques Toufaux', + 'mobile': '06.42.77.42.77', + 'fax': '(0) 1 45 44 42 43', + 'country_id': self.env.ref('base.fr').id, + }) + self.assertEquals(lead1.mobile, '+33 6 42 77 42 77') + self.assertEquals(lead1.fax, '+33 1 45 44 42 43') + lead2 = clo.create({ + 'name': u'Automobile Odoo deployment', + 'partner_name': u'Kia', + 'contact_name': u'Mikaël Content', + 'country_id': self.env.ref('base.ch').id, + 'phone': '04 31 23 45 67', + }) + self.assertEquals(lead2.phone, '+41 43 123 45 67') + lead3 = clo.create({ + 'name': 'Angela Strasse', + 'country_id': self.env.ref('base.de').id, + }) + lead3.write({'phone': '08912345678'}) + self.assertEquals(lead3.phone, '+49 89 12345678') + lead4 = clo.create({ + 'name': 'Large Odoo deployment', + 'partner_id': self.env.ref('base.res_partner_2').id, + }) + lead4.write({'mobile': '(0) 2-391-43-75'}) + self.assertEquals(lead4.mobile, '+32 2 391 43 75') + pco = self.env['phone.common'] + name = pco.get_name_from_phone_number('0642774277') + self.assertEquals(name, 'Jacques Toufaux (Ford)') + name2 = pco.get_name_from_phone_number('0041431234567') + self.assertEquals(name2, u'Mikaël Content (Kia)')