From f130ecf07833e18dddad244b0e4a4ab1c274bfbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Taymans?= Date: Mon, 10 Jul 2023 17:08:48 +0200 Subject: [PATCH] [ADD] dumpdb: tests --- .gitlab-ci.yml | 11 --- tests/__init__.py | 13 +-- tests/cli_22_dumpdb_test.py | 155 ++++++++++++++++++++++++++++++++++++ 3 files changed, 163 insertions(+), 16 deletions(-) create mode 100644 tests/cli_22_dumpdb_test.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2fb50b3..c229da9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 diff --git a/tests/__init__.py b/tests/__init__.py index 46945f8..f82cdb7 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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 = [ diff --git a/tests/cli_22_dumpdb_test.py b/tests/cli_22_dumpdb_test.py new file mode 100644 index 0000000..f542bfd --- /dev/null +++ b/tests/cli_22_dumpdb_test.py @@ -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()