[ADD] test for command execute-script-sql

This commit is contained in:
Sylvain LE GAL 2022-05-05 22:07:16 +02:00
parent fb28a547f2
commit e044954432
3 changed files with 74 additions and 8 deletions

View File

@ -40,15 +40,43 @@ def get_postgres_container(ctx):
detach=True,
)
# TODO, improve me.
# Postgres container doesn't seems available immediately.
# check in odoo container, i remember that there is
# some script to do the job
time.sleep(3)
return container
def execute_sql_file(ctx, request, sql_file):
# TODO.
# Note : work on path in a docker context.
# container = get_postgres_container(ctx)
pass
def execute_sql_file(ctx, database, sql_file):
container = get_postgres_container(ctx)
# Recreate relative path to make posible to
# call psql in the container
if str(ctx.obj["env_folder_path"]) not in str(sql_file):
raise Exception(
"The SQL file %s is not in the"
" main folder %s available"
" in the postgres container."
% (sql_file, ctx.obj["env_folder_path"])
)
relative_path = Path(
str(sql_file).replace(str(ctx.obj["env_folder_path"]), ".")
)
container_path = Path("/env/") / relative_path
docker_command = (
"psql" " --username=odoo" " --dbname={database}" " --file {file_path}"
).format(database=database, file_path=container_path)
logger.info(
"Executing the script '%s' in postgres container"
" on database %s" % (relative_path, database)
)
docker_result = container.exec_run(docker_command)
if docker_result.exit_code != 0:
raise Exception(
"The script '%s' failed on database %s. Exit Code : %d"
% (relative_path, database, docker_result.exit_code)
)
def execute_sql_request(ctx, request, database="postgres"):
@ -111,9 +139,9 @@ def ensure_database(ctx, database: str, state="present"):
def execute_sql_files_pre_migration(
ctx, database: str, migration_step: dict, sql_files: list = []
):
ensure_database(ctx, database, state="present")
if not sql_files:
script_folder = get_script_folder(ctx, migration_step)
sql_files = [
script_folder / Path(f)
for f in os.listdir(script_folder)

View File

@ -10,7 +10,7 @@ def test_cli_execute_script_python():
"./tests/extra_script_B/post-migration-custom_test.py"
).absolute()
db_name = "database_test_cli_execute_script"
db_name = "database_test_cli_execute_script_python"
# Install Odoo on V13 with product installed
cli_runner_invoke(

View File

@ -1,2 +1,40 @@
import shutil
from pathlib import Path
from odoo_openupgrade_wizard.tools_postgres import (
ensure_database,
execute_sql_request,
)
from . import build_ctx_from_config_file, cli_runner_invoke
def test_cli_execute_script_sql():
return
output_folder_path = Path("./tests/output_B").absolute()
extra_script_path = Path(
"./tests/extra_script_B/pre-migration-custom_test.sql"
).absolute()
destination_path = output_folder_path / "scripts/step_01__update__13.0"
shutil.copy(extra_script_path, destination_path)
ctx = build_ctx_from_config_file(output_folder_path)
db_name = "database_test_cli_execute_script_sql"
ensure_database(ctx, db_name, state="absent")
ensure_database(ctx, db_name, state="present")
cli_runner_invoke(
[
"--log-level=DEBUG",
"--env-folder=%s" % output_folder_path,
"execute-script-sql",
"--step=1",
"--database=%s" % db_name,
]
)
# Ensure that the request has been done correctlys
request = "SELECT name from city order by id;"
result = execute_sql_request(ctx, request, database=db_name)
assert result == [["Chicago"], ["Cavalaire Sur Mer"]]