Skip to content

Commit

Permalink
Configure test environment to better reflect reality (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
derekkraan authored Sep 4, 2017
1 parent d5cf362 commit 11bcf9a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
40 changes: 28 additions & 12 deletions lib/sequent/test/command_handler_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'thread_safe'
require 'sequent/core/event_store'

module Sequent
module Test
Expand All @@ -22,9 +23,9 @@ module Test
# describe InvoiceCommandHandler do
#
# before :each do
# @event_store = Sequent::Test::CommandHandlerHelpers::FakeEventStore.new
# @repository = Sequent::Core::AggregateRepository.new(@event_store)
# @command_handler = InvoiceCommandHandler.new(@repository)
# Sequent.configuration.event_store = Sequent::Test::CommandHandlerHelpers::FakeEventStore.new
# Sequent.configuration.command_handlers = [] # add your command handlers here
# Sequent.configuration.event_handlers = [] # add you event handlers (eg, workflows) here
# end
#
# it "marks an invoice as paid" do
Expand All @@ -37,7 +38,12 @@ module Test
module CommandHandlerHelpers

class FakeEventStore
def initialize
extend Forwardable
attr_accessor :configuration
def_delegators :@configuration, :event_handlers

def initialize(configuration = Sequent.configuration)
self.configuration = configuration
@event_streams = {}
@all_events = {}
@stored_events = []
Expand Down Expand Up @@ -73,6 +79,20 @@ def commit_events(_, streams_with_events)
@all_events[event_stream.aggregate_id] += serialized
@stored_events += serialized
end
publish_events(streams_with_events.flat_map { |_, events| events }, event_handlers)
end

def publish_events(events, event_handlers)
return if configuration.disable_event_handlers
event_handlers.each do |handler|
events.each do |event|
begin
handler.handle_message event
rescue
raise Core::EventStore::PublishEventError.new(handler.class, event)
end
end
end
end

def given_events(events)
Expand Down Expand Up @@ -122,22 +142,18 @@ def deserialize_events(events)
end

def given_events *events
@event_store.given_events(events.flatten(1))
Sequent.configuration.event_store.given_events(events.flatten(1))
end

def when_command command
raise "@command_handler is mandatory when using the #{self.class}" unless @command_handler
raise "Command handler #{@command_handler} cannot handle command #{command}, please configure the command type (forgot an include in the command class?)" unless @command_handler.class.handles_message?(command)
@command_handler.handle_message(command)
@repository.commit(command)
@repository.clear
Sequent.configuration.command_service.execute_commands command
end

def then_events(*expected_events)
expected_classes = expected_events.flatten(1).map { |event| event.class == Class ? event : event.class }
expect(@event_store.stored_events.map(&:class)).to eq(expected_classes)
expect(Sequent.configuration.event_store.stored_events.map(&:class)).to eq(expected_classes)

@event_store.stored_events.zip(expected_events.flatten(1)).each_with_index do |(actual, expected), index|
Sequent.configuration.event_store.stored_events.zip(expected_events.flatten(1)).each_with_index do |(actual, expected), index|
next if expected.class == Class
_actual = Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(actual.payload))
_expected = Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(expected.payload))
Expand Down
4 changes: 2 additions & 2 deletions lib/sequent/test/event_handler_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ def execute_commands(*commands)

def then_events(*expected_events)
expected_classes = expected_events.flatten(1).map { |event| event.class == Class ? event : event.class }
expect(@event_store.stored_events.map(&:class)).to eq(expected_classes)
expect(Sequent.configuration.event_store.stored_events.map(&:class)).to eq(expected_classes)

@event_store.stored_events.zip(expected_events.flatten(1)).each do |actual, expected|
Sequent.configuration.event_store.stored_events.zip(expected_events.flatten(1)).each do |actual, expected|
next if expected.class == Class
expect(Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(actual.payload))).to eq(Sequent::Core::Oj.strict_load(Sequent::Core::Oj.dump(expected.payload))) if expected
end
Expand Down

0 comments on commit 11bcf9a

Please sign in to comment.