-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Query#exists? method that bases on :count search_type
- Loading branch information
Sergey Kintsel
committed
May 17, 2016
1 parent
01baeb4
commit f62c584
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Chewy | ||
class Query | ||
module Existance | ||
# Returns true if there are at least one document that matches the query | ||
# | ||
# PlacesIndex.query(...).filter(...).exists? | ||
# | ||
def exists? | ||
search_type(:count).total > 0 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'spec_helper' | ||
|
||
describe Chewy::Query::Existance do | ||
before { Chewy.massacre } | ||
|
||
before do | ||
stub_index(:products) do | ||
define_type(:product) do | ||
field :name | ||
field :age, type: 'integer' | ||
end | ||
end | ||
end | ||
|
||
let(:search) { ProductsIndex.all } | ||
|
||
specify { expect(search.exists?).to eq false } | ||
|
||
context do | ||
let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } } | ||
|
||
before { ProductsIndex::Product.import!(data.map { |h| double(h) }) } | ||
|
||
describe '#count' do | ||
specify { expect(search.exists?).to eq true } | ||
specify { expect(search.limit(5).exists?).to eq true } | ||
specify { expect(search.filter(range: {age: {gt: 20}}).limit(3).exists?).to eq true } | ||
specify { expect(search.filter(range: {age: {lt: 0}}).exists?).to eq false } | ||
end | ||
end | ||
end |