Package: odoo-test-helper
odoo-test-helper is toolbox for writing odoo testLoading Fake modelsSometime you build an abstract module that can be use by many modules.
In such case, if you want to test it with real records you need to register real models.One solution is to create amodule_testmodule
with a little implementation that use your abstract model.One other solution is define test only models and load them in tests.
This lib makes this possible and easy.ExampleThere is an example of test here:test_example.py.This example load the class ResPartner from the file:models.py.Real implementation case can be found in the following moduleconnector_search_engine.base_url.How to importBe carefull importing fake class must be done in the right way.
Importing a file will automatically add all the class in the “module_to_models”
variable. The importmustbe done after the backup !Wrong wayfromodoo.testsimportSavepointCasefromodoo_test_helperimportFakeModelLoader# The fake class is imported here !! It's wrong# And be carefull even if you only import ResPartner# all class in the file models will be proceded by odoo# so no **direct import** of a file that contain fake modelfrom.modelsimportResPartnerclassTestMixin(SavepointCase):@classmethoddefsetUpClass(cls):super(TestMixin,cls).setUpClass()cls.loader=FakeModelLoader(cls.env,cls.__module__)cls.loader.backup_registry()cls.loader.update_registry((ResPartner,))@classmethoddeftearDownClass(cls):cls.loader.restore_registry()super(TestMixin,cls).tearDownClass()deftest_create(self):partner=self.env["res.partner"].create({"name":"BAR","test_char":"youhou"})self.assertEqual(partner.name,"FOO-BAR")self.assertEqual(partner.test_char,"youhou")Right Wayfromodoo.testsimportSavepointCasefromodoo_test_helperimportFakeModelLoaderclassTestMixin(SavepointCase):@classmethoddefsetUpClass(cls):super(TestMixin,cls).setUpClass()cls.loader=FakeModelLoader(cls.env,cls.__module__)cls.loader.backup_registry()# The fake class is imported here !! After the backup_registryfrom.modelsimportResPartnercls.loader.update_registry((ResPartner,))@classmethoddeftearDownClass(cls):cls.loader.restore_registry()super(TestMixin,cls).tearDownClass()deftest_create(self):partner=self.env["res.partner"].create({"name":"BAR","test_char":"youhou"})self.assertEqual(partner.name,"FOO-BAR")self.assertEqual(partner.test_char,"youhou")ContributorSébastien BEAU <sebastien.beau@akretion.com>Laurent Mignon <laurent.mignon@acsone.eu>Simone Orsi <simone.orsi@camptocamp.com>HistoryThis module is inspired of the following mixin code that can be found in OCA and shopinvader repositoryMixin in OCA:https://github.com/OCA/search-engine/blob/7fd85a74180cfff30e212fca01ebeba6c54ee294/connector_search_engine/tests/models_mixin.pyMixin in Shopinvader:https://github.com/shopinvader/odoo-shopinvader/blob/b81b921ea52c911e5b33afac88adb8f9a1c02626/base_url/tests/models_mixin.pyIntial Authors areLaurent Mignon <laurent.mignon@acsone.eu>Simone Orsi <simone.orsi@camptocamp.com>Refactor/extraction have been done bySébastien BEAU <sebastien.beau@akretion.com>This refactor try to load all class correctly like Odoo does with the exact same syntaxNote this refactor/extraction have been done to fix the test of the following issuehttps://github.com/shopinvader/odoo-shopinvader/pull/607Changes2.1.1[FIX] FakeModelLoader: avoid loading module again on restore_registry() for Odoo 15.0+
[FIX] Ensures registy is cleaned up in addons without models2.1.0[IMP] Allow to ignore Odoo core modules to avoid warning2.0.5.gitignore added2.0.4[FIX] AttributeError in Odoo 15+, regression introduced in 2.0.32.0.3[FIX] restore_registry in Odoo 16Seeodoo/odoo@cd12293This new attribute is the source of truth for the base classes
and in setup_models (called further down in the modified code in this PR),
the model’s base classes are reset from it:https://github.com/odoo/odoo/blob/e1f06479a526c703ccabc441b1e194646206b966/odoo/models.py#L2728-L2730.The test failure fixed by this PR can be inspected inhttps://app.travis-ci.com/github/OCA/odoo-test-helper/builds/2584533312.0.2Fixmockimport for v152.0.1Fix support for Odoo 15.02.0.0Move to OCARe-license to LGPL1.1.0Refactoring (misc imp/fix)1.0.0Initial release