Skip to content

Commit

Permalink
Rename some events
Browse files Browse the repository at this point in the history
  • Loading branch information
christoph-blessing committed Nov 6, 2023
1 parent bd531b9 commit 478f175
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 27 deletions.
4 changes: 2 additions & 2 deletions link/adapters/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ def translate_tainted_primary_keys(primary_keys: Iterable[PrimaryKey]) -> set[Id
tainted_identifiers=translate_tainted_primary_keys(self.facade.get_tainted_primary_keys()),
)

def apply(self, updates: Iterable[events.EntityStateChanged]) -> None:
def apply(self, updates: Iterable[events.StateChanged]) -> None:
"""Apply updates to the persistent data representing the link."""

def keyfunc(update: events.EntityStateChanged) -> int:
def keyfunc(update: events.StateChanged) -> int:
assert update.command is not None
return update.command.value

Expand Down
6 changes: 3 additions & 3 deletions link/domain/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ class Event:


@dataclass(frozen=True)
class EntityOperationApplied(Event):
class OperationApplied(Event):
"""An operation was applied to an entity."""

operation: Operations
identifier: Identifier


@dataclass(frozen=True)
class InvalidOperationRequested(EntityOperationApplied):
class InvalidOperationRequested(OperationApplied):
"""An operation that is invalid given the entities current state was requested."""

state: type[State]


@dataclass(frozen=True)
class EntityStateChanged(EntityOperationApplied):
class StateChanged(OperationApplied):
"""The state of an entity changed during the application of an operation."""

transition: Transition
Expand Down
6 changes: 3 additions & 3 deletions link/domain/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from functools import partial

from .custom_types import Identifier
from .events import EntityOperationApplied, EntityStateChanged, InvalidOperationRequested
from .events import InvalidOperationRequested, OperationApplied, StateChanged


class State:
Expand Down Expand Up @@ -42,7 +42,7 @@ def _transition_entity(
entity.state = transition.new
entity.current_process = new_process
entity.events.append(
EntityStateChanged(operation, entity.identifier, transition, TRANSITION_MAP[transition]),
StateChanged(operation, entity.identifier, transition, TRANSITION_MAP[transition]),
)


Expand Down Expand Up @@ -269,7 +269,7 @@ class Entity:
state: type[State]
current_process: Processes
is_tainted: bool
events: deque[EntityOperationApplied]
events: deque[OperationApplied]

def pull(self) -> None:
"""Pull the entity."""
Expand Down
2 changes: 1 addition & 1 deletion link/infrastructure/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def inner(obj: type) -> Any:
list_idle_entities, uow=uow, output_port=idle_entities_updater
)
event_handlers: EventHandlers = {}
event_handlers[events.EntityStateChanged] = [lambda event: None]
event_handlers[events.StateChanged] = [lambda event: None]
event_handlers[events.InvalidOperationRequested] = [lambda event: None]
bus = MessageBus(uow, command_handlers, event_handlers)
controller = DJController(bus, translator)
Expand Down
2 changes: 1 addition & 1 deletion link/service/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ def create_link(self) -> Link:
"""Create a link from the persistent data."""

@abstractmethod
def apply(self, updates: Iterable[events.EntityStateChanged]) -> None:
def apply(self, updates: Iterable[events.StateChanged]) -> None:
"""Apply updates to the link's persistent data."""
4 changes: 2 additions & 2 deletions link/service/uow.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, gateway: LinkGateway) -> None:
"""Initialize the unit of work."""
self._gateway = gateway
self._link: Link | None = None
self._updates: deque[events.EntityStateChanged] = deque()
self._updates: deque[events.StateChanged] = deque()
self._events: deque[events.Event] = deque()
self._seen: list[Entity] = []

Expand Down Expand Up @@ -57,7 +57,7 @@ def augmented(operation: Operations) -> None:
return
transition = Transition(current_state, new_state)
command = TRANSITION_MAP[transition]
self._updates.append(events.EntityStateChanged(operation, entity.identifier, transition, command))
self._updates.append(events.StateChanged(operation, entity.identifier, transition, command))

return augmented

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
def create_link(self) -> Link:
return create_link(self.assignments, tainted_identifiers=self.tainted_identifiers, processes=self.processes)

def apply(self, updates: Iterable[events.EntityStateChanged]) -> None:
def apply(self, updates: Iterable[events.StateChanged]) -> None:
for update in updates:
if update.command is Commands.START_PULL_PROCESS:
self.processes[Processes.PULL].add(update.identifier)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_datajoint_persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def apply_update(gateway: DJLinkGateway, operation: Operations, requested: Itera
entity.apply(operation)
while entity.events:
event = entity.events.popleft()
if not isinstance(event, events.EntityStateChanged):
if not isinstance(event, events.StateChanged):
continue
gateway.apply([event])

Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_uow.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,37 +128,37 @@ def test_correct_events_are_collected() -> None:
uow.link.delete(create_identifiers("2"))
uow.commit()
expected = [
events.EntityStateChanged(
events.StateChanged(
Operations.START_PULL,
create_identifier("1"),
Transition(states.Idle, states.Activated),
Commands.START_PULL_PROCESS,
),
events.EntityStateChanged(
events.StateChanged(
Operations.PROCESS,
create_identifier("1"),
Transition(states.Activated, states.Received),
Commands.ADD_TO_LOCAL,
),
events.EntityStateChanged(
events.StateChanged(
Operations.PROCESS,
create_identifier("1"),
Transition(states.Received, states.Pulled),
Commands.FINISH_PULL_PROCESS,
),
events.EntityStateChanged(
events.StateChanged(
Operations.START_DELETE,
create_identifier("2"),
Transition(states.Pulled, states.Received),
Commands.START_DELETE_PROCESS,
),
events.EntityStateChanged(
events.StateChanged(
Operations.PROCESS,
create_identifier("2"),
Transition(states.Received, states.Activated),
Commands.REMOVE_FROM_LOCAL,
),
events.EntityStateChanged(
events.StateChanged(
Operations.PROCESS,
create_identifier("2"),
Transition(states.Activated, states.Idle),
Expand Down
12 changes: 5 additions & 7 deletions tests/unit/entities/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_start_pulling_idle_entity_returns_correct_entity() -> None:
assert entity.state is states.Activated
assert entity.current_process is Processes.PULL
assert list(entity.events) == [
events.EntityStateChanged(
events.StateChanged(
Operations.START_PULL,
entity.identifier,
Transition(states.Idle, states.Activated),
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_processing_activated_entity_returns_correct_entity(
)
entity = next(iter(link))
entity.events.append(
events.EntityStateChanged(Operations.PROCESS, entity.identifier, Transition(entity.state, new_state), command),
events.StateChanged(Operations.PROCESS, entity.identifier, Transition(entity.state, new_state), command),
)
entity.apply(Operations.PROCESS)
assert entity.state == new_state
Expand Down Expand Up @@ -124,7 +124,7 @@ def test_processing_received_entity_returns_correct_entity(
)
entity = next(iter(link))
expected_events = [
events.EntityStateChanged(Operations.PROCESS, entity.identifier, Transition(entity.state, new_state), command),
events.StateChanged(Operations.PROCESS, entity.identifier, Transition(entity.state, new_state), command),
]
entity.apply(Operations.PROCESS)
assert entity.state == new_state
Expand All @@ -139,7 +139,7 @@ def test_starting_delete_on_pulled_entity_returns_correct_entity() -> None:
entity = next(iter(link))
transition = Transition(states.Pulled, states.Received)
expected_events = [
events.EntityStateChanged(
events.StateChanged(
Operations.START_DELETE,
entity.identifier,
transition,
Expand All @@ -160,9 +160,7 @@ def test_starting_delete_on_tainted_entity_returns_correct_commands() -> None:
entity = next(iter(link))
transition = Transition(states.Tainted, states.Received)
expected_events = [
events.EntityStateChanged(
Operations.START_DELETE, entity.identifier, transition, Commands.START_DELETE_PROCESS
),
events.StateChanged(Operations.START_DELETE, entity.identifier, transition, Commands.START_DELETE_PROCESS),
]
entity.apply(Operations.START_DELETE)
assert entity.state == transition.new
Expand Down

0 comments on commit 478f175

Please sign in to comment.