From b4e662ded000df7a5505f628b342e8b377b6af70 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 11 Dec 2023 13:16:53 -0500 Subject: [PATCH 1/2] Guard agains incomplete related_dois --- app/models/doi/indexer/related_doi_indexer.rb | 16 ++- spec/models/doi/related_doi_indexer_spec.rb | 119 ++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 spec/models/doi/related_doi_indexer_spec.rb diff --git a/app/models/doi/indexer/related_doi_indexer.rb b/app/models/doi/indexer/related_doi_indexer.rb index e81c049f7..c267dd93f 100644 --- a/app/models/doi/indexer/related_doi_indexer.rb +++ b/app/models/doi/indexer/related_doi_indexer.rb @@ -2,13 +2,27 @@ module Doi::Indexer class RelatedDoiIndexer + REQUIRED_KEYS = %w[ + relatedIdentifier + relatedIdentifierType + relationType + ] + def initialize(related_identifiers) @related_identifiers = Array.wrap(related_identifiers) @related_dois = nil end + def is_related_doi?(related) + related.is_a?(Hash) && + REQUIRED_KEYS.all? { |k| related.key?(k) } && + related.fetch("relatedIdentifierType", nil) == "DOI" + end + def related_dois - @related_dois ||= @related_identifiers.select { |r| r.fetch("relatedIdentifierType", nil) == "DOI" } + @related_dois ||= @related_identifiers.select do |r| + is_related_doi?(r) + end end def related_grouped_by_id diff --git a/spec/models/doi/related_doi_indexer_spec.rb b/spec/models/doi/related_doi_indexer_spec.rb new file mode 100644 index 000000000..5ecf499f7 --- /dev/null +++ b/spec/models/doi/related_doi_indexer_spec.rb @@ -0,0 +1,119 @@ + +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Doi::Indexer::RelatedDoiIndexer do + + describe "related_dois with different input" do + let(:good_related_identifier) do + { + "relatedIdentifier": "10.1234/5678", + "relatedIdentifierType": "DOI", + "relationType": "IsPartOf", + }.with_indifferent_access + end + + it "handles nil input" do + expect(described_class.new(nil).related_dois).to eq([]) + end + + it "handles empty_string input" do + expect(described_class.new("").related_dois).to eq([]) + end + + it "handles a list with empty_string as input" do + expect(described_class.new([""]).related_dois).to eq([]) + end + + + it "handles array of hashes with all required keys" do + expect(described_class.new([good_related_identifier]).related_dois).to eq( + [good_related_identifier]) + end + + it "handles single hash with all required keys" do + expect(described_class.new(good_related_identifier).related_dois).to eq( + [good_related_identifier]) + end + + it "exclude DOIs with if a required key is missing" do + expect(described_class.new( + good_related_identifier.except("relatedIdentifier") + ).related_dois).to eq( []) + expect(described_class.new( + good_related_identifier.except("relatedIdentifierType") + ).related_dois).to eq( []) + expect(described_class.new( + good_related_identifier.except("relationType") + ).related_dois).to eq( []) + end + end + + describe "relation_type grouping" do + let(:related_identifiers) do + [ + { + "relatedIdentifier": "10.1234/5678", + "relatedIdentifierType": "DOI", + "relationType": "IsPartOf", + "resourceTypeGeneral": "Dataset", + }.with_indifferent_access, + { + "relatedIdentifier": "10.1234/9999", + "relatedIdentifierType": "DOI", + "relationType": "HasVersion", + "resourceTypeGeneral": "Text", + }.with_indifferent_access, + { + "relatedIdentifier": "10.1234/9999", + "relatedIdentifierType": "DOI", + "relationType": "References", + "resourceTypeGeneral": "Text", + }.with_indifferent_access + ] + end + + it "can accept an array of valid identifiers" do + expect(described_class.new(related_identifiers).related_dois).to eq(related_identifiers) + end + + it "groups related_dois by relatedIdentifier" do + expect(described_class.new(related_identifiers).relation_types_gouped_by_id).to eq( + { + "10.1234/5678" => ["is_part_of"], + "10.1234/9999" => ["has_version", "references"], + } + ) + end + + it "groups related_dois by relatedIdentifier" do + expect(described_class.new(related_identifiers).related_grouped_by_id).to eq( + { + "10.1234/5678" => [ + { + "relatedIdentifier"=> "10.1234/5678", + "relatedIdentifierType"=> "DOI", + "relationType"=> "IsPartOf", + "resourceTypeGeneral"=> "Dataset", + } + ], + "10.1234/9999" => [ + { + "relatedIdentifier"=> "10.1234/9999", + "relatedIdentifierType"=> "DOI", + "relationType"=> "HasVersion", + "resourceTypeGeneral"=> "Text", + }, + { + "relatedIdentifier"=> "10.1234/9999", + "relatedIdentifierType"=> "DOI", + "relationType"=> "References", + "resourceTypeGeneral"=> "Text", + } + ], + } + ) + end + end +end From df07508a14f8cee7a0bba0218dcfc3243eaf1707 Mon Sep 17 00:00:00 2001 From: jrhoads Date: Mon, 11 Dec 2023 13:27:32 -0500 Subject: [PATCH 2/2] Appease rubocop --- spec/models/doi/related_doi_indexer_spec.rb | 31 ++++++++++----------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/spec/models/doi/related_doi_indexer_spec.rb b/spec/models/doi/related_doi_indexer_spec.rb index 5ecf499f7..3ad7b78e6 100644 --- a/spec/models/doi/related_doi_indexer_spec.rb +++ b/spec/models/doi/related_doi_indexer_spec.rb @@ -4,7 +4,6 @@ require "rails_helper" RSpec.describe Doi::Indexer::RelatedDoiIndexer do - describe "related_dois with different input" do let(:good_related_identifier) do { @@ -40,13 +39,13 @@ it "exclude DOIs with if a required key is missing" do expect(described_class.new( good_related_identifier.except("relatedIdentifier") - ).related_dois).to eq( []) + ).related_dois).to eq([]) expect(described_class.new( good_related_identifier.except("relatedIdentifierType") - ).related_dois).to eq( []) + ).related_dois).to eq([]) expect(described_class.new( good_related_identifier.except("relationType") - ).related_dois).to eq( []) + ).related_dois).to eq([]) end end @@ -92,24 +91,24 @@ { "10.1234/5678" => [ { - "relatedIdentifier"=> "10.1234/5678", - "relatedIdentifierType"=> "DOI", - "relationType"=> "IsPartOf", - "resourceTypeGeneral"=> "Dataset", + "relatedIdentifier" => "10.1234/5678", + "relatedIdentifierType" => "DOI", + "relationType" => "IsPartOf", + "resourceTypeGeneral" => "Dataset", } ], "10.1234/9999" => [ { - "relatedIdentifier"=> "10.1234/9999", - "relatedIdentifierType"=> "DOI", - "relationType"=> "HasVersion", - "resourceTypeGeneral"=> "Text", + "relatedIdentifier" => "10.1234/9999", + "relatedIdentifierType" => "DOI", + "relationType" => "HasVersion", + "resourceTypeGeneral" => "Text", }, { - "relatedIdentifier"=> "10.1234/9999", - "relatedIdentifierType"=> "DOI", - "relationType"=> "References", - "resourceTypeGeneral"=> "Text", + "relatedIdentifier" => "10.1234/9999", + "relatedIdentifierType" => "DOI", + "relationType" => "References", + "resourceTypeGeneral" => "Text", } ], }