diff --git a/lib/valkyrie/persistence/fedora/query_service.rb b/lib/valkyrie/persistence/fedora/query_service.rb index 14edac43f..84379c8be 100644 --- a/lib/valkyrie/persistence/fedora/query_service.rb +++ b/lib/valkyrie/persistence/fedora/query_service.rb @@ -74,6 +74,7 @@ def content_with_inbound(id:) end def find_inverse_references_by(resource:, property:) + validate_id resource.id content = content_with_inbound(id: resource.id) property_uri = adapter.schema.predicate_for(property: property, resource: nil) ids = content.graph.query([nil, property_uri, nil]).map(&:subject).map { |x| x.to_s.gsub(/#.*/, '') }.map { |x| adapter.uri_to_id(x) } diff --git a/lib/valkyrie/persistence/memory/query_service.rb b/lib/valkyrie/persistence/memory/query_service.rb index eb00d2fba..2e9e61839 100644 --- a/lib/valkyrie/persistence/memory/query_service.rb +++ b/lib/valkyrie/persistence/memory/query_service.rb @@ -61,10 +61,12 @@ def find_references_by(resource:, property:) # other resources. # @param property [Symbol] The property which, on other resources, is # referencing the given `resource` + # @raise [ArgumentError] Raised when the ID is not in the persistence backend. # @return [Array] All resources in the persistence backend # which have the ID of the given `resource` in their `property` property. Not # in order. def find_inverse_references_by(resource:, property:) + validate_id resource.id find_all.select do |obj| begin Array.wrap(obj[property]).include?(resource.id) diff --git a/lib/valkyrie/persistence/postgres/query_service.rb b/lib/valkyrie/persistence/postgres/query_service.rb index 01df2f9fa..bb7462347 100644 --- a/lib/valkyrie/persistence/postgres/query_service.rb +++ b/lib/valkyrie/persistence/postgres/query_service.rb @@ -54,6 +54,7 @@ def find_references_by(resource:, property:) # (see Valkyrie::Persistence::Memory::QueryService#find_inverse_references_by) def find_inverse_references_by(resource:, property:) + validate_id resource.id internal_array = "{\"#{property}\": [{\"id\": \"#{resource.id}\"}]}" run_query(find_inverse_references_query, internal_array) end diff --git a/lib/valkyrie/persistence/solr/query_service.rb b/lib/valkyrie/persistence/solr/query_service.rb index 06c184b17..6a3c039f9 100644 --- a/lib/valkyrie/persistence/solr/query_service.rb +++ b/lib/valkyrie/persistence/solr/query_service.rb @@ -44,6 +44,7 @@ def find_references_by(resource:, property:) # (see Valkyrie::Persistence::Memory::QueryService#find_inverse_references_by) def find_inverse_references_by(resource:, property:) + validate_id resource.id Valkyrie::Persistence::Solr::Queries::FindInverseReferencesQuery.new(resource: resource, property: property, connection: connection, resource_factory: resource_factory).run end diff --git a/lib/valkyrie/specs/shared_specs/queries.rb b/lib/valkyrie/specs/shared_specs/queries.rb index 9c3cd10eb..2dcb701c8 100644 --- a/lib/valkyrie/specs/shared_specs/queries.rb +++ b/lib/valkyrie/specs/shared_specs/queries.rb @@ -153,18 +153,27 @@ class SecondResource < Valkyrie::Resource end describe ".find_inverse_references_by" do - it "returns everything which references the given resource by the given property" do - parent = persister.save(resource: resource_class.new) - child = persister.save(resource: resource_class.new(a_member_of: [parent.id])) - persister.save(resource: resource_class.new) - persister.save(resource: SecondResource.new) + context "when the resource is saved" do + it "returns everything which references the given resource by the given property" do + parent = persister.save(resource: resource_class.new) + child = persister.save(resource: resource_class.new(a_member_of: [parent.id])) + persister.save(resource: resource_class.new) + persister.save(resource: SecondResource.new) + + expect(query_service.find_inverse_references_by(resource: parent, property: :a_member_of).map(&:id).to_a).to eq [child.id] + end + it "returns an empty array if there are none" do + parent = persister.save(resource: resource_class.new) - expect(query_service.find_inverse_references_by(resource: parent, property: :a_member_of).map(&:id).to_a).to eq [child.id] + expect(query_service.find_inverse_references_by(resource: parent, property: :a_member_of).to_a).to eq [] + end end - it "returns an empty array if there are none" do - parent = persister.save(resource: resource_class.new) + context "when the resource is not saved" do + it "raises an error" do + parent = resource_class.new - expect(query_service.find_inverse_references_by(resource: parent, property: :a_member_of).to_a).to eq [] + expect { query_service.find_inverse_references_by(resource: parent, property: :a_member_of).to_a }.to raise_error ArgumentError + end end end