Skip to content

Commit

Permalink
partner_identification: fix/imp tests on res.partner
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed Nov 29, 2017
1 parent b68a482 commit 04a5bb7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 56 deletions.
1 change: 1 addition & 0 deletions partner_identification/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import res_partner_id_number
from . import res_partner_id_category
from . import res_partner
from . import test_models
25 changes: 25 additions & 0 deletions partner_identification/models/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# Copyright 2017 LasLabs Inc.
# Copyright 2017 Camptocamp
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from odoo import fields, models, tools
import os

testing = tools.config.get('test_enable') or os.environ.get('ODOO_TEST_ENABLE')

if testing:
class ResPartner(models.Model):
_inherit = 'res.partner'

social_security = fields.Char(
compute=lambda s: s._compute_identification(
'social_security', 'SSN',
),
inverse=lambda s: s._inverse_identification(
'social_security', 'SSN',
),
search=lambda s, *a: s._search_identification(
'SSN', *a
),
)
16 changes: 12 additions & 4 deletions partner_identification/tests/test_partner_identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,35 @@
from psycopg2._psycopg import IntegrityError
from odoo.tests import common
from odoo.exceptions import ValidationError
from odoo.tools import mute_logger


class TestPartnerIdentificationBase(common.TransactionCase):

def test_base_functionalities(self):
"""Dummy CRUD test
"""
def test_create_id_category(self):
partner_id_category = self.env['res.partner.id_category'].create({
'code': 'id_code',
'name': 'id_name',
})
self.assertEqual(partner_id_category.name, 'id_name')
self.assertEqual(partner_id_category.code, 'id_code')

@mute_logger('odoo.sql_db')
def test_update_partner_with_no_category(self):
partner_1 = self.env.ref('base.res_partner_1')
self.assertEqual(len(partner_1.id_numbers), 0)
# create without required category
with self.assertRaises(IntegrityError), self.cr.savepoint():
with self.assertRaises(IntegrityError):
partner_1.write({'id_numbers': [(0, 0, {
'name': '1234',
})]})

def test_update_partner_with_category(self):
partner_1 = self.env.ref('base.res_partner_1')
partner_id_category = self.env['res.partner.id_category'].create({
'code': 'new_code',
'name': 'new_name',
})
# successful creation
partner_1.write({'id_numbers': [(0, 0, {
'name': '1234',
Expand Down
60 changes: 8 additions & 52 deletions partner_identification/tests/test_res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,33 @@
# Copyright 2017 LasLabs Inc.
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import fields, models
from odoo.tests import common
from odoo.exceptions import ValidationError


class ResPartner(models.Model):
_inherit = 'res.partner'

social_security = fields.Char(
compute=lambda s: s._compute_identification(
'social_security', 'SSN',
),
inverse=lambda s: s._inverse_identification(
'social_security', 'SSN',
),
search=lambda s, *a: s._search_identification(
'SSN', *a
),
)


class TestResPartner(common.SavepointCase):

@classmethod
def _init_test_model(cls, model_cls):
""" Build a model from model_cls in order to test abstract models.
Note that this does not actually create a table in the database, so
there may be some unidentified edge cases.
Args:
model_cls (openerp.models.BaseModel): Class of model to initialize
Returns:
model_cls: Instance
"""
registry = cls.env.registry
cr = cls.env.cr
inst = model_cls._build_model(registry, cr)
model = cls.env[model_cls._inherit].with_context(todo=[])
model._prepare_setup()
model._setup_base()
model._setup_fields()
model._setup_complete()
model._auto_init()
model.init()
return inst

@classmethod
def setUpClass(cls):
super(TestResPartner, cls).setUpClass()
cls.env.registry.enter_test_mode()
cls._init_test_model(ResPartner)

def setUp(self):
super(TestResPartner, self).setUp()
bad_cat = self.env['res.partner.id_category'].create({
bad_cat = cls.env['res.partner.id_category'].create({
'code': 'another_code',
'name': 'another_name',
})
self.env['res.partner.id_number'].create({
cls.env['res.partner.id_number'].create({
'name': 'Bad ID',
'category_id': bad_cat.id,
'partner_id': self.env.user.partner_id.id,
'partner_id': cls.env.user.partner_id.id,
})
self.partner_id_category = self.env['res.partner.id_category'].create({
cls.partner_id_category = cls.env['res.partner.id_category'].create({
'code': 'id_code',
'name': 'id_name',
})
self.partner = self.env.user.partner_id
self.partner_id = self.env['res.partner.id_number'].create({
cls.partner = cls.env.user.partner_id
cls.partner_id = cls.env['res.partner.id_number'].create({
'name': 'Good ID',
'category_id': self.partner_id_category.id,
'partner_id': self.partner.id,
'category_id': cls.partner_id_category.id,
'partner_id': cls.partner.id,
})

def test_compute_identification(self):
Expand Down

0 comments on commit 04a5bb7

Please sign in to comment.