-
-
Notifications
You must be signed in to change notification settings - Fork 481
[ADD] template: Abstract model test strategy #242
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright <YEAR(S)> <AUTHOR(S)> | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
|
||
| from openerp import models | ||
|
|
||
|
|
||
| class AbstractSomething(models.AbstractModel): | ||
| _name = "abstract.something" | ||
| _description = "Abstract Somethings" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright <YEAR(S)> <AUTHOR(S)> | ||
| # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). | ||
|
|
||
| from openerp import api, models | ||
| from openerp.tests.common import SingleTransactionCase | ||
|
|
||
|
|
||
| class AbstractSomethingTester(models.Model): | ||
| """ It provides a real model object to test the abstract with """ | ||
| _name = 'abstract.something.tester' | ||
| _description = 'Abstract Something Tester' | ||
| _inherit = 'abstract.something' | ||
|
|
||
|
|
||
| class TestAbstractSomething(SingleTransactionCase): | ||
| @classmethod | ||
| def _init_test_model(cls, model_cls): | ||
| """ It builds a model from model_cls in order to test abstract models | ||
|
|
||
| Args: | ||
| model_cls: (openerp.models.BaseModel) Class of model to initialize | ||
| Returns: | ||
| Model instance | ||
| """ | ||
| model_cls._build_model(cls.registry, cls.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() | ||
| return model | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| super(TestAbstractSomething, cls).setUpClass() | ||
| cls.registry.enter_test_mode() | ||
| cls.old_cursor = cls.cr | ||
| cls.cr = cls.registry.cursor() | ||
| cls.env = api.Environment(cls.cr, cls.uid, {}) | ||
| cls.test_model = cls._init_test_model(AbstractSomethingTester) | ||
|
|
||
| @classmethod | ||
| def tearDownClass(cls): | ||
| cls.registry.leave_test_mode() | ||
| cls.registry[cls.test_model._name]._abstract = True | ||
| cls.registry[cls.test_model._name]._auto = False | ||
| cls.cr = cls.old_cursor | ||
| cls.env = api.Environment(cls.cr, cls.uid, {}) | ||
| super(TestAbstractSomething, cls).tearDownClass() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the method that actually creates a table for the model, and it will break the isolation provided by
TransactionCase: https://github.com/odoo/odoo/blob/18456b9d42769a522adf86183746b49735e9d8b3/odoo/models.py#L2535. Unfortunately, I don't see a way to proceed beyond the_setup_complete()step without introducing this issue, and doing so seems to defeat the entire purpose of building a model in this way, so the last few calls here should probably be dropped.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm yep good call, thanks @obulkin. I can't think of an instance where the table be required outside of the test transaction, so I believe you're correct in being able to remove it. Well, that and the whole necessity thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep I've forgotten to mock cr.commit...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhhh mock
cr.commit! And there's the solution, I like this better than removing the table creation. Thanks @lmignonThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use a test cursor like the one used in HttpCase instead. It mocks cr.commit by itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL, thanks @yajo. So glad I made this PR, we're really drilling to the true solution!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh. Nice find, @yajo. I wonder why
TransactionCasedoesn't just use the test cursor/savepoint approach.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wasn't so nice at the beginning, it's just getting nicer now. 😆
FTR,
TransactionCasemakes a rollback instead. Not sure why, but that's what it does.