Skip to content

Commit 5ece9e6

Browse files
committed
Primo/timdex partials respected
* Adds a lot more tests of the application controller logic by introducing unit tests in addition to Integration tests * Adds an `api` key to both normalized timdex and primo records that we can use in the views * Removes redundant logic for `all` tab in views and normalizes to use newly introduced `api` key * Ensures timdex queries always run when they should be removing condition that unnecessarily complicated all tab queries
1 parent 8290364 commit 5ece9e6

11 files changed

+110
-15
lines changed

app/controllers/application_controller.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ def set_active_tab
2222
end
2323

2424
def primo_tabs
25-
%w[all alma cdi primo]
25+
%w[alma cdi primo]
2626
end
2727

2828
def timdex_tabs
29-
%w[all aspace timdex timdex_alma website]
29+
%w[aspace timdex timdex_alma website]
30+
end
31+
32+
def all_tabs
33+
['all', *primo_tabs, *timdex_tabs]
3034
end
3135

3236
private
3337

3438
def valid_tab?(tab)
35-
(primo_tabs << timdex_tabs).flatten.uniq.include?(tab)
39+
all_tabs.include?(tab)
3640
end
3741
end

app/controllers/search_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def query_timdex(query)
181181
Rails.cache.fetch("#{cache_key}/#{@active_tab}", expires_in: 12.hours) do
182182
raw = if Feature.enabled?(:geodata)
183183
execute_geospatial_query(query)
184-
elsif timdex_tabs.include? @active_tab
184+
else
185185
TimdexBase::Client.query(TimdexSearch::BaseQuery, variables: query)
186186
end
187187

app/models/normalize_primo_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(record, query)
88
def normalize
99
{
1010
# Core fields
11+
api: 'primo',
1112
title:,
1213
creators:,
1314
source:,

app/models/normalize_timdex_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def initialize(record, query)
88
def normalize
99
{
1010
# Core fields
11+
api: 'timdex',
1112
title:,
1213
creators:,
1314
source:,

app/views/search/results.html.erb

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,14 @@
2222
<p class="results-context-description">From all MIT Libraries sources</p>
2323
<div id="results-layout-wrapper">
2424
<main id="results">
25-
<ol class="results-list" start="<%= @pagination[:start] %>">
26-
<% case @active_tab %>
27-
<% when *@primo_tabs %>
28-
<%= render(partial: 'search/result_primo', collection: @results, as: :result) %>
29-
<% when *@timdex_tabs %>
30-
<%= render(partial: 'search/result', collection: @results, as: :result) %>
31-
<% when 'all' %>
32-
<% @results.each do |result| %>
33-
<% if result[:source] == 'Primo' %>
25+
<ol class="results-list" start="<%= @pagination[:start] %>">
26+
<% @results.each do |result| %>
27+
<% if result[:api] == 'primo' %>
3428
<%= render(partial: 'search/result_primo', locals: { result: result }) %>
35-
<% else %>
29+
<% elsif result[:api] == 'timdex' %>
3630
<%= render(partial: 'search/result', locals: { result: result }) %>
3731
<% end %>
3832
<% end %>
39-
<% end %>
4033
</ol>
4134
<%= render partial: "results_callouts" %>
4235
</main>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'test_helper'
2+
3+
class ApplicationControllerUnitTest < ActionController::TestCase
4+
setup do
5+
@controller = ApplicationController.new
6+
end
7+
8+
test '#primo_tabs returns expected tabs' do
9+
assert_equal %w[alma cdi primo], @controller.primo_tabs
10+
end
11+
12+
test '#timdex_tabs returns expected tabs' do
13+
assert_equal %w[aspace timdex timdex_alma website], @controller.timdex_tabs
14+
end
15+
16+
test '#all_tabs includes both primo and timdex tabs as well as all tab' do
17+
expected = %w[all alma cdi primo aspace timdex timdex_alma website]
18+
assert_equal expected, @controller.all_tabs
19+
end
20+
21+
test '#valid_tab? returns true for valid tabs' do
22+
assert @controller.send(:valid_tab?, 'all')
23+
assert @controller.send(:valid_tab?, 'alma')
24+
assert @controller.send(:valid_tab?, 'timdex')
25+
end
26+
27+
test '#valid_tab? returns false for invalid tabs' do
28+
refute @controller.send(:valid_tab?, 'invalid')
29+
refute @controller.send(:valid_tab?, '')
30+
end
31+
32+
test 'set_active_tab defaults @active_tab to all when no params or cookies are set' do
33+
@controller.stubs(:params).returns({})
34+
@controller.stubs(:cookies).returns({})
35+
@controller.set_active_tab
36+
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
37+
end
38+
39+
test 'set_active_tab sets @active_tab to tab when tab params is valid' do
40+
@controller.stubs(:params).returns({ tab: 'primo' })
41+
@controller.stubs(:cookies).returns({})
42+
@controller.set_active_tab
43+
assert_equal 'primo', @controller.instance_variable_get(:@active_tab)
44+
end
45+
46+
test 'set_active_tab sets @active_tab to all when tab params is invalid and no cookie is set' do
47+
@controller.stubs(:params).returns({ tab: 'supertab' })
48+
@controller.stubs(:cookies).returns({})
49+
@controller.set_active_tab
50+
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
51+
end
52+
53+
test 'set_active_tab sets @active_tab to cookie value when tab params is invalid and valid cookie is set' do
54+
@controller.stubs(:params).returns({ tab: 'supertab' })
55+
@controller.stubs(:cookies).returns({ last_tab: 'timdex' })
56+
@controller.set_active_tab
57+
assert_equal 'timdex', @controller.instance_variable_get(:@active_tab)
58+
end
59+
60+
test 'set_active_tab sets @active_tab to all value when tab params is invalid and cookie is invalid' do
61+
@controller.stubs(:params).returns({ tab: 'supertab' })
62+
@controller.stubs(:cookies).returns({ last_tab: 'woohoo' })
63+
@controller.set_active_tab
64+
assert_equal 'all', @controller.instance_variable_get(:@active_tab)
65+
end
66+
end

test/controllers/search_controller_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ class SearchControllerTest < ActionDispatch::IntegrationTest
44
def mock_primo_search_success
55
# Mock the Primo search components to avoid external API calls
66
sample_doc = {
7+
api: 'primo',
78
title: 'Sample Primo Document Title',
89
format: 'Article',
910
year: '2025',
@@ -26,6 +27,7 @@ def mock_primo_search_success
2627
def mock_timdex_search_success
2728
# Mock the TIMDEX GraphQL client to avoid external API calls
2829
sample_result = {
30+
'api' => 'timdex',
2931
'title' => 'Sample TIMDEX Document Title',
3032
'timdexRecordId' => 'sample-record-123',
3133
'contentType' => [{ 'value' => 'Article' }],

test/models/normalize_primo_record_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,9 @@ def cdi_record
423423
assert_not_nil dedup_url
424424
assert_match %r{/discovery/search\?}, dedup_url
425425
end
426+
427+
test 'includes primo as source api' do
428+
normalized = NormalizePrimoRecord.new(full_record, 'test query').normalize
429+
assert_equal 'primo', normalized[:api]
430+
end
426431
end

test/models/normalize_primo_results_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,13 @@ def empty_results
6767
normalizer = NormalizePrimoResults.new(nil, 'test query')
6868
assert_equal 0, normalizer.total_results
6969
end
70+
71+
test 'all primo results includes primo as source api' do
72+
normalizer = NormalizePrimoResults.new(sample_results, 'test query')
73+
normalized = normalizer.normalize
74+
75+
normalized.each do |result|
76+
assert_equal 'primo', result[:api]
77+
end
78+
end
7079
end

test/models/normalize_timdex_record_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@ def minimal_record
218218
assert_not_includes normalized.keys, 'other_availability'
219219
assert_not_includes normalized.keys, 'container'
220220
end
221+
222+
test 'includes timdex as source api' do
223+
normalized = NormalizeTimdexRecord.new(full_record, 'test').normalize
224+
assert_equal 'timdex', normalized[:api]
225+
end
221226
end

0 commit comments

Comments
 (0)