Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add first/last to read specification #399

Merged
merged 27 commits into from
Jul 30, 2018

Conversation

mpraglowski
Copy link
Member

Now it will be possible:

first_event_in_dummy_stream = event_store.read.stream("Dummy").first
last_event_published = event_store.read.last

@mpraglowski mpraglowski requested a review from joelvh July 18, 2018 13:02
@mpraglowski
Copy link
Member Author

@joelvh please review
d39bbd3

@mostlyobvious
Copy link
Member

mostlyobvious commented Jul 18, 2018

Getting closer including Enumerable 😉

.ordered(direction, stream, offset_entry_id)
.combine(:event)
.map_with(:stream_entry_to_serialized_record, auto_struct: false)
.to_ary.first
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpraglowski I think you don't want .to_ary here -- just first should get you one record

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, first is implemented here

.ordered(direction, stream, offset_entry_id)
.combine(:event)
.map_with(:stream_entry_to_serialized_record, auto_struct: false)
.to_ary.last
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mpraglowski I think you don't want .to_ary here -- just last should get you one record. Internally, it reverses the SQL ORDER and LIMITs to one record. I'm not sure if that means of reversing the query is always accurate. Presumably your tests check for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however here last without to_ary results in failing tests:

     NoMethodError:
       undefined method `last' for #<ROM::Relation::Composite:0x00007fd4226cecf8>
     Shared Example Group: :event_repository called from ./lib/ruby_event_store/spec/rom/event_repository_lint.rb:26

and

     Sequel::DatabaseError:
       SQLite3::SQLException: near "DESC": syntax error
     Shared Example Group: :event_repository called from ./lib/ruby_event_store/spec/rom/event_repository_lint.rb:26

@joelvh
Copy link
Contributor

joelvh commented Jul 20, 2018

@mpraglowski worked up a fix to DRY things up. To solve the last issue, I switch direction and use first. Tests pass. However, can you make sure we have tests that sufficiently test first/last with an offset, to make sure the proper last event is retrieved?

@@ -7,10 +7,16 @@ class Specification
NO_LIMIT = Object.new.freeze
# @private
# @api private
NO_BATCH = Object.new.freeze
FIRST = Object.new.freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use private_const instead!

@mpraglowski
Copy link
Member Author

However, can you make sure we have tests that sufficiently test first/last with an offset, to make sure the proper last event is retrieved?

@joelvh I will add that test later today.

@joelvh
Copy link
Contributor

joelvh commented Jul 23, 2018

thanks @mpraglowski !

Limit is a count value or infinity. Becuase when we do not set count for
specification our limit is infinity. It cound be clearly spotted in
total_limit method of AR eveny repository.
@@ -79,7 +79,7 @@ def match_events?
end

def last_event(spec)
spec.backward.limit(1).each.first
spec.last
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline method maybe?

@@ -76,7 +80,7 @@ def read(spec)
private

def total_limit(specification)
specification.limit? ? specification.count : Float::INFINITY
specification.limit
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would inline this as well

end
alias :in_batches_of :in_batches

def read_first
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc missing

Specification.new(repository, mapper, result.dup.tap { |r| r.read_as = :first })
end

def read_last
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc missing

specify do
repository.append_to_stream([test_record], Stream.new("Dummy"), ExpectedVersion.none)

expect(specification.result.batched?).to be_falsey
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eq(false)

expect(specification.result.last?).to be_falsey

expect(specification.read_first.result.batched?).to be_falsey
expect(specification.read_first.result.first?).to be_truthy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eq(true)

@mpraglowski mpraglowski merged commit 6cc5ad5 into RailsEventStore:master Jul 30, 2018
@mpraglowski mpraglowski deleted the read-spec-last branch July 30, 2018 16:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants