From 0ced1f547735cae5187d0abcfaa7a43f03d38101 Mon Sep 17 00:00:00 2001 From: rafsaf Date: Fri, 21 Jul 2023 02:29:47 +0200 Subject: [PATCH] Refactor config file with better class target model names --- backuper/config.py | 56 ++++++++++++-------------- backuper/main.py | 47 ++++++++++----------- tests/conftest.py | 42 +++++++++---------- tests/test_backup_target_mariadb.py | 6 +-- tests/test_backup_target_mysql.py | 6 +-- tests/test_backup_target_postgresql.py | 6 +-- tests/test_base_backup_target.py | 4 +- tests/test_config.py | 30 +++++++------- tests/test_main.py | 20 ++++----- 9 files changed, 107 insertions(+), 110 deletions(-) diff --git a/backuper/config.py b/backuper/config.py index dec257b..453f6cf 100644 --- a/backuper/config.py +++ b/backuper/config.py @@ -4,7 +4,7 @@ import re from enum import StrEnum from pathlib import Path -from typing import Any, TypeVar +from typing import Any, TypeVar, Self from croniter import croniter from pydantic import BaseModel, SecretStr, field_validator, model_validator @@ -95,6 +95,8 @@ def logging_config(log_level: str) -> None: log = logging.getLogger(__name__) +log.info("Start configuring backuper") + class BackupProviderEnum(StrEnum): LOCAL_FILES = "local" @@ -124,7 +126,7 @@ class BackupTargetEnum(StrEnum): DISCORD_FAIL_WEBHOOK_URL: str = os.environ.get("DISCORD_FAIL_WEBHOOK_URL", "") -class BackupTarget(BaseModel): +class TargetModel(BaseModel): env_name: str type: BackupTargetEnum cron_rule: str @@ -148,7 +150,7 @@ def env_name_is_valid(cls, env_name: str) -> str: return env_name -class PostgreSQLBackupTarget(BackupTarget): +class PostgreSQLTargetModel(TargetModel): user: str = "postgres" host: str = "localhost" port: int = 5432 @@ -157,7 +159,7 @@ class PostgreSQLBackupTarget(BackupTarget): type: BackupTargetEnum = BackupTargetEnum.POSTGRESQL -class MySQLBackupTarget(BackupTarget): +class MySQLTargetModel(TargetModel): user: str = "root" host: str = "localhost" port: int = 3306 @@ -166,7 +168,7 @@ class MySQLBackupTarget(BackupTarget): type: BackupTargetEnum = BackupTargetEnum.MYSQL -class MariaDBBackupTarget(BackupTarget): +class MariaDBTargetModel(TargetModel): user: str = "root" host: str = "localhost" port: int = 3306 @@ -175,12 +177,12 @@ class MariaDBBackupTarget(BackupTarget): type: BackupTargetEnum = BackupTargetEnum.MARIADB -class FileBackupTarget(BackupTarget): +class FileTargetModel(TargetModel): abs_path: Path type: BackupTargetEnum = BackupTargetEnum.FILE @model_validator(mode="after") # type: ignore [arg-type] - def abs_path_is_valid(self) -> "FileBackupTarget": + def abs_path_is_valid(self) -> Self: if not self.abs_path.is_file() or not self.abs_path.exists(): raise ValueError( f"Path {self.abs_path} is not a file or does not exist\n " @@ -189,12 +191,12 @@ def abs_path_is_valid(self) -> "FileBackupTarget": return self -class FolderBackupTarget(BackupTarget): +class FolderTargetModel(TargetModel): abs_path: Path type: BackupTargetEnum = BackupTargetEnum.FOLDER @model_validator(mode="after") # type: ignore [arg-type] - def abs_path_is_valid(self) -> "FolderBackupTarget": + def abs_path_is_valid(self) -> Self: if not self.abs_path.is_dir() or not self.abs_path.exists(): raise ValueError( f"Path {self.abs_path} is not a dir or does not exist\n " @@ -203,10 +205,10 @@ def abs_path_is_valid(self) -> "FolderBackupTarget": return self -_BT = TypeVar("_BT", bound=BackupTarget) +_BT = TypeVar("_BT", bound=TargetModel) -def _validate_backup_target(env_name: str, env_value: str, target: type[_BT]) -> _BT: +def _validate_target_model(env_name: str, env_value: str, target: type[_BT]) -> _BT: target_type: str = target.__name__.lower() log.info("validating %s variable: `%s`", target_type, env_name) log.debug("%s=%s", target_type, env_value) @@ -232,46 +234,41 @@ def _validate_backup_target(env_name: str, env_value: str, target: type[_BT]) -> return validated_target -_targets_lst = list[ - PostgreSQLBackupTarget - | MySQLBackupTarget - | FileBackupTarget - | FolderBackupTarget - | MariaDBBackupTarget +_target_models_lst = list[ + PostgreSQLTargetModel + | MySQLTargetModel + | FileTargetModel + | FolderTargetModel + | MariaDBTargetModel ] -def create_backup_targets() -> _targets_lst: - targets: _targets_lst = [] +def create_target_models() -> _target_models_lst: + targets: _target_models_lst = [] for env_name, env_value in os.environ.items(): env_name = env_name.lower() if env_name.startswith(BackupTargetEnum.POSTGRESQL): targets.append( - _validate_backup_target(env_name, env_value, PostgreSQLBackupTarget) + _validate_target_model(env_name, env_value, PostgreSQLTargetModel) ) elif env_name.startswith(BackupTargetEnum.MYSQL): targets.append( - _validate_backup_target(env_name, env_value, MySQLBackupTarget) + _validate_target_model(env_name, env_value, MySQLTargetModel) ) elif env_name.startswith(BackupTargetEnum.FILE): - targets.append( - _validate_backup_target(env_name, env_value, FileBackupTarget) - ) + targets.append(_validate_target_model(env_name, env_value, FileTargetModel)) elif env_name.startswith(BackupTargetEnum.FOLDER): targets.append( - _validate_backup_target(env_name, env_value, FolderBackupTarget) + _validate_target_model(env_name, env_value, FolderTargetModel) ) elif env_name.startswith(BackupTargetEnum.MARIADB): targets.append( - _validate_backup_target(env_name, env_value, MariaDBBackupTarget) + _validate_target_model(env_name, env_value, MariaDBTargetModel) ) return targets -BACKUP_TARGETS = create_backup_targets() - - def runtime_configuration() -> None: allowed_logs_levels = ["DEBUG", "INFO", "WARNING", "ERROR"] if LOG_LEVEL not in allowed_logs_levels: @@ -299,7 +296,6 @@ def runtime_configuration() -> None: raise RuntimeError( f"For provider: `{BACKUP_PROVIDER}` you must use environment variable `GOOGLE_BUCKET_UPLOAD_PATH`" ) - log.error(GOOGLE_BUCKET_UPLOAD_PATH) if ZIP_ARCHIVE_PASSWORD and not CONST_ZIP_BIN_7ZZ_PATH.exists(): raise RuntimeError( f"`{ZIP_ARCHIVE_PASSWORD}` is set but `{CONST_ZIP_BIN_7ZZ_PATH}` binary does not exists, did you forget to create it?" diff --git a/backuper/main.py b/backuper/main.py index 1dec3cd..0928385 100644 --- a/backuper/main.py +++ b/backuper/main.py @@ -43,57 +43,58 @@ def backup_provider() -> BaseBackupProvider: def backup_targets() -> list[BaseBackupTarget]: targets: list[BaseBackupTarget] = [] - if not config.BACKUP_TARGETS: + models = config.create_target_models() + if not models: raise RuntimeError("Found 0 backup targets, at least 1 is required.") - for target in config.BACKUP_TARGETS: - if target.type == config.BackupTargetEnum.POSTGRESQL: + for target_model in models: + if target_model.type == config.BackupTargetEnum.POSTGRESQL: log.info( "initializing postgres target, connecting to database: `%s`", - target.env_name, + target_model.env_name, ) - pg_target = PostgreSQL(**target.model_dump()) + pg_target = PostgreSQL(**target_model.model_dump()) targets.append(pg_target) log.info( "success initializing postgres target db version is %s: `%s`", pg_target.db_version, - target.env_name, + target_model.env_name, ) - elif target.type == config.BackupTargetEnum.FILE: - log.info("initializing file target: `%s`", target.env_name) - targets.append(File(**target.model_dump())) - log.info("success initializing file target: `%s`", target.env_name) - elif target.type == config.BackupTargetEnum.FOLDER: - log.info("initializing folder target: `%s`", target.env_name) - targets.append(Folder(**target.model_dump())) - log.info("success initializing folder target: `%s`", target.env_name) - elif target.type == config.BackupTargetEnum.MYSQL: + elif target_model.type == config.BackupTargetEnum.FILE: + log.info("initializing file target: `%s`", target_model.env_name) + targets.append(File(**target_model.model_dump())) + log.info("success initializing file target: `%s`", target_model.env_name) + elif target_model.type == config.BackupTargetEnum.FOLDER: + log.info("initializing folder target: `%s`", target_model.env_name) + targets.append(Folder(**target_model.model_dump())) + log.info("success initializing folder target: `%s`", target_model.env_name) + elif target_model.type == config.BackupTargetEnum.MYSQL: log.info( "initializing mysql target, connecting to database: `%s`", - target.env_name, + target_model.env_name, ) - mysql_target = MySQL(**target.model_dump()) + mysql_target = MySQL(**target_model.model_dump()) targets.append(mysql_target) log.info( "success initializing mysql target db version is %s: `%s`", mysql_target.db_version, - target.env_name, + target_model.env_name, ) - elif target.type == config.BackupTargetEnum.MARIADB: + elif target_model.type == config.BackupTargetEnum.MARIADB: log.info( "initializing mariadb target, connecting to database: `%s`", - target.env_name, + target_model.env_name, ) - maria_target = MariaDB(**target.model_dump()) + maria_target = MariaDB(**target_model.model_dump()) targets.append(maria_target) log.info( "success initializing mariadb target db version is %s: `%s`", maria_target.db_version, - target.env_name, + target_model.env_name, ) else: # pragma: no cover raise RuntimeError( "panic!!! unsupported backup target", - target.model_dump(), + target_model.model_dump(), ) return targets diff --git a/tests/conftest.py b/tests/conftest.py index 961f71b..5fde356 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,28 +10,28 @@ from backuper import config from backuper.config import ( BackupTargetEnum, - FileBackupTarget, - FolderBackupTarget, - MariaDBBackupTarget, - MySQLBackupTarget, - PostgreSQLBackupTarget, + FileTargetModel, + FolderTargetModel, + MariaDBTargetModel, + MySQLTargetModel, + PostgreSQLTargetModel, ) DOCKER_TESTS: bool = os.environ.get("DOCKER_TESTS", None) is not None CONST_TOKEN_URLSAFE = "mock" -FILE_1 = FileBackupTarget( +FILE_1 = FileTargetModel( env_name="singlefile_1", cron_rule="* * * * *", type=BackupTargetEnum.FILE, abs_path=Path(__file__).absolute().parent / "const/testfile.txt", ) -FOLDER_1 = FolderBackupTarget( +FOLDER_1 = FolderTargetModel( env_name="directory_1", cron_rule="* * * * *", type=BackupTargetEnum.FOLDER, abs_path=Path(__file__).absolute().parent / "const/testfolder", ) -POSTGRES_15 = PostgreSQLBackupTarget( +POSTGRES_15 = PostgreSQLTargetModel( env_name="postgresql_db_15", type=BackupTargetEnum.POSTGRESQL, cron_rule="* * * * *", @@ -41,7 +41,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -POSTGRES_14 = PostgreSQLBackupTarget( +POSTGRES_14 = PostgreSQLTargetModel( env_name="postgresql_db_14", type=BackupTargetEnum.POSTGRESQL, cron_rule="* * * * *", @@ -51,7 +51,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -POSTGRES_13 = PostgreSQLBackupTarget( +POSTGRES_13 = PostgreSQLTargetModel( env_name="postgresql_db_13", type=BackupTargetEnum.POSTGRESQL, cron_rule="* * * * *", @@ -61,7 +61,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -POSTGRES_12 = PostgreSQLBackupTarget( +POSTGRES_12 = PostgreSQLTargetModel( env_name="postgresql_db_12", type=BackupTargetEnum.POSTGRESQL, cron_rule="* * * * *", @@ -71,7 +71,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -POSTGRES_11 = PostgreSQLBackupTarget( +POSTGRES_11 = PostgreSQLTargetModel( env_name="postgresql_db_11", type=BackupTargetEnum.POSTGRESQL, cron_rule="* * * * *", @@ -81,7 +81,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -MYSQL_57 = MySQLBackupTarget( +MYSQL_57 = MySQLTargetModel( env_name="mysql_db_57", type=BackupTargetEnum.MYSQL, cron_rule="* * * * *", @@ -91,7 +91,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -MYSQL_80 = MySQLBackupTarget( +MYSQL_80 = MySQLTargetModel( env_name="mysql_db_80", type=BackupTargetEnum.MYSQL, cron_rule="* * * * *", @@ -101,7 +101,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -MARIADB_1011 = MariaDBBackupTarget( +MARIADB_1011 = MariaDBTargetModel( env_name="mariadb_1011", type=BackupTargetEnum.MARIADB, cron_rule="* * * * *", @@ -111,7 +111,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -MARIADB_1006 = MariaDBBackupTarget( +MARIADB_1006 = MariaDBTargetModel( env_name="mariadb_1006", type=BackupTargetEnum.MARIADB, cron_rule="* * * * *", @@ -121,7 +121,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -MARIADB_1005 = MariaDBBackupTarget( +MARIADB_1005 = MariaDBTargetModel( env_name="mariadb_1005", type=BackupTargetEnum.MARIADB, cron_rule="* * * * *", @@ -131,7 +131,7 @@ db="database-_-12!@#$%^&*()/;><.,]}{[", user="user-_-12!@#$%^&*()/;><.,]}{[", ) -MARIADB_1004 = MariaDBBackupTarget( +MARIADB_1004 = MariaDBTargetModel( env_name="mariadb_1004", type=BackupTargetEnum.MARIADB, cron_rule="* * * * *", @@ -155,18 +155,18 @@ "mariadb_1005": "10.5.19", "mariadb_1004": "10.4.28", } -ALL_POSTGRES_DBS_TARGETS: list[PostgreSQLBackupTarget] = [ +ALL_POSTGRES_DBS_TARGETS: list[PostgreSQLTargetModel] = [ POSTGRES_11, POSTGRES_12, POSTGRES_13, POSTGRES_14, POSTGRES_15, ] -ALL_MYSQL_DBS_TARGETS: list[MySQLBackupTarget] = [ +ALL_MYSQL_DBS_TARGETS: list[MySQLTargetModel] = [ MYSQL_57, MYSQL_80, ] -ALL_MARIADB_DBS_TARGETS: list[MariaDBBackupTarget] = [ +ALL_MARIADB_DBS_TARGETS: list[MariaDBTargetModel] = [ MARIADB_1011, MARIADB_1006, MARIADB_1005, diff --git a/tests/test_backup_target_mariadb.py b/tests/test_backup_target_mariadb.py index a26b8ae..dcf7bdd 100644 --- a/tests/test_backup_target_mariadb.py +++ b/tests/test_backup_target_mariadb.py @@ -14,14 +14,14 @@ @pytest.mark.parametrize("mariadb_target", ALL_MARIADB_DBS_TARGETS) -def test_mariadb_connection_success(mariadb_target: config.MariaDBBackupTarget) -> None: +def test_mariadb_connection_success(mariadb_target: config.MariaDBTargetModel) -> None: db = MariaDB(**mariadb_target.model_dump()) assert db.db_version == DB_VERSION_BY_ENV_VAR[mariadb_target.env_name] @pytest.mark.parametrize("mariadb_target", ALL_MARIADB_DBS_TARGETS) def test_mariadb_connection_fail( - mariadb_target: config.MariaDBBackupTarget, + mariadb_target: config.MariaDBTargetModel, monkeypatch: pytest.MonkeyPatch, ) -> None: with pytest.raises(SystemExit) as system_exit: @@ -35,7 +35,7 @@ def test_mariadb_connection_fail( @freeze_time("2022-12-11") @pytest.mark.parametrize("mariadb_target", ALL_MARIADB_DBS_TARGETS) def test_run_mariadb_dump( - mariadb_target: config.MariaDBBackupTarget, + mariadb_target: config.MariaDBTargetModel, monkeypatch: pytest.MonkeyPatch, ) -> None: mock = Mock(return_value="fixed_dbname") diff --git a/tests/test_backup_target_mysql.py b/tests/test_backup_target_mysql.py index d4c74b6..dede3a6 100644 --- a/tests/test_backup_target_mysql.py +++ b/tests/test_backup_target_mysql.py @@ -10,14 +10,14 @@ @pytest.mark.parametrize("mysql_target", ALL_MYSQL_DBS_TARGETS) -def test_mysql_connection_success(mysql_target: config.MySQLBackupTarget) -> None: +def test_mysql_connection_success(mysql_target: config.MySQLTargetModel) -> None: db = MySQL(**mysql_target.model_dump()) assert db.db_version == DB_VERSION_BY_ENV_VAR[mysql_target.env_name] @pytest.mark.parametrize("mysql_target", ALL_MYSQL_DBS_TARGETS) def test_mysql_connection_fail( - mysql_target: config.MySQLBackupTarget, + mysql_target: config.MySQLTargetModel, monkeypatch: pytest.MonkeyPatch, ) -> None: with pytest.raises(SystemExit) as system_exit: @@ -31,7 +31,7 @@ def test_mysql_connection_fail( @freeze_time("2022-12-11") @pytest.mark.parametrize("mysql_target", ALL_MYSQL_DBS_TARGETS) def test_run_mysqldump( - mysql_target: config.MySQLBackupTarget, + mysql_target: config.MySQLTargetModel, monkeypatch: pytest.MonkeyPatch, ) -> None: mock = Mock(return_value="fixed_dbname") diff --git a/tests/test_backup_target_postgresql.py b/tests/test_backup_target_postgresql.py index 6c96a6e..58f7822 100644 --- a/tests/test_backup_target_postgresql.py +++ b/tests/test_backup_target_postgresql.py @@ -15,7 +15,7 @@ @pytest.mark.parametrize("postgres_target", ALL_POSTGRES_DBS_TARGETS) def test_postgres_connection_success( - postgres_target: config.PostgreSQLBackupTarget, + postgres_target: config.PostgreSQLTargetModel, ) -> None: db = PostgreSQL(**postgres_target.model_dump()) assert db.db_version == DB_VERSION_BY_ENV_VAR[postgres_target.env_name] @@ -23,7 +23,7 @@ def test_postgres_connection_success( @pytest.mark.parametrize("postgres_target", ALL_POSTGRES_DBS_TARGETS) def test_postgres_connection_fail( - postgres_target: config.PostgreSQLBackupTarget, + postgres_target: config.PostgreSQLTargetModel, monkeypatch: pytest.MonkeyPatch, ) -> None: with pytest.raises(SystemExit) as system_exit: @@ -37,7 +37,7 @@ def test_postgres_connection_fail( @freeze_time("2022-12-11") @pytest.mark.parametrize("postgres_target", ALL_POSTGRES_DBS_TARGETS) def test_run_pg_dump( - postgres_target: config.PostgreSQLBackupTarget, + postgres_target: config.PostgreSQLTargetModel, monkeypatch: pytest.MonkeyPatch, ) -> None: mock = Mock(return_value="fixed_dbname") diff --git a/tests/test_base_backup_target.py b/tests/test_base_backup_target.py index 5db6f0f..6f364f1 100644 --- a/tests/test_base_backup_target.py +++ b/tests/test_base_backup_target.py @@ -6,14 +6,14 @@ from backuper.backup_targets.base_target import BaseBackupTarget -class BackupTarget(BaseBackupTarget): +class TargetModel(BaseBackupTarget): def _backup(self) -> Path: return Path(__file__) @freeze_time("2023-05-03 17:58") def test_base_backup_target_next_backup() -> None: - target = BackupTarget(cron_rule="* * * * *", env_name="env") + target = TargetModel(cron_rule="* * * * *", env_name="env") assert target.cron_rule == "* * * * *" assert target.env_name == "env" assert target.last_backup_time == datetime(2023, 5, 3, 17, 58) diff --git a/tests/test_config.py b/tests/test_config.py index ea3d7aa..a9cc8a2 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -13,37 +13,37 @@ "target_cls,target_params,valid", [ ( - config.BackupTarget, + config.TargetModel, {"env_name": "VALID", "type": "postgresql", "cron_rule": "* * * * *"}, True, ), ( - config.BackupTarget, + config.TargetModel, {"env_name": "VALID", "type": "mysql", "cron_rule": "* * * * *"}, True, ), ( - config.BackupTarget, + config.TargetModel, {"env_name": ":(()", "type": "postgresql", "cron_rule": "* * * * *"}, False, ), ( - config.BackupTarget, + config.TargetModel, {"env_name": "valid", "type": "postgresql", "cron_rule": "* * ** *"}, False, ), ( - config.BackupTarget, + config.TargetModel, {"env_name": "valid", "type": "postgresql", "cron_rule": "!"}, False, ), ( - config.PostgreSQLBackupTarget, + config.PostgreSQLTargetModel, {"password": "secret", "env_name": "valid", "cron_rule": "5 5 * * *"}, True, ), ( - config.MySQLBackupTarget, + config.MySQLTargetModel, { "password": "secret", "db": "xxx", @@ -53,7 +53,7 @@ True, ), ( - config.MariaDBBackupTarget, + config.MariaDBTargetModel, { "password": "secret", "db": "xxx", @@ -63,7 +63,7 @@ True, ), ( - config.FileBackupTarget, + config.FileTargetModel, { "abs_path": Path("/tmp/asdasd/not_existing_asd.txt"), "env_name": "valid", @@ -72,12 +72,12 @@ False, ), ( - config.FileBackupTarget, + config.FileTargetModel, {"abs_path": Path(__file__), "env_name": "valid", "cron_rule": "5 5 * * *"}, True, ), ( - config.FolderBackupTarget, + config.FolderTargetModel, { "abs_path": Path("/tmp/asdasd/not_existing_asd/folder"), "env_name": "valid", @@ -86,7 +86,7 @@ False, ), ( - config.FolderBackupTarget, + config.FolderTargetModel, { "abs_path": Path(__file__).parent, "env_name": "valid", @@ -97,7 +97,7 @@ ], ) def test_backup_targets( - target_cls: type[config.BackupTarget], + target_cls: type[config.TargetModel], target_params: dict[str, Any], valid: bool, ) -> None: @@ -222,10 +222,10 @@ def test_create_backup_targets( items_mock = Mock(return_value=env_lst) monkeypatch.setattr(os.environ, "items", items_mock) if valid: - assert config.create_backup_targets() + assert config.create_target_models() else: with pytest.raises(Exception): - config.create_backup_targets() + config.create_target_models() def test_runtime_configuration_invalid_log_level( diff --git a/tests/test_main.py b/tests/test_main.py index 17101e0..4bdd6b8 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -20,8 +20,8 @@ def mock_google_storage_client(monkeypatch: pytest.MonkeyPatch) -> None: def test_backup_targets(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr( config, - "BACKUP_TARGETS", - [POSTGRES_15, MYSQL_80, MARIADB_1011, FILE_1, FOLDER_1], + "create_target_models", + Mock(return_value=[POSTGRES_15, MYSQL_80, MARIADB_1011, FILE_1, FOLDER_1]), ) targets = main.backup_targets() assert len(targets) == 5 @@ -32,8 +32,8 @@ def test_empty_backup_targets_raise_runtime_error( ) -> None: monkeypatch.setattr( config, - "BACKUP_TARGETS", - [], + "create_target_models", + Mock(return_value=[]), ) with pytest.raises(RuntimeError): main.backup_targets() @@ -91,8 +91,8 @@ def test_main(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(config, "BACKUP_PROVIDER", "local") monkeypatch.setattr( config, - "BACKUP_TARGETS", - [POSTGRES_15, MYSQL_80, MARIADB_1011, FILE_1, FOLDER_1], + "create_target_models", + Mock(return_value=[POSTGRES_15, MYSQL_80, MARIADB_1011, FILE_1, FOLDER_1]), ) with pytest.raises(SystemExit) as system_exit: main.main() @@ -119,8 +119,8 @@ def test_run_backup_fail_message_when_no_backup_file( ) -> None: monkeypatch.setattr( config, - "BACKUP_TARGETS", - [POSTGRES_15], + "create_target_models", + Mock(return_value=[POSTGRES_15]), ) target = main.backup_targets()[0] monkeypatch.setattr(target, "_backup", Mock(side_effect=ValueError())) @@ -142,8 +142,8 @@ def test_run_backup_fail_message_when_upload_fail( ) -> None: monkeypatch.setattr( config, - "BACKUP_TARGETS", - [POSTGRES_15], + "create_target_models", + Mock(return_value=[POSTGRES_15]), ) target = main.backup_targets()[0] backup_file = Path("/tmp/fake")