Skip to content

Commit

Permalink
allow for extensions of valkyrie indexer's to_solr that depends on re…
Browse files Browse the repository at this point in the history
…source custom metadata

Fixes #4464

Allow shared spec testing of indexers with customizations to #to_solr that depend on properties defined in the resource class that the indexer indexes.

Update shared indexers spec to have a valid resource passed in to the shared spec.  The shared spec sets values it depends on in a before block.
  • Loading branch information
elrayle committed Jul 21, 2020
1 parent 505b135 commit cb3d5de
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 26 deletions.
54 changes: 36 additions & 18 deletions lib/hyrax/specs/shared_specs/indexers.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true

RSpec.shared_examples 'a Hyrax::Resource indexer' do
subject(:indexer) { indexer_class.new(resource: resource) }
let(:ids) { ['id1', 'id2'] }
let(:resource) { Hyrax::Resource.new(alternate_ids: ids) }
subject(:indexer) { indexer_class.new(resource: the_resource) }
let(:ids) { ['id1', 'id2'] }
let(:the_resource) { defined?(resource) ? resource : Hyrax::Resource.new }

describe '#to_solr' do
before { the_resource.alternate_ids = ids }
it 'indexes alternate_ids' do
expect(indexer.to_solr)
.to include(alternate_ids_sm: a_collection_containing_exactly(*ids))
Expand All @@ -14,19 +15,26 @@
end

RSpec.shared_examples 'a permission indexer' do
subject(:indexer) { indexer_class.new(resource: resource) }
subject(:indexer) { indexer_class.new(resource: the_resource) }
let(:edit_groups) { [:managers] }
let(:edit_users) { [FactoryBot.create(:user)] }
let(:read_users) { [FactoryBot.create(:user)] }

let(:resource) do
FactoryBot.valkyrie_create(:hyrax_work, :public,
read_users: read_users,
edit_groups: edit_groups,
edit_users: edit_users)
let(:the_resource) do
if defined?(resource)
resource = Hyrax.persister.save(resource: resource)
resource.permission_manager.edit_groups = edit_groups
resource.permission_manager.edit_users = edit_users
resource.permission_manager.read_users = read_users
resource.permission_manager.acl.save
else
FactoryBot.valkyrie_create(:hyrax_work, :public,
read_users: read_users,
edit_groups: edit_groups,
edit_users: edit_users)
end
end


describe '#to_solr' do
it 'indexes read permissions' do
expect(indexer.to_solr)
Expand All @@ -43,16 +51,24 @@
end

RSpec.shared_examples 'a visibility indexer' do
subject(:indexer) { indexer_class.new(resource: resource) }
let(:resource) { FactoryBot.build(:hyrax_work) }
subject(:indexer) { indexer_class.new(resource: the_resource) }
let(:the_resource) { defined?(resource) ? resource : FactoryBot.build(:hyrax_work) }

describe '#to_solr' do
it 'indexes visibility' do
expect(indexer.to_solr).to include(visibility_ssi: 'restricted')
end

context 'when resource is public' do
let(:resource) { FactoryBot.valkyrie_create(:hyrax_work, :public) }
let(:the_resource) do
if defined?(resource)
resource = Hyrax.persister.save(resource: resource)
Hyrax::VisibilityWriter.new(resource: resource).assign_access_for(visibility: :public)
resource
else
FactoryBot.valkyrie_create(:hyrax_work, :public)
end
end

it 'indexes as open' do
expect(indexer.to_solr).to include(visibility_ssi: 'open')
Expand All @@ -62,8 +78,8 @@
end

RSpec.shared_examples 'a Basic metadata indexer' do
subject(:indexer) { indexer_class.new(resource: resource) }
let(:resource) { resource_class.new(**attributes) }
subject(:indexer) { indexer_class.new(resource: the_resource) }
let(:the_resource) { defined?(resource) ? resource : resource_class.new }

let(:attributes) do
{
Expand All @@ -79,9 +95,10 @@
end

describe '#to_solr' do
before { attributes.each { |k, v| the_resource.set_value(k, v) } }
it 'indexes basic metadata' do
expect(indexer.to_solr)
.to include(keyword_sim: a_collection_containing_exactly(*attributes[:keyword]),
.to include(keyword_sim: a_collection_containing_exactly(*attributes[:keyword]),
subject_tesim: a_collection_containing_exactly(*attributes[:subject]),
subject_sim: a_collection_containing_exactly(*attributes[:subject]))
end
Expand Down Expand Up @@ -109,11 +126,12 @@
end

RSpec.shared_examples 'a Core metadata indexer' do
subject(:indexer) { indexer_class.new(resource: resource) }
subject(:indexer) { indexer_class.new(resource: the_resource) }
let(:titles) { ['Comet in Moominland', 'Finn Family Moomintroll'] }
let(:resource) { Hyrax::Work.new(title: titles) }
let(:the_resource) { defined?(resource) ? resource : Hyrax::Work.new }

describe '#to_solr' do
before { the_resource.title = titles }
it 'indexes title as text' do
expect(indexer.to_solr)
.to include(title_tesim: a_collection_containing_exactly(*titles))
Expand Down
1 change: 1 addition & 0 deletions spec/indexers/hyrax/resource_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'hyrax/specs/shared_specs'

RSpec.describe Hyrax::ResourceIndexer do
# let(:resource_class) { Hyrax::Resource }
let(:indexer_class) do
Class.new(Hyrax::ValkyrieIndexer) do
include Hyrax::ResourceIndexer
Expand Down
7 changes: 6 additions & 1 deletion spec/indexers/hyrax/valkyrie_collection_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
require 'hyrax/specs/shared_specs'

RSpec.describe Hyrax::ValkyrieCollectionIndexer do
subject(:indexer) { described_class.new(resource: resource) }
let(:resource_class) do
Class.new(Hyrax::Resource) do
# include ::Hyrax::CollectionBehavior # TODO: How is this represented in the Valkyrie::Resource version of a Collection?
include Hyrax::Schema(:basic_metadata)
end
end
let(:indexer_class) { described_class }

it_behaves_like 'a Hyrax::Resource indexer'
Expand Down
12 changes: 10 additions & 2 deletions spec/indexers/hyrax/valkyrie_work_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
require 'hyrax/specs/shared_specs'

RSpec.describe Hyrax::ValkyrieWorkIndexer do
let(:indexer) { described_class.new(resource: resource) }
let(:resource) { FactoryBot.valkyrie_create(:hyrax_work) }
let(:resource_class) { Hyrax::Work }
let(:indexer_class) { described_class }

it_behaves_like 'a Hyrax::Resource indexer'
Expand All @@ -21,4 +20,13 @@

it_behaves_like 'a Basic metadata indexer'
end

context 'when extending with custom metadata' do
let(:resource) { Hyrax::Test::BookResource.new(isbn: '978-3-16-148410-0', publisher: 'The Publisher', title: 'A title') }
let(:indexer_class) { Hyrax::Test::BookResourceIndexer }

it_behaves_like 'a Hyrax::Resource indexer'
it_behaves_like 'a permission indexer'
it_behaves_like 'a visibility indexer'
end
end
20 changes: 15 additions & 5 deletions spec/support/book_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ module Test
# Use this for testing valkyrie models generically, with Hyrax assumptions
# but no PCDM modelling behavior.
class BookResource < Hyrax::Resource
attribute :author, Valkyrie::Types::String
attribute :created, Valkyrie::Types::Date
attribute :isbn, Valkyrie::Types::String
attribute :pubisher, Valkyrie::Types::String
attribute :title, Valkyrie::Types::String
attribute :author, Valkyrie::Types::String
attribute :created, Valkyrie::Types::Date
attribute :isbn, Valkyrie::Types::String
attribute :publisher, Valkyrie::Types::String
attribute :title, Valkyrie::Types::String
end

class Book < ActiveFedora::Base
Expand All @@ -21,6 +21,16 @@ class Book < ActiveFedora::Base
property :publisher, predicate: ::RDF::URI('http://example.com/ns/publisher')
property :title, predicate: ::RDF::URI("http://example.com/ns/title")
end

class BookResourceIndexer < Hyrax::ValkyrieWorkIndexer
def to_solr
super.tap do |solr_doc|
solr_doc['title_ssim'] = resource.title
solr_doc['isbn_ssim'] = resource.isbn
solr_doc['publisher_ssim'] = resource.publisher
end
end
end
end
end

Expand Down

0 comments on commit cb3d5de

Please sign in to comment.