Skip to content

Commit

Permalink
Merge 67c3d41 into 558d424
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Mar 16, 2016
2 parents 558d424 + 67c3d41 commit 092c7f5
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
6 changes: 1 addition & 5 deletions lib/active_fedora/relation/calculations.rb
Expand Up @@ -8,11 +8,7 @@ def count(*args)
opts[:rows] = limit_value if limit_value
opts[:sort] = order_values if order_values

calculate :count, where_values, opts
end

def calculate(_calculation, conditions, _opts = {})
SolrService.query(create_query(conditions), raw: true, rows: 0).fetch('response'.freeze)['numFound'.freeze]
SolrService.count(create_query(where_values))
end
end
end
19 changes: 14 additions & 5 deletions lib/active_fedora/solr_service.rb
Expand Up @@ -105,11 +105,20 @@ def construct_query_for_rel(field_pairs, join_with = 'AND')
SolrQueryBuilder.construct_query_for_rel(field_pairs, join_with)
end

def get(query, args = {})
args = args.merge(q: query, qt: 'standard')
SolrService.instance.conn.get(select_path, params: args)
end

def query(query, args = {})
raw = args.delete(:raw)
args = args.merge(q: query, qt: 'standard')
result = SolrService.instance.conn.get(select_path, params: args)
return result if raw
result = get(query, args)

if raw
Deprecation.warn SolrService, "SolrService.query with raw: true is deprecated. Use SolrService.get instead. This will be removed in active-fedora 10.0"
return result
end

result['response']['docs'].map do |doc|
ActiveFedora::SolrHit.new(doc)
end
Expand All @@ -124,8 +133,8 @@ def delete(id)
# @param [Hash] args arguments to pass through to `args' param of SolrService.query (note that :rows will be overwritten to 0)
# @return [Integer] number of records matching
def count(query, args = {})
args = args.merge(raw: true, rows: 0)
SolrService.query(query, args)['response']['numFound'].to_i
args = args.merge(rows: 0)
SolrService.get(query, args)['response']['numFound'].to_i
end

# @param [Hash] doc the document to index
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/query_spec.rb
Expand Up @@ -177,22 +177,22 @@ class Basic < ActiveFedora::Base
let(:mock_result) { { 'response' => { 'numFound' => 7 } } }

it "returns a count" do
expect(ActiveFedora::SolrService).to receive(:query)
.with(model_query, rows: 0, raw: true)
expect(ActiveFedora::SolrService).to receive(:get)
.with(model_query, rows: 0)
.and_return(mock_result)
expect(SpecModel::Basic.count).to eq 7
end

it "allows conditions" do
expect(ActiveFedora::SolrService).to receive(:query)
.with("#{model_query} AND (foo:bar)", rows: 0, raw: true)
expect(ActiveFedora::SolrService).to receive(:get)
.with("#{model_query} AND (foo:bar)", rows: 0)
.and_return(mock_result)
expect(SpecModel::Basic.count(conditions: 'foo:bar')).to eq 7
end

it "counts without a class specified" do
expect(ActiveFedora::SolrService).to receive(:query)
.with("(foo:bar)", rows: 0, raw: true)
expect(ActiveFedora::SolrService).to receive(:get)
.with("(foo:bar)", rows: 0)
.and_return(mock_result)
expect(described_class.count(conditions: 'foo:bar')).to eq 7
end
Expand Down
22 changes: 19 additions & 3 deletions spec/unit/solr_service_spec.rb
Expand Up @@ -48,23 +48,39 @@
end
end

describe ".query" do
describe "#get" do
it "calls solr" do
mock_conn = double("Connection")
stub_result = double("Result")
expect(mock_conn).to receive(:get).with('select', params: { q: 'querytext', qt: 'standard' }).and_return(stub_result)
allow(described_class).to receive(:instance).and_return(double("instance", conn: mock_conn))
expect(described_class.query('querytext', raw: true)).to eq stub_result
expect(described_class.get('querytext')).to eq stub_result
end
it "uses select_path" do
mock_conn = double("Connection")
stub_result = double("Result")
expect(mock_conn).to receive(:get).with('select_test', params: { q: 'querytext', qt: 'standard' }).and_return(stub_result)
expect(described_class).to receive(:select_path).and_return('select_test')
allow(described_class).to receive(:instance).and_return(double("instance", conn: mock_conn))
expect(described_class.query('querytext', raw: true)).to eq stub_result
expect(described_class.get('querytext')).to eq stub_result
end
end

describe "#query" do
let(:doc) { { 'id' => 'x' } }
let(:docs) { [doc] }

it "wraps the solr response documents in Solr hits" do
mock_conn = double("Connection")
stub_result = { 'response' => { 'docs' => docs } }
expect(mock_conn).to receive(:get).with('select', params: { q: 'querytext', qt: 'standard' }).and_return(stub_result)
allow(described_class).to receive(:instance).and_return(double("instance", conn: mock_conn))
result = described_class.query('querytext')
expect(result.size).to eq 1
expect(result.first.id).to eq 'x'
end
end

describe ".count" do
it "returns a count of matching records" do
mock_conn = double("Connection")
Expand Down

0 comments on commit 092c7f5

Please sign in to comment.