diff --git a/features/backup_plan_cli_show.feature b/features/backup_plan_cli_show.feature index f56dfa1..a5d0d5f 100644 --- a/features/backup_plan_cli_show.feature +++ b/features/backup_plan_cli_show.feature @@ -1,18 +1,46 @@ -Feature: Deleting a backup plan with the CLI +Feature: Show a backup plan with the CLI @fixture.injector @fixture.in_memory_database @fixture.seeds @fixture.cli - Scenario Outline: Deleting a backup plan with the CLI + Scenario Outline: Show a backup plan with the CLI Given the CLI arguments are "" And I have a backup plan with id="" When I run the CLI Then the CLI executed with "" Examples: - | arguments | existing_backup_plan_id | result | description | - | plans show one -i 8aa59e7e-dc75-459b-beb5-b710b39be583 | 8aa59e7e-dc75-459b-beb5-b710b39be583 | success | delete an existing plan | - | plans show one -i 8aa59e7e-dc75-459b-aeb5-b710b39be583 | 8aa59e7e-dc75-459b-beb5-b710b39be512 | error | delete a non-existing plan | + | arguments | existing_backup_plan_id | result | description | + | plans show one -i 8aa59e7e-dc75-459b-beb5-b710b39be583 | 8aa59e7e-dc75-459b-beb5-b710b39be583 | success | show an existing plan | + | plans show one -i 8aa59e7e-dc75-459b-aeb5-b710b39be583 | 8aa59e7e-dc75-459b-beb5-b710b39be512 | error | show a non-existing plan | + @fixture.injector + @fixture.in_memory_database + @fixture.cli + Scenario Outline: Show no backup plans with the CLI + Given the CLI arguments are "" + When I run the CLI + Then the CLI executed with "" + And the CLI output contains "" + + Examples: + | arguments | result | result_message | description | + | plans show all | success | No backup plans. | shows no plans | + + @fixture.injector + @fixture.in_memory_database + @fixture.seeds + @fixture.cli + Scenario Outline: Show backup plans with the CLI + Given the CLI arguments are "" + And I have a backup plan with id="8aa59e7e-dc75-459b-beb5-b710b39be583" + When I run the CLI + Then the CLI executed with "" + And the CLI output contains "8aa59e7e-dc75-459b-beb5-b710b39be583" + And the CLI output doesn't contains "" + + Examples: + | arguments | result | no_message | description | + | plans show all | success | No backup plans. | shows plans | diff --git a/features/steps/cli_runner_steps.py b/features/steps/cli_runner_steps.py index 0a82bf5..9410edb 100644 --- a/features/steps/cli_runner_steps.py +++ b/features/steps/cli_runner_steps.py @@ -3,9 +3,7 @@ from behave import given, when, then @given('the CLI arguments are "{arguments}"') def given_cli_arguments(context, arguments): - print(f"{arguments}") context.arguments = arguments.split() - print(f"{context.arguments}") @when('I run the CLI') @@ -27,6 +25,10 @@ def then_cli_output_contains(context, string): assert string in context.cli_result.stdout +@then('the CLI output doesn\'t contains "{string}"') +def then_cli_output_contains(context, string): + assert not (string in context.cli_result.stdout) + @then('the CLI contains the error: "{string}"') def then_cli_output_contains_error(context, string): assert string in context.cli_result.stdout diff --git a/tui_rsync/core/components/backup_plan/application/queries/__init__.py b/tui_rsync/core/components/backup_plan/application/queries/__init__.py index c822363..265b604 100644 --- a/tui_rsync/core/components/backup_plan/application/queries/__init__.py +++ b/tui_rsync/core/components/backup_plan/application/queries/__init__.py @@ -1,4 +1,5 @@ -from .get_all_backup_plans_query import GetAllBackupBackupPlansQuery -from .get_backup_plan_by_id_query import GetBackupBackupPlanByIdQuery +from .get_all_backup_plans_query import GetAllBackupPlansQuery +from .get_backup_plan_by_id_query import GetBackupPlanByIdQuery +from .get_backup_plan_count_query import GetBackupPlanCountQuery -__all__ = ['GetAllBackupBackupPlansQuery', 'GetBackupBackupPlanByIdQuery'] +__all__ = ['GetAllBackupPlansQuery', 'GetBackupPlanByIdQuery', 'GetBackupPlanCountQuery'] diff --git a/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_labels_query.py b/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_labels_query.py new file mode 100644 index 0000000..81412b7 --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_labels_query.py @@ -0,0 +1,12 @@ +from tui_rsync.core.ports.orm import DatabaseManagerPort +from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO + +from tui_rsync.infrastructure.orm.models import BackupPlanModel + + +class GetAllBackupBackupPlansQuery: + def __init__(self, database_manager: DatabaseManagerPort): + self.databaseManager = database_manager + + def execute(self): + return (BackupPlanDTO.to_domain(model) for model in BackupPlanModel.select().iterator()) diff --git a/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_query.py b/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_query.py index 81412b7..c5eafff 100644 --- a/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_query.py +++ b/tui_rsync/core/components/backup_plan/application/queries/get_all_backup_plans_query.py @@ -4,7 +4,7 @@ from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO from tui_rsync.infrastructure.orm.models import BackupPlanModel -class GetAllBackupBackupPlansQuery: +class GetAllBackupPlansQuery: def __init__(self, database_manager: DatabaseManagerPort): self.databaseManager = database_manager diff --git a/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py b/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py index d452a39..466edba 100644 --- a/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py +++ b/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_by_id_query.py @@ -7,7 +7,7 @@ from tui_rsync.infrastructure.orm.dto.dtos import BackupPlanDTO from tui_rsync.infrastructure.orm.models import BackupPlanModel -class GetBackupBackupPlanByIdQuery: +class GetBackupPlanByIdQuery: def __init__(self, database_manager: DatabaseManagerPort): self.databaseManager = database_manager diff --git a/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_count_query.py b/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_count_query.py new file mode 100644 index 0000000..e1d545f --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/queries/get_backup_plan_count_query.py @@ -0,0 +1,11 @@ +from tui_rsync.core.ports.orm import DatabaseManagerPort + +from tui_rsync.infrastructure.orm.models import BackupPlanModel + + +class GetBackupPlanCountQuery: + def __init__(self, database_manager: DatabaseManagerPort): + self.databaseManager = database_manager + + def execute(self): + return BackupPlanModel.select().count() diff --git a/tui_rsync/core/components/backup_plan/application/repository/__init__.py b/tui_rsync/core/components/backup_plan/application/repository/__init__.py index 6b75fe7..964c53b 100644 --- a/tui_rsync/core/components/backup_plan/application/repository/__init__.py +++ b/tui_rsync/core/components/backup_plan/application/repository/__init__.py @@ -1,3 +1,4 @@ from .backup_plan_repository_port import BackupPlanRepositoryPort +from .backup_plan_repository import BackupPlanRepository -__all__ = ['BackupPlanRepositoryPort'] +__all__ = ['BackupPlanRepositoryPort', 'BackupPlanRepository'] diff --git a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py index 7ac54b5..fb80562 100644 --- a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py +++ b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py @@ -9,7 +9,7 @@ from tui_rsync.core.shared_kernel.components.common import UUID from tui_rsync.infrastructure.orm.models import BackupPlanModel from ..commands import RemoveBackupBackupPlanDestinationsCommand, RemoveBackupBackupPlanCommand -from ..queries import GetBackupBackupPlanByIdQuery +from ..queries import GetBackupPlanByIdQuery class BackupPlanRepository(BackupPlanRepositoryPort): @@ -24,7 +24,7 @@ class BackupPlanRepository(BackupPlanRepositoryPort): destination.save(force_insert=True) def get_by_id(self, uuid: UUID) -> BackupPlan: - query = GetBackupBackupPlanByIdQuery(self.databaseManager) + query = GetBackupPlanByIdQuery(self.databaseManager) return query.execute(uuid) def update(self, backup_plan: BackupPlan): diff --git a/tui_rsync/core/components/backup_plan/application/services/__init__.py b/tui_rsync/core/components/backup_plan/application/services/__init__.py index e69de29..66eb856 100644 --- a/tui_rsync/core/components/backup_plan/application/services/__init__.py +++ b/tui_rsync/core/components/backup_plan/application/services/__init__.py @@ -0,0 +1,5 @@ +from .backup_plan_service import BackupPlanService +from .get_all_backup_plans_service import GetAllBackupPlansService +from .get_backup_plan_count_service import GetBackupPlanCountService + +__all__ = ['BackupPlanService', 'GetAllBackupPlansService', 'GetBackupPlanCountService'] diff --git a/tui_rsync/core/components/backup_plan/application/services/get_all_backup_plans_service.py b/tui_rsync/core/components/backup_plan/application/services/get_all_backup_plans_service.py new file mode 100644 index 0000000..e671030 --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/services/get_all_backup_plans_service.py @@ -0,0 +1,13 @@ +from typing import List + +from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupPlansQuery + +from tui_rsync.core.components.backup_plan.domain import BackupPlan + + +class GetAllBackupPlansService: + def __init__(self, get_all_backup_plan_query: GetAllBackupPlansQuery): + self.get_all_backup_plan_query = get_all_backup_plan_query + + def get_all(self) -> List[BackupPlan]: + return self.get_all_backup_plan_query.execute() diff --git a/tui_rsync/core/components/backup_plan/application/services/get_backup_plan_count_service.py b/tui_rsync/core/components/backup_plan/application/services/get_backup_plan_count_service.py new file mode 100644 index 0000000..4b5141c --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/services/get_backup_plan_count_service.py @@ -0,0 +1,16 @@ +from typing import List + +from tui_rsync.core.components.backup_plan.application.queries import GetBackupPlanCountQuery + +from tui_rsync.core.components.backup_plan.domain import BackupPlan + + +class GetBackupPlanCountService: + def __init__(self, get_backup_plan_count_query: GetBackupPlanCountQuery): + self.get_backup_plan_count_query = get_backup_plan_count_query + + def count(self) -> int: + return self.get_backup_plan_count_query.execute() + + def is_empty(self) -> bool: + return self.count() == 0 diff --git a/tui_rsync/infrastructure/configuration/configuration.py b/tui_rsync/infrastructure/configuration/configuration.py index 98f6b9e..2f8f87e 100644 --- a/tui_rsync/infrastructure/configuration/configuration.py +++ b/tui_rsync/infrastructure/configuration/configuration.py @@ -1,9 +1,10 @@ from injector import singleton, Module, provider from tui_rsync.core.components.backup_plan.application.commands import RemoveAllBackupBackupPlansCommand -from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupBackupPlansQuery -from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort -from tui_rsync.core.components.backup_plan.application.repository.backup_plan_repository import BackupPlanRepository +from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupPlansQuery, GetBackupPlanCountQuery +from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort, BackupPlanRepository +from tui_rsync.core.components.backup_plan.application.services import GetBackupPlanCountService, \ + GetAllBackupPlansService from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService from tui_rsync.core.ports.configuration import UserDataPathsPort from tui_rsync.core.ports.orm import DatabaseManagerPort @@ -38,13 +39,28 @@ class Configuration(Module): @provider @singleton - def provide_remove_all_backup_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand: + def provide_remove_all_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand: return RemoveAllBackupBackupPlansCommand(database_manager) @provider @singleton - def provide_get_all_backup_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupBackupPlansQuery: - return GetAllBackupBackupPlansQuery(database_manager) + def provide_get_all_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupPlansQuery: + return GetAllBackupPlansQuery(database_manager) + + @provider + @singleton + def provide_get_backup_plan_count_query(self, database_manager: DatabaseManagerPort) -> GetBackupPlanCountQuery: + return GetBackupPlanCountQuery(database_manager) + + @provider + @singleton + def provide_get_backup_plan_count_service(self, query: GetBackupPlanCountQuery) -> GetBackupPlanCountService: + return GetBackupPlanCountService(query) + + @provider + @singleton + def provide_get_all_backup_plans_service(self, query: GetAllBackupPlansQuery) -> GetAllBackupPlansService: + return GetAllBackupPlansService(query) @provider @singleton diff --git a/tui_rsync/infrastructure/configuration/testing_configuration.py b/tui_rsync/infrastructure/configuration/testing_configuration.py index 6947492..abc7624 100644 --- a/tui_rsync/infrastructure/configuration/testing_configuration.py +++ b/tui_rsync/infrastructure/configuration/testing_configuration.py @@ -1,9 +1,10 @@ from injector import singleton, Module, provider from tui_rsync.core.components.backup_plan.application.commands import RemoveAllBackupBackupPlansCommand -from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupBackupPlansQuery -from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort -from tui_rsync.core.components.backup_plan.application.repository.backup_plan_repository import BackupPlanRepository +from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupPlansQuery, GetBackupPlanCountQuery +from tui_rsync.core.components.backup_plan.application.repository import BackupPlanRepositoryPort, BackupPlanRepository +from tui_rsync.core.components.backup_plan.application.services import GetBackupPlanCountService, \ + GetAllBackupPlansService from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService from tui_rsync.core.ports.configuration import UserDataPathsPort from tui_rsync.core.ports.orm import DatabaseManagerPort @@ -38,13 +39,28 @@ class TestingConfiguration(Module): @provider @singleton - def provide_remove_all_backup_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand: + def provide_remove_all_backup_plans_command(self, database_manager: DatabaseManagerPort) -> RemoveAllBackupBackupPlansCommand: return RemoveAllBackupBackupPlansCommand(database_manager) @provider @singleton - def provide_get_all_backup_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupBackupPlansQuery: - return GetAllBackupBackupPlansQuery(database_manager) + def provide_get_all_backup_plans_query(self, database_manager: DatabaseManagerPort) -> GetAllBackupPlansQuery: + return GetAllBackupPlansQuery(database_manager) + + @provider + @singleton + def provide_get_backup_plan_count_query(self, database_manager: DatabaseManagerPort) -> GetBackupPlanCountQuery: + return GetBackupPlanCountQuery(database_manager) + + @provider + @singleton + def provide_get_backup_plan_count_service(self, query: GetBackupPlanCountQuery) -> GetBackupPlanCountService: + return GetBackupPlanCountService(query) + + @provider + @singleton + def provide_get_all_backup_plans_service(self, query: GetAllBackupPlansQuery) -> GetAllBackupPlansService: + return GetAllBackupPlansService(query) @provider @singleton diff --git a/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py b/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py index e322790..a243330 100644 --- a/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py +++ b/tui_rsync/user_interface/cli/components/backup_plan/show_backup_plan.py @@ -20,7 +20,8 @@ from rich.console import Console import typer -from tui_rsync.core.components.backup_plan.application.queries import GetAllBackupBackupPlansQuery +from tui_rsync.core.components.backup_plan.application.services import GetAllBackupPlansService, \ + GetBackupPlanCountService from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService from tui_rsync.core.shared_kernel.components.common import UUID @@ -48,16 +49,8 @@ def one( service: BackupPlanService = CurrentConfiguration.get(BackupPlanService) - if id is None: - console.print("[red b][ERROR][/] Backup plan does not exists!!!") - return - plan = service.get_by_id(UUID(id)) - if plan is None: - console.print("[red b][ERROR][/] Backup plan does not exists!!!") - return - console.print(BackupPlanFormat.format(plan)) @@ -67,7 +60,10 @@ def all(): [green b]Show[/] [yellow]all existing backup plans[/]. """ - query = CurrentConfiguration.get(GetAllBackupBackupPlansQuery) - - for backup_plan in query.execute(): - console.print(BackupPlanFormat.format(backup_plan)) + count_service = CurrentConfiguration.get(GetBackupPlanCountService) + if count_service.is_empty(): + console.print("No backup plans.") + else: + service = CurrentConfiguration.get(GetAllBackupPlansService) + for backup_plan in service.get_all(): + console.print(BackupPlanFormat.format(backup_plan))