-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inline #query, #post, and #get into solr_service from AF
refs #3800
- Loading branch information
1 parent
1a4674a
commit 16c6183
Showing
4 changed files
with
138 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class SolrHit < Delegator | ||
def self.for(hit) | ||
return hit if hit.is_a? ActiveFedora::SolrHit | ||
|
||
SolrHit.new(hit) | ||
end | ||
|
||
def __getobj__ | ||
@document # return object we are delegating to, required | ||
end | ||
|
||
alias static_config __getobj__ | ||
|
||
def __setobj__(obj) | ||
@document = obj | ||
end | ||
|
||
attr_reader :document | ||
|
||
def initialize(document) | ||
document = document.with_indifferent_access | ||
super | ||
@document = document | ||
end | ||
|
||
def id | ||
document[Hyrax.config.id_field] | ||
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
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,9 @@ | ||
RSpec.describe SolrHit do | ||
subject(:solr_hit) { described_class.new "id" => "my:_ID1_" } | ||
|
||
describe "#id" do | ||
it "extracts the id from the solr hit" do | ||
expect(solr_hit.id).to eq "my:_ID1_" | ||
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 |
---|---|---|
@@ -1,7 +1,67 @@ | ||
RSpec.describe Hyrax::SolrService do | ||
let(:mock_conn) { instance_double(RSolr::Client) } | ||
|
||
describe '.select_path' do | ||
it 'raises NotImplementedError' do | ||
expect { described_class.select_path }.to raise_error NotImplementedError | ||
end | ||
end | ||
|
||
describe "#get" do | ||
it "calls solr" do | ||
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.get('querytext')).to eq stub_result | ||
end | ||
end | ||
|
||
describe "#post" do | ||
it "calls solr" do | ||
stub_result = double("Result") | ||
expect(mock_conn).to receive(:post).with('select', data: { q: 'querytext', qt: 'standard' }).and_return(stub_result) | ||
allow(described_class).to receive(:instance).and_return(double("instance", conn: mock_conn)) | ||
expect(described_class.post('querytext')).to eq stub_result | ||
end | ||
end | ||
|
||
describe "#query" do | ||
let(:doc) { { 'id' => 'x' } } | ||
let(:docs) { [doc] } | ||
let(:stub_result) { { 'response' => { 'docs' => docs } } } | ||
|
||
before do | ||
allow(described_class).to receive(:instance).and_return(double("instance", conn: mock_conn)) | ||
end | ||
|
||
it "defaults to HTTP GET method" do | ||
expect(mock_conn).to receive(:get).with('select', params: { rows: 2, q: 'querytext', qt: 'standard' }).and_return(stub_result) | ||
described_class.query('querytext', rows: 2) | ||
end | ||
|
||
it "allows callers to specify HTTP POST method" do | ||
expect(mock_conn).to receive(:post).with('select', data: { rows: 2, q: 'querytext', qt: 'standard' }).and_return(stub_result) | ||
described_class.query('querytext', rows: 2, method: :post) | ||
end | ||
|
||
it "raises if method not GET or POST" do | ||
expect(mock_conn).not_to receive(:head).with('select', data: { rows: 2, q: 'querytext', qt: 'standard' }) | ||
expect do | ||
described_class.query('querytext', rows: 2, method: :head) | ||
end.to raise_error(RuntimeError, "Unsupported HTTP method for querying SolrService (:head)") | ||
end | ||
|
||
it "wraps the solr response documents in Solr hits" do | ||
expect(mock_conn).to receive(:get).with('select', params: { rows: 2, q: 'querytext', qt: 'standard' }).and_return(stub_result) | ||
result = described_class.query('querytext', rows: 2) | ||
expect(result.size).to eq 1 | ||
expect(result.first.id).to eq 'x' | ||
end | ||
|
||
it "warns about not passing rows" do | ||
allow(mock_conn).to receive(:get).and_return(stub_result) | ||
expect(Rails.logger).to receive(:warn).with(/^Calling Hyrax::SolrService\.get without passing an explicit value for ':rows' is not recommended/) | ||
described_class.query('querytext') | ||
end | ||
end | ||
end |