[ADD] dumpdb: tests

This commit is contained in:
Rémy Taymans 2023-07-10 17:08:48 +02:00
parent e9e02d450d
commit f130ecf078
3 changed files with 163 additions and 16 deletions

View File

@ -31,18 +31,7 @@ pytest:
- echo $PATH
- echo $PYTHONPATH
- poetry run pytest --version
- poetry run pytest --verbosity=2 --exitfirst --cov odoo_openupgrade_wizard
tests/cli_01_init_test.py
tests/cli_02_get_code_test.py
tests/cli_03_docker_build_test.py
tests/cli_04_run_test.py
tests/cli_05_execute_script_python_test.py
tests/cli_06_execute_script_sql_test.py
tests/cli_07_upgrade_test.py
tests/cli_08_estimate_workload_test.py
tests/cli_20_install_from_csv_test.py
tests/cli_21_generate_module_analysis_test.py
build:
stage: build

View File

@ -28,17 +28,20 @@ def move_to_test_folder():
os.chdir(test_folder_path)
def cli_runner_invoke(cmd):
def cli_runner_invoke(cmd, expect_success=True):
try:
result = CliRunner().invoke(
main,
cmd,
catch_exceptions=False,
)
if not result.exit_code == 0:
_logger.error("exit_code: %s" % result.exit_code)
_logger.error("output: %s" % result.output)
assert result.exit_code == 0
if expect_success:
if not result.exit_code == 0:
_logger.error("exit_code: %s" % result.exit_code)
_logger.error("output: %s" % result.output)
assert result.exit_code == 0
else:
assert result.exit_code != 0
except Exception as exception:
if Path("log").exists():
log_files = [

155
tests/cli_22_dumpdb_test.py Normal file
View File

@ -0,0 +1,155 @@
import pathlib
import shutil
from odoo_openupgrade_wizard.tools.tools_postgres import ensure_database
from . import (
build_ctx_from_config_file,
cli_runner_invoke,
move_to_test_folder,
)
def test_cli_dumpdb():
move_to_test_folder()
# Initialize database
db_name = "database_test_cli___dumpdb"
ctx = build_ctx_from_config_file()
ensure_database(ctx, db_name, state="absent")
cli_runner_invoke(
[
"--log-level=DEBUG",
"install-from-csv",
f"--database={db_name}",
],
)
# Dump database and filestore
formatlist = [("p", "d"), ("c", "tgz"), ("t", "t"), ("d", "d")]
for formats in formatlist:
database_path = pathlib.Path("database_test_cli___dumpdb")
filestore_path = pathlib.Path("database_test_clie___dumpdb.filestore")
assert not database_path.exists()
assert not filestore_path.exists()
cli_runner_invoke(
[
"--log-level=DEBUG",
"dumpdb",
f"--database={db_name}",
f"--database-path={database_path}",
f"--database-format={formats[0]}",
f"--filestore-path={filestore_path}",
f"--filestore-format={formats[1]}",
],
)
assert database_path.exists()
assert filestore_path.exists()
# Cleanup files
if database_path.is_dir():
shutil.rmtree(database_path)
else:
database_path.unlink()
if filestore_path.is_dir():
shutil.rmtree(filestore_path)
else:
filestore_path.unlink()
def test_cli_dumpdb_failure():
move_to_test_folder()
# Initialize database
db_name = "database_test_cli___dumpdb"
ctx = build_ctx_from_config_file()
ensure_database(ctx, db_name, state="absent")
cli_runner_invoke(
[
"--log-level=DEBUG",
"install-from-csv",
f"--database={db_name}",
],
)
# First dump
formats = ("d", "d")
database_path = pathlib.Path("database_test_cli___dumpdb")
filestore_path = pathlib.Path("database_test_clie___dumpdb.filestore")
assert not database_path.exists()
assert not filestore_path.exists()
cli_runner_invoke(
[
"--log-level=DEBUG",
"dumpdb",
f"--database={db_name}",
f"--database-path={database_path}",
f"--database-format={formats[0]}",
f"--filestore-path={filestore_path}",
f"--filestore-format={formats[1]}",
],
)
assert database_path.exists()
assert filestore_path.exists()
# With same name
cli_runner_invoke(
[
"--log-level=DEBUG",
"dumpdb",
f"--database={db_name}",
f"--database-path={database_path}",
f"--database-format={formats[0]}",
f"--filestore-path={filestore_path}",
f"--filestore-format={formats[1]}",
],
expect_success=False,
)
# With --force
cli_runner_invoke(
[
"--log-level=DEBUG",
"dumpdb",
f"--database={db_name}",
f"--database-path={database_path}",
f"--database-format={formats[0]}",
f"--filestore-path={filestore_path}",
f"--filestore-format={formats[1]}",
"--force",
],
)
# With name outside of project path
cli_runner_invoke(
[
"--log-level=DEBUG",
"dumpdb",
f"--database={db_name}",
f"--database-path=/{database_path}",
f"--database-format={formats[0]}",
f"--filestore-path=/{filestore_path}",
f"--filestore-format={formats[1]}",
],
expect_success=False,
)
# Cleanup files
if database_path.is_dir():
shutil.rmtree(database_path)
else:
database_path.unlink()
if filestore_path.is_dir():
shutil.rmtree(filestore_path)
else:
filestore_path.unlink()