Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ def results

# Render the response in HTML or JSON format
respond_to do |format|
format.json { render json: { results: @results, pagination: @pagination, errors: @errors } }
format.json { render json: { results: @results, hits: hit_count, errors: @errors } }
format.turbo_stream { render :results }
format.html { render :results }
end
Comment thread
qltysh[bot] marked this conversation as resolved.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found 3 issues:

1. Assignment Branch Condition size for results is too high. [<7, 35, 8> 36.58/17] [rubocop:Metrics/AbcSize]


2. Cyclomatic complexity for results is too high. [8/7] [rubocop:Metrics/CyclomaticComplexity]


3. Method has too many lines. [26/10] [rubocop:Metrics/MethodLength]

end

private

# This returns the total result count from whichever instance variable is present
def hit_count
if @pagination_load_more_enabled
@load_more&.[](:total_hits) || 0
else
@pagination&.[](:hits) || 0
end
end

# Sleep to simulate latency for testing loading indicators when responses are fast
def sleep_if_too_fast
start_time = Time.now
Expand Down
47 changes: 47 additions & 0 deletions test/controllers/search_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,34 @@ def source_filter_count(controller)
get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_USER_AGENT' => quepid_ua }
assert_response :success
assert_equal 'application/json; charset=utf-8', response.content_type
response_json = JSON.parse(response.body)
assert_equal %w[results hits errors], response_json.keys
assert_instance_of Array, response_json['results']
assert_instance_of Integer, response_json['hits']
end
end

test 'results in JSON format will report zero hit count in the face of missing data' do
SearchController.any_instance.stubs(:load_timdex_results).returns(nil)
secret_value = 'sooper_sekret'
quepid_ua = 'Quepid/1.0 (Web Scraper)'
# Test @pagination pathway
ClimateControl.modify FORMAT_TOKEN: secret_value do
get "/results?q=test&tab=timdex&format=json&format_token=#{secret_value}",
headers: { 'HTTP_USER_AGENT' => quepid_ua }
assert_response :success
response_json = JSON.parse(response.body)
assert_equal %w[results hits errors], response_json.keys
assert_equal 0, response_json['hits']
end
# Test @load_more pathway
ClimateControl.modify(FORMAT_TOKEN: secret_value, FEATURE_PAGINATION_LOAD_MORE: 'true') do
get "/results?q=test&tab=timdex&format=json&format_token=#{secret_value}",
headers: { 'HTTP_USER_AGENT' => quepid_ua }
assert_response :success
response_json = JSON.parse(response.body)
assert_equal %w[results hits errors], response_json.keys
assert_equal 0, response_json['hits']
end
end

Expand All @@ -1276,6 +1304,25 @@ def source_filter_count(controller)
get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_USER_AGENT' => quepid_ua }
assert_response :success
assert_equal 'application/json; charset=utf-8', response.content_type
response_json = JSON.parse(response.body)
assert_equal %w[results hits errors], response_json.keys
assert_instance_of Array, response_json['results']
assert_instance_of Integer, response_json['hits']
end
end

test 'results can be returned in JSON format when env is set while load-more pagination is enabled' do
secret_value = 'sooper_sekret'
quepid_ua = 'Quepid/1.0 (Web Scraper)'
ClimateControl.modify(FORMAT_TOKEN: secret_value, FEATURE_PAGINATION_LOAD_MORE: 'true') do
mock_timdex_search_with_hits(10)
get "/results?q=test&format=json&format_token=#{secret_value}", headers: { 'HTTP_USER_AGENT' => quepid_ua }
assert_response :success
assert_equal 'application/json; charset=utf-8', response.content_type
response_json = JSON.parse(response.body)
assert_equal %w[results hits errors], response_json.keys
assert_instance_of Array, response_json['results']
assert_instance_of Integer, response_json['hits']
end
end

Expand Down