Skip to content

Commit

Permalink
Validate resource id
Browse files Browse the repository at this point in the history
  • Loading branch information
dlpierce committed Feb 27, 2018
1 parent 5f2cb2e commit 9007504
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/valkyrie/persistence/fedora/query_service.rb
Expand Up @@ -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) }
Expand Down
2 changes: 2 additions & 0 deletions lib/valkyrie/persistence/memory/query_service.rb
Expand Up @@ -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<Valkyrie::Resource>] 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)
Expand Down
1 change: 1 addition & 0 deletions lib/valkyrie/persistence/postgres/query_service.rb
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/valkyrie/persistence/solr/query_service.rb
Expand Up @@ -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

Expand Down
27 changes: 18 additions & 9 deletions lib/valkyrie/specs/shared_specs/queries.rb
Expand Up @@ -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

Expand Down

0 comments on commit 9007504

Please sign in to comment.