Module: Yes::Core::TestSupport::EventHelpers
- Defined in:
- lib/yes/core/test_support/event_helpers.rb
Overview
Helpers for working with PgEventstore events in tests.
Instance Method Summary collapse
-
#append_and_reload_event(stream, event) ⇒ Yes::Core::Event
Appends an event to a stream and reloads it.
-
#append_event(stream, event) ⇒ void
Appends an event to a stream.
-
#event_instance(event_attrs) ⇒ Yes::Core::Event
Builds a Yes::Core::Event from a hash of event attributes.
-
#event_stream(event_attrs) ⇒ PgEventstore::Stream
Builds a PgEventstore::Stream from event attributes.
-
#event_type(event_attrs) ⇒ String
Constructs an event type string from event attributes.
-
#given_events { ... } ⇒ void
Creates events from a block and appends them to the eventstore.
-
#safe_read(stream) ⇒ Array<Yes::Core::Event>
(also: #read_events)
Reads eventstore and returns events from the stream or an empty array if stream does not exist.
Instance Method Details
#append_and_reload_event(stream, event) ⇒ Yes::Core::Event
Appends an event to a stream and reloads it
27 28 29 30 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 27 def append_and_reload_event(stream, event) append_event(stream, event) PgEventstore.client.read(stream, options: { max_count: 1, direction: :desc }).first end |
#append_event(stream, event) ⇒ void
This method returns an undefined value.
Appends an event to a stream
18 19 20 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 18 def append_event(stream, event) PgEventstore.client.append_to_stream(stream, event) end |
#event_instance(event_attrs) ⇒ Yes::Core::Event
Builds a Yes::Core::Event from a hash of event attributes
67 68 69 70 71 72 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 67 def event_instance(event_attrs) Yes::Core::Event.new( type: event_type(event_attrs), data: (event_attrs[:data] || {}).with_indifferent_access ) end |
#event_stream(event_attrs) ⇒ PgEventstore::Stream
Builds a PgEventstore::Stream from event attributes
78 79 80 81 82 83 84 85 86 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 78 def event_stream(event_attrs) return event_attrs[:stream] if event_attrs[:stream] PgEventstore::Stream.new( context: event_attrs[:context], stream_name: event_attrs[:aggregate], stream_id: event_attrs[:data].first.last ) end |
#event_type(event_attrs) ⇒ String
Constructs an event type string from event attributes
92 93 94 95 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 92 def event_type(event_attrs) aggregate_or_subject = event_attrs[:aggregate] || event_attrs[:subject] "#{event_attrs[:context]}::#{aggregate_or_subject}#{event_attrs[:event]}" end |
#given_events { ... } ⇒ void
This method returns an undefined value.
Creates events from a block and appends them to the eventstore
54 55 56 57 58 59 60 61 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 54 def given_events(&) events_data = yield events_data.each do |event_data| event = event_instance(event_data) stream = event_stream(event_data) append_event(stream, event) end end |
#safe_read(stream) ⇒ Array<Yes::Core::Event> Also known as: read_events
Reads eventstore and returns events from the stream or an empty array if stream does not exist
36 37 38 39 40 |
# File 'lib/yes/core/test_support/event_helpers.rb', line 36 def safe_read(stream) PgEventstore.client.read(stream) rescue PgEventstore::StreamNotFoundError [] end |