Skip to content

Commit

Permalink
Raise error when trying to append with :any to stream with specific p…
Browse files Browse the repository at this point in the history
…ositions

Issue: #420
  • Loading branch information
swistak35 committed Jul 26, 2021
1 parent 5a67cc2 commit fa3f731
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ruby_event_store/lib/ruby_event_store/in_memory_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'ostruct'
module RubyEventStore
class InMemoryRepository
UnsupportedVersionAnyUsage = Class.new(StandardError)

class EventInStream
def initialize(event_id, position)
@event_id = event_id
Expand Down Expand Up @@ -34,6 +36,7 @@ def append_to_stream(records, stream, expected_version)
serialized_records = records.map { |record| record.serialize(serializer) }

with_synchronize(expected_version, stream) do |resolved_version|
raise UnsupportedVersionAnyUsage if resolved_version.nil? && !streams.fetch(stream.name, Array.new).map(&:position).compact.empty?
raise WrongExpectedEventVersion unless resolved_version.nil? || last_stream_version(stream).equal?(resolved_version)

serialized_records.each_with_index do |serialized_record, index|
Expand Down
12 changes: 12 additions & 0 deletions ruby_event_store/spec/in_memory_repository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,17 @@ module RubyEventStore

expect(repository.global_position(eid2)).to eq(2)
end

it 'publishing with any position to stream with specific position raise an error' do
repository.append_to_stream([
event0 = SRecord.new,
], stream, version_auto)

expect do
repository.append_to_stream([
event1 = SRecord.new,
], stream, version_any)
end.to raise_error(RubyEventStore::InMemoryRepository::UnsupportedVersionAnyUsage)
end
end
end

0 comments on commit fa3f731

Please sign in to comment.