Class: Sequent::Test::CommandHandlerHelpers::FakeEventStore

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/sequent/test/command_handler_helpers.rb

Instance Method Summary collapse

Constructor Details

#initializeFakeEventStore

Returns a new instance of FakeEventStore.



44
45
46
47
48
# File 'lib/sequent/test/command_handler_helpers.rb', line 44

def initialize
  @event_streams = {}
  @all_events = {}
  @stored_events = []
end

Instance Method Details

#commit_events(_, streams_with_events) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/sequent/test/command_handler_helpers.rb', line 72

def commit_events(_, streams_with_events)
  streams_with_events.each do |event_stream, events|
    serialized = serialize_events(events)
    @event_streams[event_stream.aggregate_id] = event_stream
    @all_events[event_stream.aggregate_id] ||= []
    @all_events[event_stream.aggregate_id] += serialized
    @stored_events += serialized
  end
  publish_events(streams_with_events.flat_map { |_, events| events })
end

#events_exists?(aggregate_id) ⇒ Boolean

Returns:



96
97
98
# File 'lib/sequent/test/command_handler_helpers.rb', line 96

def events_exists?(aggregate_id)
  @event_streams[aggregate_id].present?
end

#find_event_stream(aggregate_id) ⇒ Object



64
65
66
# File 'lib/sequent/test/command_handler_helpers.rb', line 64

def find_event_stream(aggregate_id)
  @event_streams[aggregate_id]
end

#given_events(events) ⇒ Object



87
88
89
90
# File 'lib/sequent/test/command_handler_helpers.rb', line 87

def given_events(events)
  commit_events(nil, to_event_streams(events))
  @stored_events = []
end

#load_events(aggregate_id) ⇒ Object



50
51
52
# File 'lib/sequent/test/command_handler_helpers.rb', line 50

def load_events(aggregate_id)
  load_events_for_aggregates([aggregate_id])[0]
end

#load_events_for_aggregates(aggregate_ids) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/sequent/test/command_handler_helpers.rb', line 54

def load_events_for_aggregates(aggregate_ids)
  return [] if aggregate_ids.none?

  aggregate_ids.map do |aggregate_id|
    @event_streams[aggregate_id]
  end.compact.map do |event_stream|
    [event_stream, deserialize_events(@all_events[event_stream.aggregate_id])]
  end
end

#publish_events(events) ⇒ Object



83
84
85
# File 'lib/sequent/test/command_handler_helpers.rb', line 83

def publish_events(events)
  Sequent.configuration.event_publisher.publish_events(events)
end

#stored_eventsObject



68
69
70
# File 'lib/sequent/test/command_handler_helpers.rb', line 68

def stored_events
  deserialize_events(@stored_events)
end

#stream_exists?(aggregate_id) ⇒ Boolean

Returns:



92
93
94
# File 'lib/sequent/test/command_handler_helpers.rb', line 92

def stream_exists?(aggregate_id)
  @event_streams.key?(aggregate_id)
end