From 9b360c06e16310eee48f08c7aec72c863aef127d Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Tue, 4 Jun 2024 13:18:28 -0400 Subject: [PATCH 1/8] Adds field as an option for browse methods --- lib/utilities/browse_solr_client.rb | 12 +++---- spec/utilities/browse_solr_client_spec.rb | 44 +++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/utilities/browse_solr_client.rb b/lib/utilities/browse_solr_client.rb index 8df99a3..776ed3b 100644 --- a/lib/utilities/browse_solr_client.rb +++ b/lib/utilities/browse_solr_client.rb @@ -15,17 +15,17 @@ def initialize(solr_url: S.solr_url, core: S.call_number_collection, match_field @q = q end - def browse_reference_on_top(reference_id:, rows: 20) + def browse_reference_on_top(reference_id:, rows: 20, field: "id") # square brackets includes reference in return - range = "id:[\"#{reference_id}\" TO *]" - sort = "id asc" + range = "#{field}:[\"#{reference_id}\" TO *]" + sort = "#{field} asc" browse(rows: rows, sort: sort, range: range) end - def browse_reference_on_bottom(reference_id:, rows: 20) + def browse_reference_on_bottom(reference_id:, rows: 20, field: "id") # curly brackets exclues reference in return - range = "id:{* TO \"#{reference_id}\"}" - sort = "id desc" + range = "#{field}:{* TO \"#{reference_id}\"}" + sort = "#{field} desc" browse(rows: rows, sort: sort, range: range) end diff --git a/spec/utilities/browse_solr_client_spec.rb b/spec/utilities/browse_solr_client_spec.rb index 8d0df28..daa1a58 100644 --- a/spec/utilities/browse_solr_client_spec.rb +++ b/spec/utilities/browse_solr_client_spec.rb @@ -2,6 +2,50 @@ subject do described_class.new end + context "#browse_reference_on_top" do + it "uses id as the default range and sort field" do + s = stub_solr_get_request(url: "#{S.call_number_collection}/select", query: hash_including( + { + fq: 'id:["whatever" TO *]', + sort: "id asc" + } + )) + subject.browse_reference_on_top(reference_id: "whatever") + expect(s).to have_been_requested + end + it "uses field value as the default range and sort field when given" do + s = stub_solr_get_request(url: "#{S.call_number_collection}/select", query: hash_including( + { + fq: 'some_other_field:["whatever" TO *]', + sort: "some_other_field asc" + } + )) + subject.browse_reference_on_top(reference_id: "whatever", field: "some_other_field") + expect(s).to have_been_requested + end + end + context "#browse_reference_on_bottom" do + it "uses id as the default range and sort field" do + s = stub_solr_get_request(url: "#{S.call_number_collection}/select", query: hash_including( + { + fq: 'id:{* TO "whatever"}', + sort: "id desc" + } + )) + subject.browse_reference_on_bottom(reference_id: "whatever") + expect(s).to have_been_requested + end + it "uses field value as the default range and sort field when given" do + s = stub_solr_get_request(url: "#{S.call_number_collection}/select", query: hash_including( + { + fq: 'some_other_field:{* TO "whatever"}', + sort: "some_other_field desc" + } + )) + subject.browse_reference_on_bottom(reference_id: "whatever", field: "some_other_field") + expect(s).to have_been_requested + end + end context "#exact_matches" do it "returns an array of ids for an exact match" do stub_solr_get_request(url: "#{S.call_number_collection}/select", query: hash_including({fq: 'callnumber:"Thing"'}), output: fixture("biblio_results.json")) From 4cc53d514928e9f5eaddf3066eea0a447595c4b6 Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Tue, 4 Jun 2024 13:55:59 -0400 Subject: [PATCH 2/8] adds match_field BrowseList inputs This enables the first page of results to use a different field for fq and sort. --- lib/models/author_list.rb | 1 + lib/models/browse_list.rb | 14 ++++++++++---- lib/models/call_number_list.rb | 3 ++- lib/models/subject_list.rb | 1 + spec/models/browse_list_spec.rb | 11 ++++++++++- spec/requests_spec.rb | 18 +++++++++--------- 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/lib/models/author_list.rb b/lib/models/author_list.rb index bf6ef25..3ecc648 100644 --- a/lib/models/author_list.rb +++ b/lib/models/author_list.rb @@ -6,6 +6,7 @@ def self.for(direction:, reference_id:, num_rows_to_display:, original_reference num_rows_to_display: num_rows_to_display, original_reference: original_reference, banner_reference: banner_reference, + match_field: "term", browse_solr_client: BrowseSolrClient.new(core: S.authority_collection, match_field: "term", q: "browse_field:name") ) diff --git a/lib/models/browse_list.rb b/lib/models/browse_list.rb index c20348c..d268ea2 100644 --- a/lib/models/browse_list.rb +++ b/lib/models/browse_list.rb @@ -1,9 +1,15 @@ class BrowseList attr_reader :original_reference, :num_rows_to_display, :num_matches, :exact_matches, :banner_reference, :index_docs - def self.for(direction:, reference_id:, num_rows_to_display:, original_reference:, + def self.for( + direction:, + reference_id:, + num_rows_to_display:, + original_reference:, banner_reference:, - browse_solr_client: BrowseSolrClient.new) + browse_solr_client: BrowseSolrClient.new, + match_field: "PlACEHOLDER" + ) my_banner_reference = banner_reference exact_matches = browse_solr_client.exact_matches(value: original_reference) @@ -32,8 +38,8 @@ def self.for(direction:, reference_id:, num_rows_to_display:, original_reference else # index_before:, index_after: return BrowseList::Empty.new if reference_id.nil? || reference_id == "" - index_before = browse_solr_client.browse_reference_on_bottom(reference_id: reference_id, rows: 3) - index_after = browse_solr_client.browse_reference_on_top(reference_id: reference_id, rows: num_rows_to_display - 1) + index_before = browse_solr_client.browse_reference_on_bottom(reference_id: reference_id, rows: 3, field: match_field) + index_after = browse_solr_client.browse_reference_on_top(reference_id: reference_id, rows: num_rows_to_display - 1, field: match_field) my_banner_reference = index_after&.body&.dig("response", "docs")&.first&.dig("id") # need above and below BrowseList::ReferenceInMiddle.new( diff --git a/lib/models/call_number_list.rb b/lib/models/call_number_list.rb index 5cd7738..b9424c7 100644 --- a/lib/models/call_number_list.rb +++ b/lib/models/call_number_list.rb @@ -5,7 +5,8 @@ def self.for(direction:, reference_id:, num_rows_to_display:, original_reference reference_id: reference_id, num_rows_to_display: num_rows_to_display, original_reference: original_reference, - banner_reference: banner_reference + banner_reference: banner_reference, + match_field: "callnumber" ) bib_ids = browse_list.docs.map { |x| x["bib_id"] } diff --git a/lib/models/subject_list.rb b/lib/models/subject_list.rb index 64825fc..58a7076 100644 --- a/lib/models/subject_list.rb +++ b/lib/models/subject_list.rb @@ -6,6 +6,7 @@ def self.for(direction:, reference_id:, num_rows_to_display:, original_reference num_rows_to_display: num_rows_to_display, original_reference: original_reference, banner_reference: banner_reference, + match_field: "term", browse_solr_client: BrowseSolrClient.new(core: S.authority_collection, match_field: "term", q: "browse_field:subject") ) diff --git a/spec/models/browse_list_spec.rb b/spec/models/browse_list_spec.rb index a4e705d..1669e58 100644 --- a/spec/models/browse_list_spec.rb +++ b/spec/models/browse_list_spec.rb @@ -9,7 +9,8 @@ num_rows_to_display: 20, original_reference: nil, banner_reference: nil, - browse_solr_client: @browse_client + browse_solr_client: @browse_client, + match_field: "my_match_field" } end subject do @@ -19,11 +20,17 @@ @params[:direction] = "next" allow(@browse_client).to receive(:browse_reference_on_top) expect(subject.class).to eq(BrowseList::ReferenceOnTop) + expect(@browse_client).to have_received(:browse_reference_on_top).with( + {reference_id: nil, rows: 22} + ) end it "returns a ReferenceOnTop when direction is previous" do @params[:direction] = "previous" allow(@browse_client).to receive(:browse_reference_on_bottom) expect(subject.class).to eq(BrowseList::ReferenceOnBottom) + expect(@browse_client).to have_received(:browse_reference_on_bottom).with( + {reference_id: nil, rows: 21} + ) end context "direction is nil" do it "returns a ReferenceInMiddle if there's a reference_id" do @@ -31,6 +38,8 @@ allow(@browse_client).to receive(:browse_reference_on_top) allow(@browse_client).to receive(:browse_reference_on_bottom) expect(subject.class).to eq(BrowseList::ReferenceInMiddle) + expect(@browse_client).to have_received(:browse_reference_on_top).with({field: "my_match_field", reference_id: "Something", rows: 19}) + expect(@browse_client).to have_received(:browse_reference_on_bottom).with({field: "my_match_field", reference_id: "Something", rows: 3}) end it "returns a Empty if there in not a reference_id" do expect(subject.class).to eq(BrowseList::Empty) diff --git a/spec/requests_spec.rb b/spec/requests_spec.rb index 0549211..e0c4f7a 100644 --- a/spec/requests_spec.rb +++ b/spec/requests_spec.rb @@ -14,15 +14,15 @@ context "get /callnumber" do it "for a successful query, returns status OK" do stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({fq: 'callnumber:"Thing"'}), output: fixture("biblio_results.json")) - stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({sort: "id desc"}), output: fixture("callnumbers_before.json")) - stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({fq: 'id:["Thing" TO *]'}), output: fixture("callnumbers_results.json")) + stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({sort: "callnumber desc"}), output: fixture("callnumbers_before.json")) + stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({fq: 'callnumber:["Thing" TO *]'}), output: fixture("callnumbers_results.json")) stub_biblio_get_request(url: "biblio/select", query: hash_including({}), output: fixture("biblio_results_middle.json")) get "/callnumber", {query: "Thing"} expect(last_response.status).to eq(200) end it "for a network error, it still returns a successful response, but with an error message" do stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({fq: 'callnumber:"Thing"'}), no_return: true).to_timeout - stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({sort: "id desc"}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({sort: "callnumber desc"}), no_return: true).to_timeout get "/callnumber", {query: "Thing"} expect(last_response.status).to eq(200) end @@ -30,14 +30,14 @@ context "get /author" do it "returns status OK" do stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), output: fixture("author_exact_matches.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "id desc"}), output: fixture("author_results.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'id:["Thing" TO *]'}), output: fixture("author_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), output: fixture("author_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:["Thing" TO *]'}), output: fixture("author_results.json")) get "/author", {query: "Thing"} expect(last_response.status).to eq(200) end it "for a network error, it still returns a successful response, but with an error message" do stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), no_return: true).to_timeout - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "id desc"}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), no_return: true).to_timeout get "/author", {query: "Thing"} expect(last_response.status).to eq(200) end @@ -45,14 +45,14 @@ context "get /subject" do it "returns status OK" do stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), output: fixture("author_exact_matches.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "id desc"}), output: fixture("subject_results.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'id:["Thing" TO *]'}), output: fixture("subject_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), output: fixture("subject_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:["Thing" TO *]'}), output: fixture("subject_results.json")) get "/subject", {query: "Thing"} expect(last_response.status).to eq(200) end it "for a network error, it still returns a successful response, but with an error message" do stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), no_return: true).to_timeout - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "id desc"}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), no_return: true).to_timeout get "/subject", {query: "Thing"} expect(last_response.status).to eq(200) end From f07144872f16faaa23dc6892708372de4bc523c0 Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Tue, 4 Jun 2024 14:46:55 -0400 Subject: [PATCH 3/8] reverted to using id for authority browse --- lib/models/author_list.rb | 2 +- lib/models/browse_list.rb | 1 - lib/models/subject_list.rb | 2 +- spec/requests_spec.rb | 21 +++++++++++---------- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/models/author_list.rb b/lib/models/author_list.rb index 3ecc648..36cc050 100644 --- a/lib/models/author_list.rb +++ b/lib/models/author_list.rb @@ -6,7 +6,7 @@ def self.for(direction:, reference_id:, num_rows_to_display:, original_reference num_rows_to_display: num_rows_to_display, original_reference: original_reference, banner_reference: banner_reference, - match_field: "term", + match_field: "id", browse_solr_client: BrowseSolrClient.new(core: S.authority_collection, match_field: "term", q: "browse_field:name") ) diff --git a/lib/models/browse_list.rb b/lib/models/browse_list.rb index d268ea2..4a35490 100644 --- a/lib/models/browse_list.rb +++ b/lib/models/browse_list.rb @@ -16,7 +16,6 @@ def self.for( case direction when "next" # includes reference in results - # require "byebug"; byebug index_response = browse_solr_client.browse_reference_on_top(reference_id: reference_id, rows: num_rows_to_display + 2) BrowseList::ReferenceOnTop.new( index_response: index_response&.body, diff --git a/lib/models/subject_list.rb b/lib/models/subject_list.rb index 58a7076..c68d0d7 100644 --- a/lib/models/subject_list.rb +++ b/lib/models/subject_list.rb @@ -6,7 +6,7 @@ def self.for(direction:, reference_id:, num_rows_to_display:, original_reference num_rows_to_display: num_rows_to_display, original_reference: original_reference, banner_reference: banner_reference, - match_field: "term", + match_field: "id", browse_solr_client: BrowseSolrClient.new(core: S.authority_collection, match_field: "term", q: "browse_field:subject") ) diff --git a/spec/requests_spec.rb b/spec/requests_spec.rb index e0c4f7a..36af24c 100644 --- a/spec/requests_spec.rb +++ b/spec/requests_spec.rb @@ -4,6 +4,7 @@ before(:each) do @call_number_collection = S.call_number_collection @authority_collection = S.authority_collection + @authority_match_field = "id" end context "get /" do it "has status OK" do @@ -29,30 +30,30 @@ end context "get /author" do it "returns status OK" do - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), output: fixture("author_exact_matches.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), output: fixture("author_results.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:["Thing" TO *]'}), output: fixture("author_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: "term:\"Thing\""}), output: fixture("author_exact_matches.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "#{@authority_match_field} desc"}), output: fixture("author_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: "#{@authority_match_field}:[\"Thing\" TO *]"}), output: fixture("author_results.json")) get "/author", {query: "Thing"} expect(last_response.status).to eq(200) end it "for a network error, it still returns a successful response, but with an error message" do - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), no_return: true).to_timeout - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: "term:\"Thing\""}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "#{@authority_match_field} desc"}), no_return: true).to_timeout get "/author", {query: "Thing"} expect(last_response.status).to eq(200) end end context "get /subject" do it "returns status OK" do - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), output: fixture("author_exact_matches.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), output: fixture("subject_results.json")) - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:["Thing" TO *]'}), output: fixture("subject_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: "term:\"Thing\""}), output: fixture("author_exact_matches.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "#{@authority_match_field} desc"}), output: fixture("subject_results.json")) + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: "#{@authority_match_field}:[\"Thing\" TO *]"}), output: fixture("subject_results.json")) get "/subject", {query: "Thing"} expect(last_response.status).to eq(200) end it "for a network error, it still returns a successful response, but with an error message" do - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: 'term:"Thing"'}), no_return: true).to_timeout - stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "term desc"}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({fq: "term:\"Thing\""}), no_return: true).to_timeout + stub_solr_get_request(url: "#{@authority_collection}/select", query: hash_including({sort: "#{@authority_match_field} desc"}), no_return: true).to_timeout get "/subject", {query: "Thing"} expect(last_response.status).to eq(200) end From 13c0f3548815248f990e6097010da84875405be0 Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Tue, 4 Jun 2024 17:23:49 -0400 Subject: [PATCH 4/8] adds carousel route and logic --- catalog-browse.rb | 12 +++++++ lib/models/carousel_list.rb | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 lib/models/carousel_list.rb diff --git a/catalog-browse.rb b/catalog-browse.rb index cafc4a1..0c51658 100644 --- a/catalog-browse.rb +++ b/catalog-browse.rb @@ -17,6 +17,7 @@ require_relative "lib/models/author_list" require_relative "lib/models/author_item" require_relative "lib/models/subject_list" +require_relative "lib/models/carousel_list" require_relative "lib/models/subject_item" require_relative "lib/models/search_dropdown" require_relative "lib/models/datastores" @@ -61,6 +62,17 @@ end erb :call_number, locals: {list: list} end + +get "/carousel" do + call_number = params[:query] + begin + content_type :json + CarouselList.list(call_number).to_json + rescue => e + logger.error(e.message) + status 500 + end +end post "/search" do redirect SearchDropdown.for(type: params["type"], query: params["query"]).url end diff --git a/lib/models/carousel_list.rb b/lib/models/carousel_list.rb new file mode 100644 index 0000000..b0691b2 --- /dev/null +++ b/lib/models/carousel_list.rb @@ -0,0 +1,62 @@ +require "byebug" +class CarouselList + def self.list(call_number) + browse_solr_client = BrowseSolrClient.new + + before = browse_solr_client.browse_reference_on_bottom(reference_id: call_number, rows: 22, field: "callnumber").body["response"]["docs"].reverse + after = browse_solr_client.browse_reference_on_top(reference_id: call_number, rows: 23, field: "callnumber").body["response"]["docs"] + + ordered_call_number_docs = [before, after].flatten + + bib_ids = ordered_call_number_docs.map { |x| x["bib_id"] } + catalog_response = CatalogSolrClient.client.get_bibs(bib_ids: bib_ids).body + items = ordered_call_number_docs.map do |browse_doc| + catalog_doc = catalog_response["response"]["docs"].find do |x| + x["id"] == browse_doc["bib_id"] + end + CarouselItem.new(catalog_doc, browse_doc) + end + items.map { |x| x.to_h } + end + + class CarouselItem + def initialize(catalog_doc, browse_doc) + @catalog_doc = catalog_doc + @browse_doc = browse_doc + end + + def title + @catalog_doc["title_display"]&.first + end + + def author + @catalog_doc["main_author_display"]&.first + end + + def call_number + @browse_doc["callnumber"]&.strip + end + + def mms_id + @browse_doc["bib_id"] + end + + def date + @catalog_doc["display_date"] + end + + def url + "https://search.lib.umich.edu/catalog/record/#{mms_id}" + end + + def to_h + { + title: title, + author: author, + date: date, + call_number: call_number, + url: url + } + end + end +end From 7a72db5bc772ef22d298a84ba49cb80893f77fc8 Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Tue, 4 Jun 2024 17:31:23 -0400 Subject: [PATCH 5/8] forgot to get rid of byebug --- lib/models/carousel_list.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/models/carousel_list.rb b/lib/models/carousel_list.rb index b0691b2..6c21ab3 100644 --- a/lib/models/carousel_list.rb +++ b/lib/models/carousel_list.rb @@ -1,4 +1,3 @@ -require "byebug" class CarouselList def self.list(call_number) browse_solr_client = BrowseSolrClient.new From b7cf3ce9d487a15ecb5788110179d411642b20ca Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Wed, 26 Jun 2024 09:38:28 -0400 Subject: [PATCH 6/8] allow cors for carousel --- catalog-browse.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/catalog-browse.rb b/catalog-browse.rb index 0c51658..3e68331 100644 --- a/catalog-browse.rb +++ b/catalog-browse.rb @@ -67,6 +67,7 @@ call_number = params[:query] begin content_type :json + headers "Access-Control-Allow-Origin" => "*" CarouselList.list(call_number).to_json rescue => e logger.error(e.message) From 22d44389e084509a082d6aae7cce88c864ac419c Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Fri, 19 Jul 2024 14:04:53 -0400 Subject: [PATCH 7/8] adds unit tests to carousel --- lib/models/carousel_list.rb | 8 ++--- spec/models/carousel_list_spec.rb | 58 +++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 spec/models/carousel_list_spec.rb diff --git a/lib/models/carousel_list.rb b/lib/models/carousel_list.rb index 6c21ab3..856e6dc 100644 --- a/lib/models/carousel_list.rb +++ b/lib/models/carousel_list.rb @@ -1,18 +1,16 @@ class CarouselList - def self.list(call_number) - browse_solr_client = BrowseSolrClient.new - + def self.list(call_number, browse_solr_client = BrowseSolrClient.new, catalog_client = CatalongSolrClient.client) before = browse_solr_client.browse_reference_on_bottom(reference_id: call_number, rows: 22, field: "callnumber").body["response"]["docs"].reverse after = browse_solr_client.browse_reference_on_top(reference_id: call_number, rows: 23, field: "callnumber").body["response"]["docs"] ordered_call_number_docs = [before, after].flatten bib_ids = ordered_call_number_docs.map { |x| x["bib_id"] } - catalog_response = CatalogSolrClient.client.get_bibs(bib_ids: bib_ids).body + catalog_response = catalog_client.get_bibs(bib_ids: bib_ids).body items = ordered_call_number_docs.map do |browse_doc| catalog_doc = catalog_response["response"]["docs"].find do |x| x["id"] == browse_doc["bib_id"] - end + end || {} CarouselItem.new(catalog_doc, browse_doc) end items.map { |x| x.to_h } diff --git a/spec/models/carousel_list_spec.rb b/spec/models/carousel_list_spec.rb new file mode 100644 index 0000000..4679e24 --- /dev/null +++ b/spec/models/carousel_list_spec.rb @@ -0,0 +1,58 @@ +describe CarouselList do + before(:each) do + @browse_client = instance_double(BrowseSolrClient) + @catalog_client = instance_double(CatalogSolrClient::Client, get_bibs: OpenStruct.new(body: JSON.parse(fixture("biblio_results.json")))) + end + subject do + described_class.list("my_call_number", @browse_client, @catalog_client) + end + def body(file_name) + out = JSON.parse(fixture(file_name)) + OpenStruct.new(body: out) + end + it "returns a list of carousel items" do + allow(@browse_client).to receive(:browse_reference_on_bottom).and_return(body("callnumbers_before.json")) + + allow(@browse_client).to receive(:browse_reference_on_top).and_return(body("callnumbers_results.json")) + expect(subject.first[:call_number]).to eq("Z 253 .U581 1952") + end +end +describe CarouselList::CarouselItem do + before(:each) do + @catalog_doc = JSON.parse(fixture("biblio_results.json"))["response"]["docs"].first + @browse_doc = JSON.parse(fixture("callnumbers_results.json"))["response"]["docs"].first + end + subject do + described_class.new(@catalog_doc, @browse_doc) + end + it "has a title" do + expect(subject.title).to eq("Theory and practice of composition.") + end + it "has an author" do + expect(subject.author).to eq("United States. Government Printing Office") + end + it "has a call_number" do + expect(subject.call_number).to eq("Z 253 .U6 1963") + end + it "has an mms_id" do + expect(subject.mms_id).to eq("990011613060106381") + end + it "has a date" do + expect(subject.date).to eq("1950") + end + it "has a url" do + expect(subject.url).to eq("#{S.search_url}/catalog/record/990011613060106381") + end + it "has a hash output" do + expect(subject.to_h).to eq( + { + author: "United States. Government Printing Office", + call_number: "Z 253 .U6 1963", + date: "1950", + title: "Theory and practice of composition.", + url: "#{S.search_url}/catalog/record/990011613060106381" + + } + ) + end +end From f2f9486c39ca55e02add3bd28b904d700e693deb Mon Sep 17 00:00:00 2001 From: Monique Rio Date: Fri, 19 Jul 2024 14:10:37 -0400 Subject: [PATCH 8/8] add carousel to request spec; fixes bug --- lib/models/carousel_list.rb | 2 +- spec/requests_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/models/carousel_list.rb b/lib/models/carousel_list.rb index 856e6dc..0557cd6 100644 --- a/lib/models/carousel_list.rb +++ b/lib/models/carousel_list.rb @@ -1,5 +1,5 @@ class CarouselList - def self.list(call_number, browse_solr_client = BrowseSolrClient.new, catalog_client = CatalongSolrClient.client) + def self.list(call_number, browse_solr_client = BrowseSolrClient.new, catalog_client = CatalogSolrClient.client) before = browse_solr_client.browse_reference_on_bottom(reference_id: call_number, rows: 22, field: "callnumber").body["response"]["docs"].reverse after = browse_solr_client.browse_reference_on_top(reference_id: call_number, rows: 23, field: "callnumber").body["response"]["docs"] diff --git a/spec/requests_spec.rb b/spec/requests_spec.rb index 36af24c..a876716 100644 --- a/spec/requests_spec.rb +++ b/spec/requests_spec.rb @@ -65,6 +65,16 @@ expect(last_response.headers["Location"]).to eq("#{S.base_url}/author?query=Thing") end end + context "get /carousel" do + it "returns some json" do + stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({fq: 'callnumber:"Thing"'}), output: fixture("biblio_results.json")) + stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({sort: "callnumber desc"}), output: fixture("callnumbers_before.json")) + stub_solr_get_request(url: "#{@call_number_collection}/select", query: hash_including({fq: 'callnumber:["Thing" TO *]'}), output: fixture("callnumbers_results.json")) + stub_biblio_get_request(url: "biblio/select", query: hash_including({}), output: fixture("biblio_results_middle.json")) + get "/carousel", {query: "Thing"} + expect(last_response.status).to eq(200) + end + end context "get /-/live" do it "returns status OK" do get "/-/live"