From f4029e1252330437a449c8c871f31e76f1fd056f Mon Sep 17 00:00:00 2001 From: codycooperross <50597551+codycooperross@users.noreply.github.com> Date: Thu, 12 Dec 2024 11:38:52 -0500 Subject: [PATCH] Implements expected behavior for disable_facets=false and allow query_aggregations to be callable with an array of symbols --- app/models/doi.rb | 21 +++-- spec/models/doi_spec.rb | 85 +++++++++++++++++++ .../datacite_dois/datacite_dois_spec.rb | 78 +++++++++++------ 3 files changed, 149 insertions(+), 35 deletions(-) diff --git a/app/models/doi.rb b/app/models/doi.rb index 53cb7b206..0210d0475 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -674,10 +674,9 @@ def as_indexed_json(_options = {}) } end - def self.all_doi_aggregations - { - # number of resourceTypeGeneral increased from 16 to 28 in schema 4.4 - resource_types: { terms: { field: "resource_type_id_and_name", size: 30, min_doc_count: 1 } }, + DOI_AGGREGATION_DEFINITIONS = { + # number of resourceTypeGeneral increased from 30 to 32 in schema 4.6 + resource_types: { terms: { field: "resource_type_id_and_name", size: 32, min_doc_count: 1 } }, states: { terms: { field: "aasm_state", size: 3, min_doc_count: 1 } }, published: { date_histogram: { @@ -775,7 +774,6 @@ def self.all_doi_aggregations }, }, } - end def self.default_doi_query_facets [ @@ -801,10 +799,17 @@ def self.default_doi_query_facets end def self.query_aggregations(disable_facets: false, facets: nil) - return {} if disable_facets + return {} if disable_facets.to_s == "true" + + selected_facets = if facets.is_a?(String) + facets.split(",").map(&:strip).map(&:underscore).map(&:to_sym) + else + Array.wrap(facets).map(&:to_sym) + end.uniq + + selected_facets = default_doi_query_facets if facets.nil? - requested_facets = facets.respond_to?(:split) ? facets.split(",").map(&:strip).map(&:underscore).map(&:to_sym).uniq : default_doi_query_facets - requested_facets.index_with { |facet| all_doi_aggregations.dig(facet) }.compact + DOI_AGGREGATION_DEFINITIONS.slice(*selected_facets) end def self.provider_aggregations diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 85bad8fc1..5d03eb7f5 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -1929,6 +1929,91 @@ end end + describe "query_aggregations" do + default_aggregations = Doi.default_doi_query_facets + + it "returns default aggregations when disable_facets and facets are not set" do + aggregations = Doi.query_aggregations + + expect(aggregations.keys).to match_array(default_aggregations) + end + + it "returns default aggregations when disable_facets is set to false" do + aggregations = Doi.query_aggregations(disable_facets: false) + + expect(aggregations.keys).to match_array(default_aggregations) + end + + it "returns blank aggregations when disable_facets is true" do + aggregations = Doi.query_aggregations(disable_facets: true) + + expect(aggregations).to eq({}) + end + + it "returns blank aggregations when disable_facets is true string" do + aggregations = Doi.query_aggregations(disable_facets: "true") + + expect(aggregations).to eq({}) + end + + it "returns default aggregations when disable_facets is false" do + aggregations = Doi.query_aggregations(disable_facets: false) + + expect(aggregations.keys).to match_array(default_aggregations) + end + + it "returns default aggregations when disable_facets is false string" do + aggregations = Doi.query_aggregations(disable_facets: "false") + + expect(aggregations.keys).to match_array(default_aggregations) + end + + it "returns selected aggregations when facets is a string" do + facets_string = "creators_and_contributors, registrationAgencies,made_up_facet,states,registration_agencies" + aggregations = Doi.query_aggregations(facets: facets_string) + expected_aggregations = [:creators_and_contributors, :registration_agencies, :states] + + expect(aggregations.keys).to match_array(expected_aggregations) + end + + it "returns blank aggregations when facets is a blank string" do + facets_string = "" + aggregations = Doi.query_aggregations(facets: facets_string) + + expect(aggregations).to eq({}) + end + + it "returns selected aggregations when facets is an array of symbols" do + facets_array = [:creators_and_contributors, :registration_agencies, :states, :made_up_facet, :registration_agencies] + aggregations = Doi.query_aggregations(facets: facets_array) + expected_aggregations = [:creators_and_contributors, :registration_agencies, :states] + + expect(aggregations.keys).to match_array(expected_aggregations) + end + + it "returns blank aggregations when facets is a blank array" do + facets_array = [] + aggregations = Doi.query_aggregations(facets: facets_array) + + expect(aggregations).to eq({}) + end + + it "returns selected aggregations when facets are an array of symbols and disable_facets is false" do + facets_array = [:creators_and_contributors, :registration_agencies, :states, :made_up_facet, :registration_agencies] + aggregations = Doi.query_aggregations(facets: facets_array, disable_facets: false) + expected_aggregations = [:creators_and_contributors, :registration_agencies, :states] + + expect(aggregations.keys).to match_array(expected_aggregations) + end + + it "returns blank aggregations when facets are an array of symbols and disable_facets is true" do + facets_array = [:creators_and_contributors, :registration_agencies, :states, :made_up_facet, :registration_agencies] + aggregations = Doi.query_aggregations(facets: facets_array, disable_facets: true) + + expect(aggregations).to eq({}) + end + end + describe "formats" do content_url = [ "https://redivis.com/datasets/rt7m-4ndqm48zf/tables/1dgp-0rkbx6ahe?v=1.2", diff --git a/spec/requests/datacite_dois/datacite_dois_spec.rb b/spec/requests/datacite_dois/datacite_dois_spec.rb index 7f29ab56a..ee79d372f 100755 --- a/spec/requests/datacite_dois/datacite_dois_spec.rb +++ b/spec/requests/datacite_dois/datacite_dois_spec.rb @@ -13,6 +13,26 @@ def clear_doi_index DataciteDoi.__elasticsearch__.client.indices.refresh(index: DataciteDoi.index_name) end +DEFAULT_DOIS_FACETS = [ + "states", + "resourceTypes", + "created", + "published", + "registered", + "providers", + "clients", + "affiliations", + "prefixes", + "certificates", + "licenses", + "schemaVersions", + "linkChecksStatus", + "subjects", + "fieldsOfScience", + "citations", + "views", + "downloads" +] describe DataciteDoisController, type: :request, vcr: true do let(:admin) { create(:provider, symbol: "ADMIN") } @@ -244,39 +264,43 @@ def clear_doi_index expect(json["data"].size).to eq(10) expect(json.dig("meta", "total")).to eq(10) - expect(json.dig("meta").length).to eq(21) - expect(json.dig("meta", "states")).to be_truthy - expect(json.dig("meta", "resourceTypes")).to be_truthy - expect(json.dig("meta", "created")).to be_truthy - expect(json.dig("meta", "published")).to be_truthy - expect(json.dig("meta", "registered")).to be_truthy - expect(json.dig("meta", "providers")).to be_truthy - expect(json.dig("meta", "clients")).to be_truthy - expect(json.dig("meta", "affiliations")).to be_truthy - expect(json.dig("meta", "prefixes")).to be_truthy - expect(json.dig("meta", "certificates")).to be_truthy - expect(json.dig("meta", "licenses")).to be_truthy - expect(json.dig("meta", "schemaVersions")).to be_truthy - expect(json.dig("meta", "linkChecksStatus")).to be_truthy - expect(json.dig("meta", "subjects")).to be_truthy - expect(json.dig("meta", "fieldsOfScience")).to be_truthy - expect(json.dig("meta", "citations")).to be_truthy - expect(json.dig("meta", "views")).to be_truthy - expect(json.dig("meta", "downloads")).to be_truthy - - expect(json.dig("meta", "clientTypes")).to eq(nil) - expect(json.dig("meta", "languages")).to eq(nil) - expect(json.dig("meta", "creatorsAndContributors")).to eq(nil) - end - - it "returns no facets when disable-facets is set" do + expect(json.dig("meta").length).to eq(DEFAULT_DOIS_FACETS.length + 3) + expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + ["total", "totalPages", "page"]) + end + + it "returns default facets when disable-facets is set to false" do + get "/dois?disable-facets=false", nil, headers + + expect(last_response.status).to eq(200) + expect(json["data"].size).to eq(10) + expect(json.dig("meta", "total")).to eq(10) + + expect(json.dig("meta").length).to eq(DEFAULT_DOIS_FACETS.length + 3) + expect(json.dig("meta").keys).to match_array(DEFAULT_DOIS_FACETS + ["total", "totalPages", "page"]) + end + + it "returns no facets when disable-facets is set to true" do get "/dois?disable-facets=true", nil, headers expect(last_response.status).to eq(200) expect(json["data"].size).to eq(10) expect(json.dig("meta", "total")).to eq(10) expect(json.dig("meta").length).to eq(3) - expect(json.dig("meta", "states")).to eq(nil) + DEFAULT_DOIS_FACETS.each do |facet| + expect(json.dig("meta", facet)).to eq(nil) + end + end + + it "returns no facets when facets is empty" do + get "/dois?facets=", nil, headers + + expect(last_response.status).to eq(200) + expect(json["data"].size).to eq(10) + expect(json.dig("meta", "total")).to eq(10) + expect(json.dig("meta").length).to eq(3) + DEFAULT_DOIS_FACETS.each do |facet| + expect(json.dig("meta", facet)).to eq(nil) + end end it "returns specified facets when facets is set" do