spec/services/blacklight/search_service_spec.rb in blacklight-7.40.0 vs spec/services/blacklight/search_service_spec.rb in blacklight-8.0.0.beta1
- old
+ new
@@ -6,11 +6,11 @@
# queries to solr such that it gets appropriate results. When a user does a search,
# do we get data back from solr (i.e. did we properly configure blacklight code
# to talk with solr and get results)? when we do a document request, does
# blacklight code get a single document returned?)
#
-RSpec.describe Blacklight::SearchService, :api do
+RSpec.describe Blacklight::SearchService, api: true do
subject { service }
let(:context) { { whatever: :value } }
let(:service) { described_class.new(config: blacklight_config, user_params: user_params, **context) }
let(:repository) { Blacklight::Solr::Repository.new(blacklight_config) }
@@ -47,129 +47,101 @@
end
end
end
# SPECS FOR SEARCH RESULTS FOR QUERY
- describe 'Search Results', :integration do
+ describe 'Search Results', integration: true do
let(:blacklight_config) { copy_of_catalog_config }
describe 'for a sample query returning results' do
let(:user_params) { { q: all_docs_query } }
- before do
- (@solr_response, @document_list) = service.search_results
- end
-
it "uses the configured request handler" do
allow(blacklight_config).to receive(:default_solr_params).and_return(qt: 'custom_request_handler')
allow(blacklight_solr).to receive(:send_and_receive) do |path, params|
expect(path).to eq 'select'
expect(params[:params]['facet.field']).to eq ["format", "{!ex=pub_date_ssim_single}pub_date_ssim", "subject_ssim", "language_ssim", "lc_1letter_ssim", "subject_geo_ssim", "subject_era_ssim"]
expect(params[:params]["facet.query"]).to eq ["pub_date_ssim:[#{5.years.ago.year} TO *]", "pub_date_ssim:[#{10.years.ago.year} TO *]", "pub_date_ssim:[#{25.years.ago.year} TO *]"]
expect(params[:params]).to include('rows' => 10, 'qt' => "custom_request_handler", 'q' => "", "f.subject_ssim.facet.limit" => 21, 'sort' => "score desc, pub_date_si desc, title_si asc")
end.and_return('response' => { 'docs' => [] })
service.search_results
end
-
- it 'has a @response.docs list of the same size as @document_list' do
- expect(@solr_response.docs).to have(@document_list.length).docs
- end
-
- it 'has @response.docs list representing same documents as SolrDocuments in @document_list' do
- @solr_response.docs.each_index do |index|
- mash = @solr_response.docs[index]
- solr_document = @document_list[index]
-
- expect(Set.new(mash.keys)).to eq Set.new(solr_document.keys)
-
- mash.keys.each do |key|
- expect(mash[key]).to eq solr_document[key]
- end
- end
- end
end
describe "for a query returning a grouped response" do
let(:blacklight_config) { copy_of_catalog_config }
let(:user_params) { { q: all_docs_query } }
before do
blacklight_config.default_solr_params[:group] = true
blacklight_config.default_solr_params[:'group.field'] = 'pub_date_si'
- (@solr_response, @document_list) = service.search_results
end
it "returns a grouped response" do
- expect(@document_list).to be_empty
- expect(@solr_response).to be_a Blacklight::Solr::Response::GroupResponse
+ expect(service.search_results).to be_a_kind_of Blacklight::Solr::Response::GroupResponse
end
end
- describe "for a query returning multiple groups", :integration do
+ describe "for a query returning multiple groups", integration: true do
let(:blacklight_config) { copy_of_catalog_config }
let(:user_params) { { q: all_docs_query } }
before do
allow(subject).to receive_messages grouped_key_for_results: 'title_si'
blacklight_config.default_solr_params[:group] = true
blacklight_config.default_solr_params[:'group.field'] = %w[pub_date_si title_si]
- (@solr_response, @document_list) = service.search_results
end
it "returns a grouped response" do
- expect(@document_list).to be_empty
- expect(@solr_response).to be_a Blacklight::Solr::Response::GroupResponse
- expect(@solr_response.group_field).to eq "title_si"
+ solr_response = service.search_results
+ expect(solr_response).to be_a_kind_of Blacklight::Solr::Response::GroupResponse
+ expect(solr_response.group_field).to eq "title_si"
end
end
describe "for All Docs Query and One Facet" do
let(:user_params) { { q: all_docs_query, f: single_facet } }
it 'has results' do
- (solr_response, document_list) = service.search_results
- expect(solr_response.docs).to have(document_list.size).results
+ solr_response = service.search_results
expect(solr_response.docs).to have_at_least(1).result
end
# TODO: check that number of these results < number of results for all docs query
# BUT can't: num docs isn't total, it's the num docs in the single Solr response (e.g. 10)
end
describe "for Query Without Results and No Facet" do
let(:user_params) { { q: no_docs_query } }
it 'has no results and not raise error' do
- (solr_response, document_list) = service.search_results
- expect(document_list).to have(0).results
+ solr_response = service.search_results
expect(solr_response.docs).to have(0).results
end
end
describe "for Query Without Results and One Facet" do
let(:user_params) { { q: no_docs_query, f: single_facet } }
it 'has no results and not raise error' do
- (solr_response, document_list) = service.search_results
- expect(document_list).to have(0).results
+ solr_response = service.search_results
expect(solr_response.docs).to have(0).results
end
end
describe "for All Docs Query and Bad Facet" do
- let(:bad_facet) { { format: '666' } }
+ let(:bad_facet) { { format: ['666'] } }
let(:user_params) { { q: all_docs_query, f: bad_facet } }
it 'has no results and not raise error' do
- (solr_response, document_list) = service.search_results
- expect(document_list).to have(0).results
+ solr_response = service.search_results
expect(solr_response.docs).to have(0).results
end
end
end # Search Results
# SPECS FOR SEARCH RESULTS FOR FACETS
- describe 'Facets in Search Results for All Docs Query', :integration do
+ describe 'Facets in Search Results for All Docs Query', integration: true do
let(:blacklight_config) { copy_of_catalog_config }
let(:user_params) { { q: all_docs_query } }
before do
(solr_response,) = service.search_results
@@ -179,12 +151,12 @@
it 'has more than one facet' do
expect(@facets).to have_at_least(1).facet
end
it 'has all facets specified in initializer' do
- expect(@facets.keys).to include(*blacklight_config.facet_fields.keys)
- expect(@facets.none? { |_k, v| v.nil? }).to eq true
+ expect(@facets.keys).to include *blacklight_config.facet_fields.keys
+ expect(@facets.none? { |_k, v| v.nil? }).to be true
end
it 'has at least one value for each facet' do
@facets.each do |_key, facet|
expect(facet.items).to have_at_least(1).hit
@@ -197,11 +169,11 @@
if facet.items.size > 1
has_mult_values = true
break
end
end
- expect(has_mult_values).to eq true
+ expect(has_mult_values).to be true
end
it 'has all value counts > 0' do
@facets.each do |_key, facet|
facet.items.each do |facet_vals|
@@ -210,43 +182,40 @@
end
end
end # facet specs
# SPECS FOR SEARCH RESULTS FOR PAGING
- describe 'Paging', :integration do
+ describe 'Paging', integration: true do
let(:blacklight_config) { copy_of_catalog_config }
let(:user_params) { { q: all_docs_query } }
it 'starts with first results by default' do
(solr_response,) = service.search_results
expect(solr_response.params[:start].to_i).to eq 0
end
it 'has number of results (per page) set in initializer, by default' do
- (solr_response, document_list) = service.search_results
+ solr_response = service.search_results
expect(solr_response.docs).to have(blacklight_config[:default_solr_params][:rows]).items
- expect(document_list).to have(blacklight_config[:default_solr_params][:rows]).items
end
context "with per page requested" do
let(:user_params) { { q: all_docs_query, per_page: num_results } }
let(:num_results) { 3 } # non-default value
it 'gets number of results per page requested' do
- (solr_response1, document_list1) = service.search_results
- expect(document_list1).to have(num_results).docs
+ solr_response1 = service.search_results
expect(solr_response1.docs).to have(num_results).docs
end
end
context "with rows requested" do
let(:user_params) { { q: all_docs_query, rows: num_results } }
let(:num_results) { 4 } # non-default value
it 'gets number of rows requested' do
- (solr_response1, document_list1) = service.search_results
- expect(document_list1).to have(num_results).docs
+ solr_response1 = service.search_results
expect(solr_response1.docs).to have(num_results).docs
end
end
context "with page requested" do
@@ -275,12 +244,11 @@
let(:page) { 5000 }
let(:rows) { 5000 }
let(:user_params) { { q: all_docs_query, page: page, rows: rows } }
it 'has no results when prompted for page after last result' do
- (solr_response3, document_list3) = service.search_results
- expect(document_list3).to have(0).docs
+ solr_response3 = service.search_results
expect(solr_response3.docs).to have(0).docs
end
end
context "with negative page" do
@@ -299,24 +267,23 @@
let(:page) { 1 }
let(:rows) { 5000 }
let(:user_params) { { q: all_docs_query, page: page, rows: rows } }
it 'has results available when asked for more than are in response' do
- (solr_response5, document_list5) = service.search_results
- expect(solr_response5.docs).to have(document_list5.length).docs
+ solr_response5 = service.search_results
expect(solr_response5.docs).to have_at_least(1).doc
end
end
end # page specs
# SPECS FOR SINGLE DOCUMENT REQUESTS
- describe 'Get Document By Id', :integration do
+ describe 'Get Document By Id', integration: true do
let(:doc_id) { '2007020969' }
let(:bad_id) { 'redrum' }
before do
- @response2, @document = service.fetch(doc_id)
+ @document = service.fetch(doc_id)
end
it "raises Blacklight::RecordNotFound for an unknown id" do
expect do
service.fetch(bad_id)
@@ -325,39 +292,35 @@
it "has a non-nil result for a known id" do
expect(@document).not_to be_nil
end
- it "has a single document in the response for a known id" do
- expect(@response2.docs.size).to eq 1
- end
-
it 'has the expected value in the id field' do
expect(@document.id).to eq doc_id
end
end
- describe 'Get multiple documents By Id', :integration do
+ describe 'Get multiple documents By Id', integration: true do
let(:doc_id) { '2007020969' }
let(:bad_id) { 'redrum' }
- let(:response) { service.fetch([doc_id]).first }
+ let(:response) { service.fetch([doc_id]) }
before do
blacklight_config.fetch_many_document_params[:fl] = 'id,format'
end
it 'has the expected value in the id field' do
- expect(response.documents.first.id).to eq doc_id
+ expect(response.first.id).to eq doc_id
end
it 'returns all the requested fields' do
- expect(response.documents.first['format']).to eq ['Book']
+ expect(response.first['format']).to eq ['Book']
end
end
# SPECS FOR SPELLING SUGGESTIONS VIA SEARCH
- describe "Searches should return spelling suggestions", :integration do
+ describe "Searches should return spelling suggestions", integration: true do
context "for just-poor-enough-query term" do
let(:user_params) { { q: 'boo' } }
it 'has (multiple) spelling suggestions' do
(solr_response,) = service.search_results
@@ -380,29 +343,29 @@
# solr_response.spelling.words).to include('political') # more freq
end
end
context "for title search" do
- let(:user_params) { { q: 'yehudiyam', qt: 'search', "spellcheck.dictionary": "title" } }
+ let(:user_params) { { q: 'yehudiyam', 'spellcheck.dictionary': "title" } }
it 'has spelling suggestions' do
(solr_response,) = service.search_results
expect(solr_response.spelling.words).to include('yehudiyim')
end
end
context "for author search" do
- let(:user_params) { { q: 'shirma', qt: 'search', "spellcheck.dictionary": "author" } }
+ let(:user_params) { { q: 'shirma', 'spellcheck.dictionary': "author" } }
it 'has spelling suggestions' do
(solr_response,) = service.search_results
expect(solr_response.spelling.words).to include('sharma')
end
end
context "for subject search" do
- let(:user_params) { { q: 'wome', qt: 'search', "spellcheck.dictionary": "subject" } }
+ let(:user_params) { { q: 'wome', 'spellcheck.dictionary': "subject" } }
it 'has spelling suggestions' do
(solr_response,) = service.search_results
expect(solr_response.spelling.words).to include('women')
end
@@ -431,30 +394,30 @@
describe "#previous_and_next_documents_for_search" do
let(:user_params) { { q: '', per_page: 100 } }
before do
- @full_response, @all_docs = service.search_results
+ @full_response = service.search_results
end
it "returns the previous and next documents for a search" do
_response, docs = service.previous_and_next_documents_for_search(4, q: '')
- expect(docs.first.id).to eq @all_docs[3].id
- expect(docs.last.id).to eq @all_docs[5].id
+ expect(docs.first.id).to eq @full_response.documents[3].id
+ expect(docs.last.id).to eq @full_response.documents[5].id
end
it "returns only the next document if the counter is 0" do
_response, docs = service.previous_and_next_documents_for_search(0, q: '')
expect(docs.first).to be_nil
- expect(docs.last.id).to eq @all_docs[1].id
+ expect(docs.last.id).to eq @full_response.documents[1].id
end
it "returns only the previous document if the counter is the total number of documents" do
_response, docs = service.previous_and_next_documents_for_search(@full_response.total - 1, q: '')
- expect(docs.first.id).to eq @all_docs.slice(-2).id
+ expect(docs.first.id).to eq @full_response.documents.slice(-2).id
expect(docs.last).to be_nil
end
it "returns an array of nil values if there is only one result" do
_response, docs = service.previous_and_next_documents_for_search(0, q: 'id:2007020969')
@@ -462,18 +425,18 @@
expect(docs.first).to be_nil
end
it 'returns only the unique key by default' do
_response, docs = service.previous_and_next_documents_for_search(0, q: '')
- expect(docs.last.to_h).to eq 'id' => @all_docs[1].id
+ expect(docs.last.to_h).to eq 'id' => @full_response.documents[1].id
end
it 'allows the query parameters to be customized using configuration' do
blacklight_config.document_pagination_params[:fl] = 'id,format'
_response, docs = service.previous_and_next_documents_for_search(0, q: '')
- expect(docs.last.to_h).to eq @all_docs[1].to_h.slice('id', 'format')
+ expect(docs.last.to_h).to eq @full_response.documents[1].to_h.slice('id', 'format')
end
end
describe '#opensearch_response' do
let(:user_params) { { q: 'Book' } }