Skip to content

Commit

Permalink
Resolve mutation edge case
Browse files Browse the repository at this point in the history
* the guard clause is relevant for older AS versions, not the one we
  mutate with

* mutant exclusion syntax still does not allow to target this particular
  guard close

* introducing private method to cover its body while ignoring the caller
  with guard close completely

  Co-authored-by: Szymon Fiedler <[email protected]>
  • Loading branch information
mostlyobvious committed Jun 12, 2024
1 parent 317ea8b commit f2e4173
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ruby_event_store-active_record/.mutant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ matcher:
- RubyEventStore::ActiveRecord::EventRepository#update_messages
- RubyEventStore::ActiveRecord::EventRepository#upsert_hash
- RubyEventStore::ActiveRecord::EventRepository#add_to_stream
- RubyEventStore::ActiveRecord::EventRepository#link_to_stream
- RubyEventStore::ActiveRecord::EventRepository#append_to_stream
- RubyEventStore::ActiveRecord::WithAbstractBaseClass#build_event_klass
- RubyEventStore::ActiveRecord::WithAbstractBaseClass#build_stream_klass
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,13 @@ def initialize(model_factory: WithDefaultModels.new, serializer:)
def append_to_stream(records, stream, expected_version)
return if records.empty?

hashes = []
event_ids = []
records.each do |record|
hashes << insert_hash(record, record.serialize(serializer))
event_ids << record.event_id
end
add_to_stream(event_ids, stream, expected_version) { @event_klass.insert_all!(hashes) }
append_to_stream_(records, stream, expected_version)
end

def link_to_stream(event_ids, stream, expected_version)
return if event_ids.empty?

(event_ids - @event_klass.where(event_id: event_ids).pluck(:event_id)).each do |id|
raise EventNotFound.new(id)
end
add_to_stream(event_ids, stream, expected_version)
link_to_stream_(event_ids, stream, expected_version)
end

def delete_stream(stream)
Expand Down Expand Up @@ -167,6 +158,25 @@ def optimize_timestamp(valid_at, created_at)
def start_transaction(&block)
@event_klass.transaction(requires_new: true, &block)
end

def link_to_stream_(event_ids, stream, expected_version)
(
event_ids - @event_klass.where(event_id: event_ids).pluck(:event_id)
).each { |id| raise EventNotFound.new(id) }
add_to_stream(event_ids, stream, expected_version)
end

def append_to_stream_(records, stream, expected_version)
hashes = []
event_ids = []
records.each do |record|
hashes << insert_hash(record, record.serialize(serializer))
event_ids << record.event_id
end
add_to_stream(event_ids, stream, expected_version) do
@event_klass.insert_all!(hashes)
end
end
end
end
end

0 comments on commit f2e4173

Please sign in to comment.