Browse Source

Implement newer abstract test strategy

pull/681/head
Dave Lasley 8 years ago
parent
commit
e0d6bb01d6
No known key found for this signature in database GPG Key ID: 7DDBA4BA81B934CF
  1. 4
      base_kanban_stage/models/base_kanban_abstract.py
  2. 8
      base_kanban_stage/models/base_kanban_stage.py
  3. 62
      base_kanban_stage/tests/test_base_kanban_abstract.py
  4. 24
      base_kanban_stage/tests/test_base_kanban_stage.py
  5. 6
      base_kanban_stage/views/base_kanban_stage.xml

4
base_kanban_stage/models/base_kanban_abstract.py

@ -37,7 +37,7 @@ class BaseKanbanAbstract(models.AbstractModel):
copy=False, copy=False,
help='The Kanban stage that this record is currently in', help='The Kanban stage that this record is currently in',
default=lambda s: s._default_stage_id(), default=lambda s: s._default_stage_id(),
domain=lambda s: [('res_model.model', '=', s._name)],
domain=lambda s: [('res_model_id.model', '=', s._name)],
) )
user_id = fields.Many2one( user_id = fields.Many2one(
string='Assigned To', string='Assigned To',
@ -108,7 +108,7 @@ class BaseKanbanAbstract(models.AbstractModel):
stage_model = self.env['base.kanban.stage'] stage_model = self.env['base.kanban.stage']
if access_rights_uid: if access_rights_uid:
stage_model = stage_model.sudo(access_rights_uid) stage_model = stage_model.sudo(access_rights_uid)
stages = stage_model.search([('res_model.model', '=', self._name)])
stages = stage_model.search([('res_model_id.model', '=', self._name)])
names = [(r.id, r.display_name) for r in stages] names = [(r.id, r.display_name) for r in stages]
fold = {r.id: r.fold for r in stages} fold = {r.id: r.fold for r in stages}
return names, fold return names, fold

8
base_kanban_stage/models/base_kanban_stage.py

@ -8,7 +8,7 @@ from odoo import api, fields, models
class BaseKanbanStage(models.Model): class BaseKanbanStage(models.Model):
_name = 'base.kanban.stage' _name = 'base.kanban.stage'
_description = 'Kanban Stage' _description = 'Kanban Stage'
_order = 'res_model, sequence'
_order = 'res_model_id, sequence'
name = fields.Char( name = fields.Char(
string='Stage Name', string='Stage Name',
@ -64,18 +64,18 @@ class BaseKanbanStage(models.Model):
help='Determines whether this stage will be collapsed down in Kanban' help='Determines whether this stage will be collapsed down in Kanban'
' views', ' views',
) )
res_model = fields.Many2one(
res_model_id = fields.Many2one(
string='Associated Model', string='Associated Model',
comodel_name='ir.model', comodel_name='ir.model',
required=True, required=True,
index=True, index=True,
help='The model that this Kanban stage will be used for', help='The model that this Kanban stage will be used for',
domain=[('transient', '=', False)], domain=[('transient', '=', False)],
default=lambda s: s._default_res_model(),
default=lambda s: s._default_res_model_id(),
) )
@api.model @api.model
def _default_res_model(self):
def _default_res_model_id(self):
'''Useful when creating stages from a Kanban view for another model''' '''Useful when creating stages from a Kanban view for another model'''
action_id = self.env.context.get('params', {}).get('action') action_id = self.env.context.get('params', {}).get('action')
action = self.env['ir.actions.act_window'].browse(action_id) action = self.env['ir.actions.act_window'].browse(action_id)

62
base_kanban_stage/tests/test_base_kanban_abstract.py

@ -3,34 +3,60 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html). # License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
from odoo import models from odoo import models
from odoo.tests.common import TransactionCase
from odoo.tests.common import SavepointCase
class BaseKanbanAbstractTester(models.Model):
class BaseKanbanAbstractTester(models.TransientModel):
_name = 'base.kanban.abstract.tester' _name = 'base.kanban.abstract.tester'
_inherit = 'base.kanban.abstract' _inherit = 'base.kanban.abstract'
class TestBaseKanbanAbstract(TransactionCase):
def setUp(self):
super(TestBaseKanbanAbstract, self).setUp()
class TestBaseKanbanAbstract(SavepointCase):
BaseKanbanAbstractTester._build_model(self.registry, self.cr)
self.test_model = self.env[BaseKanbanAbstractTester._name]
@classmethod
def _init_test_model(cls, model_cls):
""" It builds a model from model_cls in order to test abstract models.
test_model_type = self.env['ir.model'].create({
'model': BaseKanbanAbstractTester._name,
'name': 'Kanban Abstract - Test Model',
'state': 'base',
})
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._name].with_context(todo=[])
model._prepare_setup()
model._setup_base(partial=False)
model._setup_fields(partial=False)
model._setup_complete()
model._auto_init()
model.init()
model._auto_end()
cls.test_model_record = cls.env['ir.model'].search([
('name', '=', model._name),
])
return inst
@classmethod
def setUpClass(cls):
super(TestBaseKanbanAbstract, cls).setUpClass()
cls.env.registry.enter_test_mode()
cls._init_test_model(BaseKanbanAbstractTester)
cls.test_model = cls.env[BaseKanbanAbstractTester._name]
def setUp(self):
super(TestBaseKanbanAbstract, self).setUp()
test_stage_1 = self.env['base.kanban.stage'].create({ test_stage_1 = self.env['base.kanban.stage'].create({
'name': 'Test Stage 1', 'name': 'Test Stage 1',
'res_model': test_model_type.id,
'res_model_id': self.test_model_record.id,
}) })
test_stage_2 = self.env['base.kanban.stage'].create({ test_stage_2 = self.env['base.kanban.stage'].create({
'name': 'Test Stage 2', 'name': 'Test Stage 2',
'res_model': test_model_type.id,
'res_model_id': self.test_model_record.id,
'fold': True, 'fold': True,
}) })
@ -38,7 +64,7 @@ class TestBaseKanbanAbstract(TransactionCase):
self.id_2 = test_stage_2.id self.id_2 = test_stage_2.id
def test_read_group_stage_ids_base_case(self): def test_read_group_stage_ids_base_case(self):
'''It should return a structure with the proper content'''
"""It should return a structure with the proper content"""
self.assertEqual( self.assertEqual(
self.test_model._read_group_stage_ids(), self.test_model._read_group_stage_ids(),
( (
@ -48,13 +74,13 @@ class TestBaseKanbanAbstract(TransactionCase):
) )
def test_read_group_stage_ids_correct_associated_model(self): def test_read_group_stage_ids_correct_associated_model(self):
'''It should only return info for stages with right associated model'''
"""It should only return info for stages with right associated model"""
stage_model = self.env['ir.model'].search([ stage_model = self.env['ir.model'].search([
('model', '=', 'base.kanban.stage'), ('model', '=', 'base.kanban.stage'),
]) ])
self.env['base.kanban.stage'].create({ self.env['base.kanban.stage'].create({
'name': 'Test Stage 3', 'name': 'Test Stage 3',
'res_model': stage_model.id,
'res_model_id': stage_model.id,
}) })
self.assertEqual( self.assertEqual(
@ -66,7 +92,7 @@ class TestBaseKanbanAbstract(TransactionCase):
) )
def test_default_stage_id(self): def test_default_stage_id(self):
''' It should return an empty RecordSet '''
""" It should return an empty RecordSet """
self.assertEqual( self.assertEqual(
self.env['base.kanban.abstract']._default_stage_id(), self.env['base.kanban.abstract']._default_stage_id(),
self.env['base.kanban.stage'] self.env['base.kanban.stage']

24
base_kanban_stage/tests/test_base_kanban_stage.py

@ -6,23 +6,23 @@ from odoo.tests.common import TransactionCase
class TestBaseKanbanStage(TransactionCase): class TestBaseKanbanStage(TransactionCase):
def test_default_res_model_no_params(self):
def test_default_res_model_id_no_params(self):
'''It should return empty ir.model Recordset if no params in context''' '''It should return empty ir.model Recordset if no params in context'''
test_stage = self.env['base.kanban.stage'].with_context({}) test_stage = self.env['base.kanban.stage'].with_context({})
res_model = test_stage._default_res_model()
res_model_id = test_stage._default_res_model_id()
self.assertFalse(res_model)
self.assertEqual(res_model._name, 'ir.model')
self.assertFalse(res_model_id)
self.assertEqual(res_model_id._name, 'ir.model')
def test_default_res_model_no_action(self):
def test_default_res_model_id_no_action(self):
'''It should return empty ir.model Recordset if no action in params''' '''It should return empty ir.model Recordset if no action in params'''
test_stage = self.env['base.kanban.stage'].with_context(params={}) test_stage = self.env['base.kanban.stage'].with_context(params={})
res_model = test_stage._default_res_model()
res_model_id = test_stage._default_res_model_id()
self.assertFalse(res_model)
self.assertEqual(res_model._name, 'ir.model')
self.assertFalse(res_model_id)
self.assertEqual(res_model_id._name, 'ir.model')
def test_default_res_model_info_in_context(self):
def test_default_res_model_id_info_in_context(self):
'''It should return correct ir.model record if info in context''' '''It should return correct ir.model record if info in context'''
test_action = self.env['ir.actions.act_window'].create({ test_action = self.env['ir.actions.act_window'].create({
'name': 'Test Action', 'name': 'Test Action',
@ -33,11 +33,11 @@ class TestBaseKanbanStage(TransactionCase):
) )
self.assertEqual( self.assertEqual(
test_stage._default_res_model(),
test_stage._default_res_model_id(),
self.env['ir.model'].search([('model', '=', 'res.users')]) self.env['ir.model'].search([('model', '=', 'res.users')])
) )
def test_default_res_model_ignore_self(self):
def test_default_res_model_id_ignore_self(self):
'''It should not return ir.model record corresponding to stage model''' '''It should not return ir.model record corresponding to stage model'''
test_action = self.env['ir.actions.act_window'].create({ test_action = self.env['ir.actions.act_window'].create({
'name': 'Test Action', 'name': 'Test Action',
@ -47,4 +47,4 @@ class TestBaseKanbanStage(TransactionCase):
params={'action': test_action.id}, params={'action': test_action.id},
) )
self.assertFalse(test_stage._default_res_model())
self.assertFalse(test_stage._default_res_model_id())

6
base_kanban_stage/views/base_kanban_stage.xml

@ -15,7 +15,7 @@
<sheet> <sheet>
<group string="Core Info" name="core_info"> <group string="Core Info" name="core_info">
<field name="name"/> <field name="name"/>
<field name="res_model"/>
<field name="res_model_id"/>
<field name="sequence"/> <field name="sequence"/>
<field name="fold"/> <field name="fold"/>
<field name="description"/> <field name="description"/>
@ -37,7 +37,7 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<tree string="Kanban Stages"> <tree string="Kanban Stages">
<field name="name"/> <field name="name"/>
<field name="res_model"/>
<field name="res_model_id"/>
<field name="sequence"/> <field name="sequence"/>
</tree> </tree>
</field> </field>
@ -49,7 +49,7 @@
<field name="arch" type="xml"> <field name="arch" type="xml">
<search string="Kanban Stages"> <search string="Kanban Stages">
<field name="name"/> <field name="name"/>
<field name="res_model"/>
<field name="res_model_id"/>
<field name="sequence"/> <field name="sequence"/>
</search> </search>
</field> </field>

Loading…
Cancel
Save