-
Notifications
You must be signed in to change notification settings - Fork 125
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
Add first/last to read specification #399
Conversation
Getting closer including |
.ordered(direction, stream, offset_entry_id) | ||
.combine(:event) | ||
.map_with(:stream_entry_to_serialized_record, auto_struct: false) | ||
.to_ary.first |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 LIMIT
s to one record. I'm not sure if that means of reversing the query is always accurate. Presumably your tests check for that.
There was a problem hiding this comment.
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
3a18e2d
to
9a87f65
Compare
@mpraglowski worked up a fix to DRY things up. To solve the |
@@ -7,10 +7,16 @@ class Specification | |||
NO_LIMIT = Object.new.freeze | |||
# @private | |||
# @api private | |||
NO_BATCH = Object.new.freeze | |||
FIRST = Object.new.freeze |
There was a problem hiding this comment.
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!
That's just an implementation details that should never leak out of the Specification class. Still used in specification tests however should it also should be avoided.
89a1af2
to
b319a24
Compare
@joelvh I will add that test later today. |
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eq(true)
Method first & last returns nil when no event is found based on given read specification. No error is raised here.
Now it will be possible: