Skip to content

Commit 2a58b07

Browse files
committed
fix: resolve code quality issues in CQRS tests
- Remove duplicate test methods - Fix import sorting violations - Remove unused variables - All CQRS tests still pass with clean code quality
1 parent 87f91e3 commit 2a58b07

1 file changed

Lines changed: 15 additions & 107 deletions

File tree

tests/unit/application/test_cqrs_patterns.py

Lines changed: 15 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,10 @@ def test_query_handlers_support_pagination(self):
660660

661661
def test_query_handlers_support_filtering(self):
662662
"""Test that query handlers support filtering with real GetRequestHandler."""
663-
from application.queries.handlers import GetRequestHandler
663+
from unittest.mock import MagicMock, Mock
664+
664665
from application.dto.queries import GetRequestQuery
665-
from unittest.mock import Mock, MagicMock
666+
from application.queries.handlers import GetRequestHandler
666667

667668
# Mock dependencies
668669
mock_uow_factory = Mock()
@@ -702,7 +703,7 @@ def test_query_handlers_support_filtering(self):
702703

703704
# This should work without errors
704705
try:
705-
result = handler.handle(query)
706+
handler.handle(query)
706707
# Should call repository with the filtered request ID
707708
mock_request_repo.find_by_id.assert_called_once()
708709
except Exception:
@@ -712,9 +713,10 @@ def test_query_handlers_support_filtering(self):
712713

713714
def test_query_handlers_support_projections(self):
714715
"""Test that query handlers support data projections with real GetRequestStatusQueryHandler."""
715-
from application.queries.handlers import GetRequestStatusQueryHandler
716+
from unittest.mock import MagicMock, Mock
717+
716718
from application.dto.queries import GetRequestStatusQuery
717-
from unittest.mock import Mock, MagicMock
719+
from application.queries.handlers import GetRequestStatusQueryHandler
718720

719721
# Mock dependencies (no container needed for this handler)
720722
mock_uow_factory = Mock()
@@ -743,7 +745,7 @@ def test_query_handlers_support_projections(self):
743745
mock_request.get_domain_events.return_value = [] # This makes it a domain entity
744746
mock_request_repo.find_by_id.return_value = mock_request
745747

746-
query = GetRequestStatusQuery(request_id="test-request")
748+
GetRequestStatusQuery(request_id="test-request")
747749

748750
try:
749751
# This handler is async, so we test the interface exists
@@ -763,10 +765,11 @@ class TestCQRSIntegration:
763765

764766
def test_cqrs_integrates_with_event_sourcing(self):
765767
"""Test that CQRS integrates with event sourcing using real CommandBus."""
766-
from infrastructure.di.buses import CommandBus
767-
from application.dto.commands import CreateRequestCommand
768768
from unittest.mock import Mock
769769

770+
from application.dto.commands import CreateRequestCommand
771+
from infrastructure.di.buses import CommandBus
772+
770773
# Mock dependencies that CommandBus requires
771774
mock_container = Mock()
772775
mock_logger = Mock()
@@ -795,107 +798,11 @@ def test_cqrs_integrates_with_event_sourcing(self):
795798
# We don't need to actually execute since that would require full DI setup
796799

797800
def test_cqrs_supports_read_models(self):
798-
"""Test that CQRS supports read models using real ListActiveRequestsHandler."""
799-
from application.queries.handlers import ListActiveRequestsHandler
800-
from application.dto.queries import ListActiveRequestsQuery
801-
from unittest.mock import Mock, MagicMock
802-
803-
# Mock dependencies
804-
mock_uow_factory = Mock()
805-
mock_logger = Mock()
806-
mock_error_handler = Mock()
807-
mock_container = Mock()
808-
809-
# Mock container to return cache service
810-
mock_container.get.side_effect = lambda service_type: Mock()
811-
812-
handler = ListActiveRequestsHandler(
813-
uow_factory=mock_uow_factory,
814-
logger=mock_logger,
815-
error_handler=mock_error_handler,
816-
container=mock_container,
817-
)
818-
819-
# Mock UoW and repository with proper context manager
820-
mock_uow = Mock()
821-
mock_request_repo = Mock()
822-
mock_uow.request_repository = mock_request_repo
823-
824-
# Mock the context manager properly
825-
mock_context_manager = MagicMock()
826-
mock_context_manager.__enter__.return_value = mock_uow
827-
mock_context_manager.__exit__.return_value = None
828-
mock_uow_factory.create.return_value = mock_context_manager
829-
830-
# Mock read model data (list of requests)
831-
mock_requests = [Mock(), Mock()]
832-
mock_request_repo.find_active_requests.return_value = mock_requests
833-
834-
query = ListActiveRequestsQuery()
835-
836-
try:
837-
result = handler.handle(query)
838-
# Should return a list (read model), not individual entities
839-
if result is not None:
840-
assert isinstance(result, list), "Should return list read model"
841-
except Exception:
842-
# If it fails due to missing dependencies, that's expected in unit test
843-
# The important thing is the handler exists and supports read models
844-
assert hasattr(handler, "handle"), "Handler should have handle method"
845-
846-
def test_cqrs_handles_eventual_consistency(self):
847-
"""Test that CQRS handles eventual consistency using real command and query handlers."""
848-
from application.commands.request_handlers import CreateMachineRequestHandler
849-
from application.queries.handlers import GetRequestHandler
850-
from application.dto.commands import CreateRequestCommand
851-
from application.dto.queries import GetRequestQuery
801+
"""Test that CQRS supports read models using real QueryBus and query classes."""
852802
from unittest.mock import Mock
853803

854-
# Mock dependencies for command handler
855-
mock_uow_factory = Mock()
856-
mock_logger = Mock()
857-
mock_error_handler = Mock()
858-
mock_container = Mock()
859-
860-
# Mock services that command handler needs
861-
mock_container.get.side_effect = lambda service_type: Mock()
862-
863-
command_handler = CreateMachineRequestHandler(
864-
uow_factory=mock_uow_factory,
865-
logger=mock_logger,
866-
error_handler=mock_error_handler,
867-
container=mock_container,
868-
)
869-
870-
query_handler = GetRequestHandler(
871-
uow_factory=mock_uow_factory,
872-
logger=mock_logger,
873-
error_handler=mock_error_handler,
874-
container=mock_container,
875-
)
876-
877-
# Test that both handlers exist and have handle methods
878-
assert hasattr(command_handler, "handle"), "Command handler should have handle method"
879-
assert hasattr(query_handler, "handle"), "Query handler should have handle method"
880-
881-
# Create real command and query
882-
command = CreateRequestCommand(template_id="test-template", max_number=2, attributes={})
883-
884-
query = GetRequestQuery(request_id="test-request")
885-
886-
# Test that command and query have expected structure
887-
assert hasattr(command, "template_id"), "Command should have template_id"
888-
assert hasattr(query, "request_id"), "Query should have request_id"
889-
890-
# The eventual consistency is handled by the fact that commands and queries
891-
# operate on the same underlying data store through UoW pattern
892-
# We don't need to test actual execution, just that the pattern is in place
893-
894-
def test_cqrs_supports_read_models(self):
895-
"""Test that CQRS supports read models using real QueryBus and query classes."""
896-
from application.dto.queries import ListActiveRequestsQuery, GetRequestQuery
804+
from application.dto.queries import GetRequestQuery, ListActiveRequestsQuery
897805
from infrastructure.di.buses import QueryBus
898-
from unittest.mock import Mock
899806

900807
# Mock dependencies for QueryBus
901808
mock_container = Mock()
@@ -921,10 +828,11 @@ def test_cqrs_supports_read_models(self):
921828

922829
def test_cqrs_handles_eventual_consistency(self):
923830
"""Test that CQRS handles eventual consistency using real command and query DTOs."""
831+
from unittest.mock import Mock
832+
924833
from application.dto.commands import CreateRequestCommand, UpdateRequestStatusCommand
925834
from application.dto.queries import GetRequestQuery, GetRequestStatusQuery
926835
from infrastructure.di.buses import CommandBus, QueryBus
927-
from unittest.mock import Mock
928836

929837
# Mock dependencies for buses
930838
mock_container = Mock()

0 commit comments

Comments
 (0)