Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] FakeModelLoader: avoid loading module again on restore_registry() for version < 15 #28

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ install:
- export PATH=${HOME}/maintainer-quality-tools/travis:${PATH}
- travis_install_nightly
- pip install -e .
- mv tests/test_helper test_helper
- mv tests/test_helper_bis test_helper_bis
- mv tests/test_helper* .

script:
- travis_run_tests
Expand Down
23 changes: 12 additions & 11 deletions odoo_test_helper/fake_model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,16 @@ def restore_registry(self):

self._clean_module_to_model()

# reload is need to reset correctly the field on the record
with mock.patch.object(self.env.cr, "commit"):
# Only before V15
if hasattr(self.env.registry, "model_cache"):
# reload is needed to reset correctly the field on the record
# but only up to Odoo 14
if version_info[0] < 15:

with mock.patch.object(self.env.cr, "commit"):
self.env.registry.model_cache.clear()
model_names = self.env.registry.load(
self.env.cr, FakePackage(self._module_name)
)
self.env.registry.setup_models(self.env.cr)
self.env.registry.init_models(
self.env.cr, model_names, {"module": self._module_name}
)
model_names = self.env.registry.load(
self.env.cr, FakePackage(self._module_name)
)
self.env.registry.setup_models(self.env.cr)
self.env.registry.init_models(
self.env.cr, model_names, {"module": self._module_name}
)
1 change: 1 addition & 0 deletions tests/test_helper_extended/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
19 changes: 19 additions & 0 deletions tests/test_helper_extended/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).


{
"name": "Test Helper Extended",
"summary": "Another fake module extending fake module Test Helper for testing",
"version": "0.0.0",
"category": "Uncategorized",
"website": "www.akretion.com",
"author": " Akretion",
"license": "LGPL-3",
"application": False,
"installable": True,
"external_dependencies": {"python": [], "bin": []},
"depends": ["test_helper"],
"data": [],
"demo": [],
"qweb": [],
}
1 change: 1 addition & 0 deletions tests/test_helper_extended/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_mixin
10 changes: 10 additions & 0 deletions tests/test_helper_extended/models/test_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import fields, models


class TestMixin(models.AbstractModel):
_inherit = "test.mixin"
_description = "Test Mixin Extension"

test_char_02 = fields.Char()
1 change: 1 addition & 0 deletions tests/test_helper_extended/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_registry
33 changes: 33 additions & 0 deletions tests/test_helper_extended/tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo.release import version_info

if version_info[0] >= 15:
from odoo.tests import TransactionCase

from odoo_test_helper import FakeModelLoader

class TestMixin(TransactionCase):
def test_mixin_after_reloading_parent_module(self):
self.assertIn("test.mixin", self.env.registry)
self.assertIn("test_char", self.env["test.mixin"]._fields)
self.assertIn("test_char_02", self.env["test.mixin"]._fields)

loader = FakeModelLoader(self.env, "odoo.addons.test_helper")
loader.backup_registry()

# trigger reload of parent module: test_helper
loader.update_registry(())

self.assertIn("test.mixin", self.env.registry)
self.assertIn("test_char", self.env["test.mixin"]._fields)
# new field should not be there anymore
self.assertNotIn("test_char_02", self.env["test.mixin"]._fields)

# but now we restore registry
loader.restore_registry()

self.assertIn("test.mixin", self.env.registry)
self.assertIn("test_char", self.env["test.mixin"]._fields)
# new field should be there again
self.assertIn("test_char_02", self.env["test.mixin"]._fields)
Loading