Skip to content

Commit

Permalink
Add tests for Blacklight::SearchBuilder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 18, 2015
1 parent e0c9d69 commit 698774d
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 13 deletions.
4 changes: 2 additions & 2 deletions lib/blacklight/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def configure
# Returns default search field, used for simpler display in history, etc.
# if not set, defaults to first defined search field
def default_search_field
field = nil
field = self[:default_search_field]
field ||= search_fields.values.select { |field| field.default == true }.first
field ||= search_fields.values.first

Expand All @@ -242,7 +242,7 @@ def default_search_field
# Returns default sort field, used for simpler display in history, etc.
# if not set, defaults to first defined sort field
def default_sort_field
field = nil
field = self[:default_sort_field]
field ||= sort_fields.values.select { |field| field.default == true }.first
field ||= sort_fields.values.first

Expand Down
33 changes: 26 additions & 7 deletions lib/blacklight/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@ class SearchBuilder
extend Deprecation
self.deprecation_horizon = "blacklight 6.0"

attr_reader :blacklight_params
attr_reader :processor_chain, :blacklight_params

# @param [List<Symbol>] processor_chain a list of filter methods to run
# @param [Object] scope the scope where the filter methods reside in.
def initialize(processor_chain, scope)
@processor_chain = processor_chain
@scope = scope
@blacklight_params = {}
end

def with blacklight_params
##
# Set the parameters to pass through the processor chain
def with blacklight_params = {}
@blacklight_params = blacklight_params
self
end

##
# Append additional processor chain directives
def append *addl_processor_chain
self.class.new(processor_chain + addl_processor_chain, scope).with(blacklight_params)
end

# a solr query method
# @param [Hash,HashWithIndifferentAccess] extra_controller_params (nil) extra parameters to add to the search
# @return [Blacklight::SolrResponse] the solr response object
Expand Down Expand Up @@ -58,18 +67,23 @@ def blacklight_config

protected
def page
blacklight_params[:page].to_i unless blacklight_params[:page].blank?
if blacklight_params[:page].blank?
1
else
blacklight_params[:page].to_i
end
end

def rows default = nil
# default number of rows
rows = default
rows ||= blacklight_config.default_per_page
rows ||= 10

# user-provided parameters should override any default row
rows = blacklight_params[:rows].to_i unless blacklight_params[:rows].blank?
rows = blacklight_params[:per_page].to_i unless blacklight_params[:per_page].blank?

default ||= blacklight_config.default_per_page
default ||= 10
rows ||= default

# ensure we don't excede the max page size
rows = blacklight_config.max_per_page if rows.to_i > blacklight_config.max_per_page

Expand Down Expand Up @@ -100,5 +114,10 @@ def should_add_field_to_request? field_name, field
field.include_in_request || (field.include_in_request.nil? && blacklight_config.add_field_configuration_to_solr_request)
end

private
def scope
@scope
end

end
end
4 changes: 1 addition & 3 deletions lib/blacklight/solr/search_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,9 @@ def add_solr_fields_to_query solr_parameters
def add_paging_to_solr(solr_params)
# user-provided parameters should override any default row
solr_params[:rows] = rows(solr_params[:rows])
unless page.blank?
if page > 1
solr_params[:start] = solr_params[:rows].to_i * (page - 1)
solr_params[:start] = 0 if solr_params[:start].to_i < 0
end

end

###
Expand Down
131 changes: 130 additions & 1 deletion spec/lib/blacklight/search_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,134 @@
require 'spec_helper'

describe Blacklight::SearchBuilder do
let(:processor_chain) { [] }
let(:blacklight_config) { Blacklight::Configuration.new }
let(:scope) { double blacklight_config: blacklight_config }
subject { described_class.new processor_chain, scope }
describe "#with" do
it "should set the blacklight params" do
params = {}
subject.with(params)
expect(subject.blacklight_params).to eq params
end
end

end
describe "#processor_chain" do
let(:processor_chain) { [:a, :b, :c] }
it "should be mutable" do
subject.processor_chain.insert(-1, :d)
expect(subject.processor_chain).to match_array [:a, :b, :c, :d]
end
end

describe "#append" do
let(:processor_chain) { [:a, :b, :c] }
it "should provide a new search builder with the processor chain" do
builder = subject.append(:d, :e)
expect(subject.processor_chain).to eq processor_chain
expect(builder.processor_chain).not_to eq subject.processor_chain
expect(builder.processor_chain).to match_array [:a, :b, :c, :d, :e]
end
end

describe "#query" do
it "should append the extra parameters to the result" do
actual = subject.query({a: 1})
expect(actual).to include a: 1
end
end

describe "#processed_parameters" do
let(:processor_chain) { [:step_1] }
it "should try to run the processor method on the provided scope" do
allow(scope).to receive(:respond_to?).and_return(true)
allow(scope).to receive(:step_1) do |req_params, user_params|
req_params[:step_1] = 'scope'
end

Deprecation.silence(Blacklight::SearchBuilder) do
expect(subject.processed_parameters).to include step_1: 'scope'
end
end

it "should try to run the processor method on the search builder" do
allow(subject).to receive(:step_1) do |req_params, user_params|
req_params[:step_1] = 'builder'
end

expect(subject.processed_parameters).to include step_1: 'builder'
end
end

describe "#blacklight_config" do
it "should get the blacklight_config from the scope" do
expect(subject.blacklight_config).to eq scope.blacklight_config
end
end

describe "#page" do
it "should be the current user parameter page number" do
expect(subject.with(page: 2).send(:page)).to eq 2
end

it "should be page 1 if not page number given" do
expect(subject.send(:page)).to eq 1
end

it "should coerce parameters to integers" do
expect(subject.with(page: '2b').send(:page)).to eq 2
end
end

describe "#rows" do
it "should be the per_page parameter" do
expect(subject.with(per_page: 5).send(:rows)).to eq 5
end

it "should support the legacy 'rows' parameter" do
expect(subject.with(rows: 10).send(:rows)).to eq 10
end

it "should use the provided default" do
expect(subject.send(:rows, 17)).to eq 17
end

it "should be set to the configured default" do
blacklight_config.default_per_page = 42
expect(subject.send(:rows)).to eq 42
end

it "should default to 10" do
blacklight_config.default_per_page = nil
expect(subject.send(:rows)).to eq 10
end

it "should limit the number of rows to the configured maximum" do
blacklight_config.max_per_page = 1000
expect(subject.send(:rows, 1001)).to eq 1000
end
end

describe "#sort" do
it "should pass through the sort parameter" do
expect(subject.with(sort: 'x').send(:sort)).to eq 'x'
end

it "should use the default if no sort parameter is given" do
blacklight_config.default_sort_field = double(sort: 'x desc')
expect(subject.send(:sort)).to eq 'x desc'
end

it "should use the requested sort field" do
blacklight_config.add_sort_field 'x', sort: 'x asc'
expect(subject.with(sort: 'x').send(:sort)).to eq 'x asc'
end
end

describe "#search_field" do
it "should use the requested search field" do
blacklight_config.add_search_field 'x'
expect(subject.with(search_field: 'x').send(:search_field)).to eq blacklight_config.search_fields['x']
end
end
end

0 comments on commit 698774d

Please sign in to comment.