Skip to content

Commit

Permalink
Implements expected behavior for disable_facets=false and allow query…
Browse files Browse the repository at this point in the history
…_aggregations to be callable with an array of symbols
  • Loading branch information
codycooperross committed Dec 12, 2024
1 parent 642b221 commit f4029e1
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 35 deletions.
21 changes: 13 additions & 8 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -775,7 +774,6 @@ def self.all_doi_aggregations
},
},
}
end

def self.default_doi_query_facets
[
Expand All @@ -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
Expand Down
85 changes: 85 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
78 changes: 51 additions & 27 deletions spec/requests/datacite_dois/datacite_dois_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f4029e1

Please sign in to comment.