-
Notifications
You must be signed in to change notification settings - Fork 15
[FIX] databases: neutralize with the custom addons of the database #180
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
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
2 changes: 2 additions & 0 deletions
2
tests/resources/repositories/test/test-addons/addon_01/data/neutralize.sql
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,2 @@ | ||
| -- Neutralization script shipped by a custom module, used by tests. | ||
| SELECT 1; |
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,96 @@ | ||
| from argparse import Namespace | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from odev.commands.database.quickstart import QuickStartCommand | ||
| from odev.common.databases import LocalDatabase, Repository | ||
|
|
||
| from tests.fixtures import OdevTestCase | ||
|
|
||
|
|
||
| class TestQuickStartLinksRepository(OdevTestCase): | ||
| """The repository must be linked before the dump is restored, and still be linked afterwards.""" | ||
|
|
||
| def make_command(self, repository: Repository | None) -> QuickStartCommand: | ||
| command = QuickStartCommand.__new__(QuickStartCommand) | ||
| command._framework = self.odev | ||
| command.args = Namespace(branch=None, version=None, name="quickstart-target", filestore=False) | ||
|
|
||
| source = MagicMock() | ||
| source.name = "quickstart-source" | ||
| source.repository = repository | ||
| source.platform.name = "remote" | ||
| source._get_dump_filename.return_value = "dump.zip" | ||
| command._database = source | ||
| return command | ||
|
|
||
| def make_dump_file(self) -> Path: | ||
| dump_file = self.run_path / "dump.zip" | ||
| dump_file.parent.mkdir(parents=True, exist_ok=True) | ||
| dump_file.touch() | ||
| return dump_file | ||
|
|
||
| def test_repository_is_linked_before_restore(self): | ||
| """Regression for #98: neutralization runs from within `restore`, so a repository linked | ||
| only after the restore call is invisible to it. | ||
| """ | ||
| repository = Repository("psbe-project", "odoo-ps") | ||
| command = self.make_command(repository) | ||
| target = MagicMock(spec=LocalDatabase) | ||
| target.repository = None | ||
| seen: list[Repository | None] = [] | ||
|
|
||
| def fake_run_command(name, *_args, database=None, **_kwargs): | ||
| if name == "restore": | ||
| seen.append(database.repository) | ||
| return True | ||
|
|
||
| dump_file = self.make_dump_file() | ||
|
|
||
| with ( | ||
| self.patch(self.odev, "run_command", side_effect=fake_run_command), | ||
| self.patch_property(type(self.odev), "dumps_path", dump_file.parent), | ||
| self.patch("odev.commands.database.quickstart", "LocalDatabase", return_value=target), | ||
| ): | ||
| command.run() | ||
|
|
||
| self.assertEqual(seen, [Repository("psbe-project", "odoo-ps")]) | ||
|
|
||
| def test_repository_is_still_linked_after_restore(self): | ||
| """`restore` drops and recreates the database, which clears its entry in the data store.""" | ||
| repository = Repository("psbe-project", "odoo-ps") | ||
| command = self.make_command(repository) | ||
| target = MagicMock(spec=LocalDatabase) | ||
| target.repository = None | ||
|
|
||
| def fake_run_command(name, *_args, database=None, **_kwargs): | ||
| if name == "restore": | ||
| database.repository = None # `restore` wipes the store entry | ||
| return True | ||
|
|
||
| dump_file = self.make_dump_file() | ||
|
|
||
| with ( | ||
| self.patch(self.odev, "run_command", side_effect=fake_run_command), | ||
| self.patch_property(type(self.odev), "dumps_path", dump_file.parent), | ||
| self.patch("odev.commands.database.quickstart", "LocalDatabase", return_value=target), | ||
| ): | ||
| command.run() | ||
|
|
||
| self.assertEqual(target.repository, Repository("psbe-project", "odoo-ps")) | ||
|
|
||
| def test_no_repository_on_source_links_nothing(self): | ||
| command = self.make_command(None) | ||
| target = MagicMock(spec=LocalDatabase) | ||
| target.repository = None | ||
|
|
||
| dump_file = self.make_dump_file() | ||
|
|
||
| with ( | ||
| self.patch(self.odev, "run_command", return_value=True), | ||
| self.patch_property(type(self.odev), "dumps_path", dump_file.parent), | ||
| self.patch("odev.commands.database.quickstart", "LocalDatabase", return_value=target), | ||
| ): | ||
| command.run() | ||
|
|
||
| self.assertIsNone(target.repository) |
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,79 @@ | ||
| from pathlib import Path | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from odev.common.databases import LocalDatabase | ||
| from odev.common.version import OdooVersion | ||
|
|
||
| from tests.fixtures import OdevTestCase | ||
|
|
||
|
|
||
| class TestNeutralizeScripts(OdevTestCase): | ||
| """Neutralization must pick up the `data/neutralize.sql` scripts shipped by custom modules.""" | ||
|
|
||
| def setUp(self): | ||
| super().setUp() | ||
| self.addons_path = self.res_path / "repositories" / "test" / "test-addons" | ||
|
|
||
| def make_database(self, addons_paths: list[Path] | None = None) -> LocalDatabase: | ||
| database = LocalDatabase.__new__(LocalDatabase) | ||
| database._framework = self.odev | ||
| database._process = MagicMock() | ||
| database._process.additional_addons_paths = [self.addons_path] if addons_paths is None else addons_paths | ||
| return database | ||
|
|
||
| def neutralize_scripts( | ||
| self, | ||
| installed_modules: list[str], | ||
| addons_paths: list[Path] | None = None, | ||
| version: str = "18.0", | ||
| ) -> list[Path]: | ||
| database = self.make_database(addons_paths) | ||
|
|
||
| with ( | ||
| self.patch_property(LocalDatabase, "installed_modules", installed_modules), | ||
| self.patch_property(LocalDatabase, "process", database._process), | ||
| self.patch_property(LocalDatabase, "version", OdooVersion(version)), | ||
| ): | ||
| return database._neutralize_scripts() | ||
|
|
||
| def test_custom_module_script_is_collected(self): | ||
| """Regression for #98: the module scripts were filtered against the *addons directory* | ||
| names rather than the module names, so no custom script was ever collected. | ||
| """ | ||
| scripts = self.neutralize_scripts(["base", "addon_01"]) | ||
| self.assertEqual( | ||
| scripts, | ||
| [ | ||
| self.odev.static_path / "neutralize-pre.sql", | ||
| self.addons_path / "addon_01" / "data" / "neutralize.sql", | ||
| self.odev.static_path / "neutralize-post.sql", | ||
| ], | ||
| ) | ||
|
|
||
| def test_module_without_script_is_skipped(self): | ||
| scripts = self.neutralize_scripts(["addon_02"], addons_paths=[self.addons_path / "submodule"]) | ||
| self.assertEqual( | ||
| scripts, | ||
| [ | ||
| self.odev.static_path / "neutralize-pre.sql", | ||
| self.odev.static_path / "neutralize-post.sql", | ||
| ], | ||
| ) | ||
|
|
||
| def test_module_not_installed_is_skipped(self): | ||
| scripts = self.neutralize_scripts(["base"]) | ||
| self.assertNotIn(self.addons_path / "addon_01" / "data" / "neutralize.sql", scripts) | ||
|
|
||
| def test_no_addons_paths_yields_static_scripts_only(self): | ||
| scripts = self.neutralize_scripts(["addon_01"], addons_paths=[]) | ||
| self.assertEqual( | ||
| scripts, | ||
| [ | ||
| self.odev.static_path / "neutralize-pre.sql", | ||
| self.odev.static_path / "neutralize-post.sql", | ||
| ], | ||
| ) | ||
|
|
||
| def test_older_versions_get_the_legacy_script(self): | ||
| scripts = self.neutralize_scripts(["base"], version="14.0") | ||
| self.assertIn(self.odev.static_path / "neutralize-post-before-15.0.sql", scripts) |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
note: real sentinel values was introduced in python 3.15, see: https://peps.python.org/pep-0661/#rationale
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.
Good to know if we ever drop support for the precedent versions later