From 23cc76f7f07c68ff1e8cfc9f2cfe8954a50d0212 Mon Sep 17 00:00:00 2001 From: Simon Maillard Date: Wed, 21 Aug 2024 16:01:05 +0200 Subject: [PATCH] [IMP] Improve tests around custom migration scripts --- tests/cli_05_execute_script_python_test.py | 6 ++-- tests/cli_07_upgrade_test.py | 34 +++++++++++++++++++ ...st.py => 01-post-migration-custom_test.py} | 6 ++-- .../02-post-migration-custom_test.py | 12 +++++++ 4 files changed, 52 insertions(+), 6 deletions(-) rename tests/data/extra_script/{post-migration-custom_test.py => 01-post-migration-custom_test.py} (54%) create mode 100644 tests/data/extra_script/02-post-migration-custom_test.py diff --git a/tests/cli_05_execute_script_python_test.py b/tests/cli_05_execute_script_python_test.py index b5ceba8..2e35612 100644 --- a/tests/cli_05_execute_script_python_test.py +++ b/tests/cli_05_execute_script_python_test.py @@ -19,11 +19,11 @@ def test_cli_execute_script_python(): ctx = build_ctx_from_config_file() extra_script_path = Path( - "../extra_script/post-migration-custom_test.py" + "../extra_script/01-post-migration-custom_test.py" ).absolute() cp( extra_script_path, - Path("post-migration-custom_test.py"), + Path("01-post-migration-custom_test.py"), ) db_name = "database_test_cli___execute_script_python" @@ -52,7 +52,7 @@ def test_cli_execute_script_python(): "execute-script-python", "--step=1", f"--database={db_name}", - "--script-file-path=post-migration-custom_test.py", + "--script-file-path=01-post-migration-custom_test.py", ], ) partner_quantity_after = int( diff --git a/tests/cli_07_upgrade_test.py b/tests/cli_07_upgrade_test.py index 4d9a724..fd94ce9 100644 --- a/tests/cli_07_upgrade_test.py +++ b/tests/cli_07_upgrade_test.py @@ -1,3 +1,6 @@ +from pathlib import Path +from shutil import copy + from odoo_openupgrade_wizard.tools.tools_postgres import ( ensure_database, execute_sql_request, @@ -14,6 +17,17 @@ def test_cli_upgrade(): move_to_test_folder() ctx = build_ctx_from_config_file() + for n in ["01", "02"]: + copy( + Path( + f"../extra_script/{n}-post-migration-custom_test.py" + ).absolute(), + Path( + "scripts/step_01__regular__14.0/" + f"{n}-post-migration-custom_test.py" + ), + ) + # Initialize database db_name = "database_test_cli___upgrade" ensure_database(ctx, db_name, state="absent") @@ -58,3 +72,23 @@ def test_cli_upgrade(): latest_version = execute_sql_request(ctx, request, database=db_name) assert latest_version[0][0].startswith("15.") + + # ensure the first post-migration-custom scripts have been executed + request = ( + "SELECT name" + " FROM res_partner" + " WHERE name like 'Post Script 1 - Partner #%';" + ) + + result = execute_sql_request(ctx, request, database=db_name) + assert len(result) == 10 + + # ensure the second post-migration-custom scripts have been executed + request = ( + "SELECT name" + " FROM res_partner" + " WHERE name = 'Post Script 2 - Partner #1';" + ) + + result = execute_sql_request(ctx, request, database=db_name) + assert len(result) == 1 diff --git a/tests/data/extra_script/post-migration-custom_test.py b/tests/data/extra_script/01-post-migration-custom_test.py similarity index 54% rename from tests/data/extra_script/post-migration-custom_test.py rename to tests/data/extra_script/01-post-migration-custom_test.py index ba18852..c0112df 100644 --- a/tests/data/extra_script/post-migration-custom_test.py +++ b/tests/data/extra_script/01-post-migration-custom_test.py @@ -1,15 +1,15 @@ import logging _logger = logging.getLogger(__name__) -_logger.info("post-migration-custom_test.py : Begin of script ...") +_logger.info("01-post-migration-custom_test.py : Begin of script ...") env = env # noqa: F821 for i in range(0, 10): - partner_name = "Partner #%d" % (i) + partner_name = "Post Script 1 - Partner #%d" % (i) _logger.info("Create Partner %s" % partner_name) env["res.partner"].create({"name": partner_name}) -_logger.info("post-migration-custom_test.py : End of script.") +_logger.info("01-post-migration-custom_test.py : End of script.") env.cr.commit() diff --git a/tests/data/extra_script/02-post-migration-custom_test.py b/tests/data/extra_script/02-post-migration-custom_test.py new file mode 100644 index 0000000..34c15b0 --- /dev/null +++ b/tests/data/extra_script/02-post-migration-custom_test.py @@ -0,0 +1,12 @@ +import logging + +_logger = logging.getLogger(__name__) +_logger.info("02-post-migration-custom_test.py : Begin of script ...") + +env = env # noqa: F821 + +env["res.partner"].create({"name": "Post Script 2 - Partner #1"}) + +_logger.info("02-post-migration-custom_test.py : End of script.") + +env.cr.commit()