diff options
author | Sean McGivern <sean@gitlab.com> | 2019-02-20 11:31:06 +0000 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2019-02-20 11:31:06 +0000 |
commit | 843f4b94b59255ec27b3bdf22e93180fce74ddca (patch) | |
tree | f7ec7659c40eb0ff6ea67a1346a47f7d7d7a4e65 /spec/finders | |
parent | d83e5740e83bbee963279c9af85fb585517cb643 (diff) | |
download | gitlab-ce-843f4b94b59255ec27b3bdf22e93180fce74ddca.tar.gz |
Ignore ordering when calling find_by on finders
We shouldn't care about the ordering here; if we did, it would be more
appropriate to use `take` or `first`. Having the ordering can result in
the database picking a bad query plan, as it might think sorting the
whole table first is the best option.
Diffstat (limited to 'spec/finders')
-rw-r--r-- | spec/finders/concerns/finder_methods_spec.rb | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/spec/finders/concerns/finder_methods_spec.rb b/spec/finders/concerns/finder_methods_spec.rb index a4ad331f613..e074e53c2c5 100644 --- a/spec/finders/concerns/finder_methods_spec.rb +++ b/spec/finders/concerns/finder_methods_spec.rb @@ -12,7 +12,7 @@ describe FinderMethods do end def execute - Project.all + Project.all.order(id: :desc) end end end @@ -38,6 +38,16 @@ describe FinderMethods do it 'raises not found the user does not have access' do expect { finder.find_by!(id: unauthorized_project.id) }.to raise_error(ActiveRecord::RecordNotFound) end + + it 'ignores ordering' do + # Memoise the finder result so we can add message expectations to it + relation = finder.execute + allow(finder).to receive(:execute).and_return(relation) + + expect(relation).to receive(:reorder).with(nil).and_call_original + + finder.find_by!(id: authorized_project.id) + end end describe '#find' do @@ -66,5 +76,15 @@ describe FinderMethods do it 'returns nil when the user does not have access' do expect(finder.find_by(id: unauthorized_project.id)).to be_nil end + + it 'ignores ordering' do + # Memoise the finder result so we can add message expectations to it + relation = finder.execute + allow(finder).to receive(:execute).and_return(relation) + + expect(relation).to receive(:reorder).with(nil).and_call_original + + finder.find_by(id: authorized_project.id) + end end end |