Skip to content

Commit 446e9f2

Browse files
pjurewiczfidel
authored andcommitted
same behavior of adapters when reading from or to non-existent event
Previously, if we specified starting or ending id of non-existing event and try to read events: * with InMemoryRepository we got: NoMethodError: undefined method '+' for nil:NilClass * with ActiveRecord adapter we got: ActiveRecord::RecordNotFound We need these adapters to act in the same manner. My suggestion is to throw EventNotFoundInStream or EventNotFound depending on if the stream was specified.
1 parent 488642f commit 446e9f2

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

ruby_event_store-active_record/lib/ruby_event_store/active_record/event_repository_reader.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ def start_condition(specification)
139139
@stream_klass.find_by!(event_id: specification.start, stream: specification.stream.name),
140140
@stream_klass.table_name
141141
)
142+
rescue ::ActiveRecord::RecordNotFound
143+
raise RubyEventStore::EventNotFoundInStream.new(specification.start)
142144
end
143145

144146
def stop_condition(specification)
@@ -147,6 +149,8 @@ def stop_condition(specification)
147149
@stream_klass.find_by!(event_id: specification.stop, stream: specification.stream.name),
148150
@stream_klass.table_name
149151
)
152+
rescue ::ActiveRecord::RecordNotFound
153+
raise RubyEventStore::EventNotFoundInStream.new(specification.stop)
150154
end
151155

152156
def start_condition_in_global_stream(specification)
@@ -155,6 +159,8 @@ def start_condition_in_global_stream(specification)
155159
@event_klass.find_by!(event_id: specification.start),
156160
@event_klass.table_name
157161
)
162+
rescue ::ActiveRecord::RecordNotFound
163+
raise RubyEventStore::EventNotFound.new(specification.start)
158164
end
159165

160166
def stop_condition_in_global_stream(specification)
@@ -163,6 +169,8 @@ def stop_condition_in_global_stream(specification)
163169
@event_klass.find_by!(event_id: specification.stop),
164170
@event_klass.table_name
165171
)
172+
rescue ::ActiveRecord::RecordNotFound
173+
raise RubyEventStore::EventNotFound.new(specification.stop)
166174
end
167175

168176
def coalesce(*exprs)

ruby_event_store/lib/ruby_event_store/in_memory_repository.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,12 @@ def read_scope(spec)
150150
serialized_records = serialized_records.select { |e| spec.with_types.any? { |x| x.eql?(e.event_type) } } if spec
151151
.with_types?
152152
serialized_records = serialized_records.reverse if spec.backward?
153-
serialized_records = serialized_records.drop(index_of(serialized_records, spec.start) + 1) if spec.start
154-
serialized_records = serialized_records.take(index_of(serialized_records, spec.stop)) if spec.stop
153+
begin
154+
serialized_records = serialized_records.drop(index_of(serialized_records, spec.start) + 1) if spec.start
155+
serialized_records = serialized_records.take(index_of(serialized_records, spec.stop)) if spec.stop
156+
rescue EventNotFound => e
157+
raise spec.stream.global? ? e : EventNotFoundInStream
158+
end
155159
serialized_records = serialized_records.take(spec.limit) if spec.limit?
156160
serialized_records = serialized_records.select { |sr| Time.iso8601(time_comparison_field(spec, sr)) < spec.older_than } if spec
157161
.older_than
@@ -220,7 +224,10 @@ def has_event_in_stream?(event_id, stream_name)
220224
end
221225

222226
def index_of(source, event_id)
223-
source.index { |item| item.event_id.eql?(event_id) }
227+
index = source.index { |item| item.event_id.eql?(event_id) }
228+
raise EventNotFound.new(event_id) unless index
229+
230+
index
224231
end
225232

226233
def compute_position(resolved_version, index)

ruby_event_store/lib/ruby_event_store/spec/event_repository_lint.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,36 @@ def read_events_backward(repository, stream = nil, from: nil, to: nil, count: ni
10551055
expect(repository.read(specification.to(events[4].event_id).backward.read_last.result)).to be_nil
10561056
end
10571057

1058+
specify do
1059+
event = SRecord.new
1060+
repository.append_to_stream([event], Stream.new('dummy'), ExpectedVersion.any)
1061+
expect do
1062+
repository.read(specification.stream('another').from(event.event_id).result).to_a
1063+
end.to raise_error(RubyEventStore::EventNotFoundInStream)
1064+
end
1065+
1066+
specify do
1067+
event = SRecord.new
1068+
repository.append_to_stream([event], Stream.new('dummy'), ExpectedVersion.any)
1069+
expect do
1070+
repository.read(specification.stream('another').to(event.event_id).result).to_a
1071+
end.to raise_error(RubyEventStore::EventNotFoundInStream)
1072+
end
1073+
1074+
specify do
1075+
not_existing_uuid = SecureRandom.uuid
1076+
expect do
1077+
repository.read(specification.from(not_existing_uuid).result).to_a
1078+
end.to raise_error(RubyEventStore::EventNotFound)
1079+
end
1080+
1081+
specify do
1082+
not_existing_uuid = SecureRandom.uuid
1083+
expect do
1084+
repository.read(specification.to(not_existing_uuid).result).to_a
1085+
end.to raise_error(RubyEventStore::EventNotFound)
1086+
end
1087+
10581088
context "#update_messages" do
10591089
specify "changes events" do
10601090
skip unless helper.supports_upsert?

0 commit comments

Comments
 (0)