Skip to content

Commit

Permalink
[IMP] improve testing class to avoid issue with missing database, als…
Browse files Browse the repository at this point in the history
…o use SetUpClass to load the model only once
  • Loading branch information
sebastienbeau committed Aug 15, 2018
1 parent 01e17c1 commit cafe608
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions connector_search_engine/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Sébastien BEAU <sebastien.beau@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
import mock
from odoo import api, models
from odoo import models
from odoo.addons.component.tests.common import SavepointComponentCase


Expand All @@ -25,42 +25,47 @@ class TestSeBackendCase(SavepointComponentCase):
Tests With a fake Se Backend
"""

def _init_test_model(self, model_cls):
@classmethod
def _init_test_model(cls, model_cls):
"""
Function to init/create a new Odoo Model during unit test.
Based on base_kanban_stage/test/test_base_kanban_abstract.py
:param model_cls: Odoo Model class
:return: instance of model (empty)
"""
registry = self.env.registry
registry.enter_test_mode()
cr = self.env.cr
registry = cls.env.registry
cr = cls.env.cr
model_cls._build_model(registry, cr)
model = self.env[model_cls._name].with_context(todo=[])
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()
return self.env[model_cls._name]
return cls.env[model_cls._name]

@classmethod
def setUpClass(cls):
super(TestSeBackendCase, cls).setUpClass()
cls.env.cr.commit = mock.MagicMock()
cls._init_test_model(TestSeBackend)

def setUp(self):
super(TestSeBackendCase, self).setUp()
# To load a new Model, we have to use a new cursor and env.
# Because there is a commit on the model._auto_init()
# Based on base_kanban_stage/test/test_base_kanban_abstract.py
# self.registry.enter_test_mode()
# self.old_cursor = self.cr
# self.cr = self.registry.cursor()
self.cr.commit = mock.MagicMock()
self.env = api.Environment(self.cr, self.uid, {})
self._init_test_model(TestSeBackend)
self.se_backend = self.env['test.se.backend'].create({
'specific_model': 'test.se.backend',
})

def tearDown(self):
self.registry.leave_test_mode()
super(TestSeBackendCase, self).tearDown()
@classmethod
def tearDownClass(cls):
# The cursor have been not commited as it have been mocked
# so there is not table 'test_se_backend' in the DB
# but the registry have been polluted by the class test.se.backend
# To avoid having error message (missing table 'test.se.backend' when
# running the test on several module
# We flag this class as an abstract class
# so odoo will do not care that the table do not exist
cls.env.registry['test.se.backend']._abstract = True
super(TestSeBackendCase, cls).tearDownClass()

0 comments on commit cafe608

Please sign in to comment.