|
| 1 | +from dataclasses import dataclass |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from seedwork.application.command_handlers import CommandResult |
| 6 | +from seedwork.application.commands import Command |
| 7 | +from seedwork.application.event_dispatcher import InMemoryEventDispatcher |
| 8 | +from seedwork.application.modules import BusinessModule, UnitOfWork |
| 9 | +from seedwork.application.registry import Registry |
| 10 | +from seedwork.domain.events import DomainEvent |
| 11 | + |
| 12 | +registry = Registry() |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class CompleteOrderCommand(Command): |
| 17 | + order_id: str |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class ProcessPaymentCommand(Command): |
| 22 | + order_id: str |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class ShipOrderCommand(Command): |
| 27 | + order_id: str |
| 28 | + |
| 29 | + |
| 30 | +class OrderCompletedEvent(DomainEvent): |
| 31 | + order_id: str |
| 32 | + |
| 33 | + |
| 34 | +class PaymentProcessedEvent(DomainEvent): |
| 35 | + order_id: str |
| 36 | + |
| 37 | + |
| 38 | +class OrderShippedEvent(DomainEvent): |
| 39 | + order_id: str |
| 40 | + |
| 41 | + |
| 42 | +@registry.command_handler |
| 43 | +def complete_order(command: CompleteOrderCommand, history): |
| 44 | + history.append(f"completing {command.order_id}") |
| 45 | + return CommandResult.success( |
| 46 | + payload=None, event=OrderCompletedEvent(order_id=command.order_id) |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@registry.command_handler |
| 51 | +def process_payment(command: ProcessPaymentCommand, history): |
| 52 | + history.append(f"processing payment for {command.order_id}") |
| 53 | + return CommandResult.success( |
| 54 | + payload=None, event=PaymentProcessedEvent(order_id=command.order_id) |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +@registry.command_handler |
| 59 | +def ship_order(command: ShipOrderCommand, history): |
| 60 | + history.append(f"shipping {command.order_id}") |
| 61 | + return CommandResult.success( |
| 62 | + payload=None, event=OrderShippedEvent(order_id=command.order_id) |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +@registry.domain_event_handler |
| 67 | +def when_order_is_completed_process_payment_policy( |
| 68 | + event: OrderCompletedEvent, module: type[BusinessModule] |
| 69 | +): |
| 70 | + module.uow.history.append( |
| 71 | + f"starting when_order_is_completed_process_payment_policy for {event.order_id}" |
| 72 | + ) |
| 73 | + module.execute_command(ProcessPaymentCommand(order_id=event.order_id)) |
| 74 | + |
| 75 | + |
| 76 | +@registry.domain_event_handler |
| 77 | +def when_order_is_completed_ship_order_policy( |
| 78 | + event: OrderCompletedEvent, module: type[BusinessModule] |
| 79 | +): |
| 80 | + module.uow.history.append( |
| 81 | + f"starting when_order_is_completed_ship_order_policy for {event.order_id}" |
| 82 | + ) |
| 83 | + module.execute_command(ShipOrderCommand(order_id=event.order_id)) |
| 84 | + |
| 85 | + |
| 86 | +@registry.domain_event_handler |
| 87 | +def when_payment_is_processed_open_champagne_policy( |
| 88 | + event: PaymentProcessedEvent, module: type[BusinessModule] |
| 89 | +): |
| 90 | + module.uow.history.append( |
| 91 | + f"starting when_payment_is_processed_open_champagne_policy for {event.order_id}" |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +@registry.domain_event_handler |
| 96 | +def when_order_is_shipped_sit_and_relax_policy( |
| 97 | + event: OrderShippedEvent, module: type[BusinessModule] |
| 98 | +): |
| 99 | + module.uow.history.append( |
| 100 | + f"starting when_order_is_shipped_sit_and_relax_policy for {event.order_id}" |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +@dataclass |
| 105 | +class MonoUnitOfWork(UnitOfWork): |
| 106 | + history: list |
| 107 | + |
| 108 | + |
| 109 | +class MonoModule(BusinessModule): |
| 110 | + registry = registry |
| 111 | + unit_of_work_class = MonoUnitOfWork |
| 112 | + supported_commands = (CompleteOrderCommand, ProcessPaymentCommand, ShipOrderCommand) |
| 113 | + supported_queries = () |
| 114 | + event_handlers = ( |
| 115 | + when_order_is_completed_process_payment_policy, |
| 116 | + when_order_is_completed_ship_order_policy, |
| 117 | + when_payment_is_processed_open_champagne_policy, |
| 118 | + when_order_is_shipped_sit_and_relax_policy, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.integration |
| 123 | +def test_mono_module_command_branching_flow(): |
| 124 | + """This tests the branching code flow: |
| 125 | + CompleteOrderCommand |
| 126 | + ↓ |
| 127 | + OrderCompletedEvent |
| 128 | + ↓ ↓ |
| 129 | + when_order_is_completed_process_payment_policy when_order_is_completed_ship_order_policy |
| 130 | + ↓ ↓ |
| 131 | + ProcessPaymentCommand ShipOrderCommand |
| 132 | + ↓ ↓ |
| 133 | + PaymentProcessedEvent OrderShippedEvent |
| 134 | + ↓ ↓ |
| 135 | + when_payment_is_processed_ship_order_policy when_order_is_shipped_sit_and_relax_policy |
| 136 | + """ |
| 137 | + history = [] |
| 138 | + dispatcher = InMemoryEventDispatcher() |
| 139 | + mono_module = MonoModule(domain_event_dispatcher=dispatcher, history=history) |
| 140 | + |
| 141 | + with mono_module.unit_of_work(): |
| 142 | + mono_module.execute_command(CompleteOrderCommand(order_id="order1")) |
| 143 | + |
| 144 | + assert history == [ |
| 145 | + "completing order1", |
| 146 | + "starting when_order_is_completed_process_payment_policy for order1", |
| 147 | + "processing payment for order1", |
| 148 | + "starting when_payment_is_processed_open_champagne_policy for order1", |
| 149 | + "starting when_order_is_completed_ship_order_policy for order1", |
| 150 | + "shipping order1", |
| 151 | + "starting when_order_is_shipped_sit_and_relax_policy for order1", |
| 152 | + ] |
0 commit comments