[IMP] add new assert_database test function and harmonize tests for restoredb, copydb, dropdb

This commit is contained in:
Sylvain LE GAL 2024-03-02 23:56:50 +01:00 committed by Rémy Taymans
parent 8016b35624
commit c345d831bc
4 changed files with 25 additions and 9 deletions

View File

@ -7,6 +7,7 @@ from click.testing import CliRunner
from plumbum.cmd import mkdir from plumbum.cmd import mkdir
from odoo_openupgrade_wizard.cli.cli import main from odoo_openupgrade_wizard.cli.cli import main
from odoo_openupgrade_wizard.tools.tools_postgres import execute_sql_request
_logger = logging.getLogger() _logger = logging.getLogger()
@ -91,3 +92,12 @@ def mock_odoo_rpc_url(mocker):
"odoo_openupgrade_wizard.tools.tools_odoo_instance._ODOO_RPC_URL", "odoo_openupgrade_wizard.tools.tools_odoo_instance._ODOO_RPC_URL",
odoo_rpc_url, odoo_rpc_url,
) )
def assert_database(ctx, db_name, state):
request = "select datname FROM pg_database WHERE datistemplate = false;"
results = execute_sql_request(ctx, request)
if state == "present":
assert [db_name] in results
else:
assert [db_name] not in results

View File

@ -4,6 +4,7 @@ import shutil
from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database
from . import ( from . import (
assert_database,
build_ctx_from_config_file, build_ctx_from_config_file,
cli_runner_invoke, cli_runner_invoke,
mock_odoo_rpc_url, mock_odoo_rpc_url,
@ -45,5 +46,9 @@ def test_cli_restoredb(mocker):
# check filestore exists # check filestore exists
assert dest_filestore_path.exists() assert dest_filestore_path.exists()
# Delete filestore # Check database exists
assert_database(ctx, db_name, "present")
# Delete filestore and database
shutil.rmtree(dest_filestore_path) shutil.rmtree(dest_filestore_path)
ensure_database(ctx, db_name, state="absent")

View File

@ -4,6 +4,7 @@ import shutil
from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database
from . import ( from . import (
assert_database,
build_ctx_from_config_file, build_ctx_from_config_file,
cli_runner_invoke, cli_runner_invoke,
mock_odoo_rpc_url, mock_odoo_rpc_url,
@ -46,5 +47,9 @@ def test_cli_copydb(mocker):
# check filestore exists # check filestore exists
assert dest_filestore_path.exists() assert dest_filestore_path.exists()
# Delete filestore # Check database exists
assert_database(ctx, db_dest_name, "present")
# Delete filestore and database
shutil.rmtree(dest_filestore_path) shutil.rmtree(dest_filestore_path)
ensure_database(ctx, db_dest_name, state="absent")

View File

@ -1,12 +1,10 @@
import pathlib import pathlib
import shutil import shutil
from odoo_openupgrade_wizard.tools.tools_postgres import ( from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database
ensure_database,
execute_sql_request,
)
from . import ( from . import (
assert_database,
build_ctx_from_config_file, build_ctx_from_config_file,
cli_runner_invoke, cli_runner_invoke,
mock_odoo_rpc_url, mock_odoo_rpc_url,
@ -45,9 +43,7 @@ def test_cli_dropdb(mocker):
) )
# Check database does not exists # Check database does not exists
request = "select datname FROM pg_database WHERE datistemplate = false;" assert_database(ctx, db_name, "absent")
results = execute_sql_request(ctx, request)
assert [db_name] not in results
# Check filestore does not exists # Check filestore does not exists
assert not filestore_path.exists() assert not filestore_path.exists()