Skip to content

Commit

Permalink
Merge pull request #6285 from samvera/file-set-presenter-specs
Browse files Browse the repository at this point in the history
port most FileSetPresenter specs to Valkyrie
  • Loading branch information
dlpierce authored Sep 26, 2023
2 parents f14958e + b7a3c08 commit 0a611e2
Show file tree
Hide file tree
Showing 11 changed files with 240 additions and 197 deletions.
39 changes: 36 additions & 3 deletions app/models/hyrax/file_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,42 @@ def self.model_name(name_class: Hyrax::Name)
self.characterization_proxy = Hyrax.config.characterization_proxy

attribute :file_ids, Valkyrie::Types::Array.of(Valkyrie::Types::ID) # id for FileMetadata resources
attribute :thumbnail_id, Valkyrie::Types::ID.optional # id for FileMetadata resource
attribute :original_file_id, Valkyrie::Types::ID.optional # id for FileMetadata resource
attribute :extracted_text_id, Valkyrie::Types::ID.optional # id for FileMetadata resource

# @return [Hyrax::FileMetadata, nil]
def original_file
Hyrax.custom_queries.find_original_file(file_set: self)
rescue Valkyrie::Persistence::ObjectNotFoundError
nil
end

# @return [Valkyrie::ID, nil]
def original_file_id
original_file&.id
end

# @return [Hyrax::FileMetadata, nil]
def thumbnail
Hyrax.custom_queries.find_thumbnail(file_set: self)
rescue Valkyrie::Persistence::ObjectNotFoundError
nil
end

# @return [Valkyrie::ID, nil]
def thumbnail_id
thumbnail&.id
end

# @return [Hyrax::FileMetadata, nil]
def extracted_text
Hyrax.custom_queries.find_extracted_text(file_set: self)
rescue Valkyrie::Persistence::ObjectNotFoundError
nil
end

# @return [Valkyrie::ID, nil]
def extracted_text_id
extracted_text&.id
end

##
# @return [Valkyrie::ID]
Expand Down
26 changes: 0 additions & 26 deletions app/services/hyrax/valkyrie_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,7 @@ def version_upload(file_set:, io:, user:)
# @return [Hyrax::FileSet] updated file set
def add_file_to_file_set(file_set:, file_metadata:, user:)
file_set.file_ids << file_metadata.id
set_file_use_ids(file_set, file_metadata)

Hyrax.persister.save(resource: file_set)
Hyrax.publisher.publish('object.membership.updated', object: file_set, user: user)
end

private

# @api private
# @param [Hyrax::FileSet] file_set the file set to add to
# @param [Hyrax::FileMetadata] file_metadata the metadata object representing
# the file to add
# @return [void]
def set_file_use_ids(file_set, file_metadata)
file_metadata.pcdm_use.each do |type|
case type
when Hyrax::FileMetadata::Use::ORIGINAL_FILE
file_set.original_file_id = file_metadata.id
when Hyrax::FileMetadata::Use::THUMBNAIL
file_set.thumbnail_id = file_metadata.id
when Hyrax::FileMetadata::Use::EXTRACTED_TEXT
file_set.extracted_text_id = file_metadata.id
when Hyrax::FileMetadata::Use::SERVICE_FILE
# do nothing
else
Hyrax.logger.warn "Unknown file use #{file_metadata.type} specified for #{file_metadata.file_identifier}"
end
end
end
end
27 changes: 20 additions & 7 deletions lib/hyrax/specs/shared_specs/hydra_works.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
end
end

RSpec.shared_examples 'a Hyrax::FileSet' do
RSpec.shared_examples 'a Hyrax::FileSet', valkyrie_adapter: :test_adapter do
subject(:fileset) { described_class.new }
let(:adapter) { Valkyrie::MetadataAdapter.find(:test_adapter) }
let(:persister) { adapter.persister }
Expand All @@ -275,12 +275,10 @@
end

context 'with files' do
let(:file_class) { Hyrax::FileMetadata }
let(:files) do
[file_class.new, file_class.new, file_class.new]
.map! { |f| persister.save(resource: f) }
end

let(:original_file) { FactoryBot.valkyrie_create :hyrax_file_metadata, :original_file }
let(:thumbnail) { FactoryBot.valkyrie_create :hyrax_file_metadata, :thumbnail }
let(:extracted_text) { FactoryBot.valkyrie_create :hyrax_file_metadata, :extracted_text }
let(:files) { [original_file, thumbnail, extracted_text] }
let(:file_ids) { files.map(&:id) }

before { fileset.file_ids = file_ids }
Expand All @@ -301,6 +299,21 @@
expect { fileset.file_ids << file_ids.first }
.not_to change { query_service.custom_queries.find_files(file_set: fileset) }
end

it 'returns an original_file' do
expect(fileset.original_file).to eq original_file
expect(fileset.original_file_id).to eq original_file.id
end

it 'returns a thumbnail' do
expect(fileset.thumbnail).to eq thumbnail
expect(fileset.thumbnail_id).to eq thumbnail.id
end

it 'returns an extracted_text' do
expect(fileset.extracted_text).to eq extracted_text
expect(fileset.extracted_text_id).to eq extracted_text.id
end
end
end
end
6 changes: 6 additions & 0 deletions lib/wings/active_fedora_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def add_access_control_attributes(af_object)

# for files, add attributes to metadata_node, plus some other work
def add_file_attributes(af_object)
add_file_uri(af_object)
converted_attrs = normal_attributes
pcdm_use = converted_attrs.delete(:pcdm_use)
af_object.metadata_node.attributes = converted_attrs
Expand All @@ -180,6 +181,11 @@ def add_file_attributes(af_object)
af_object.mime_type = resource.mime_type
end

def add_file_uri(af_object)
file_uri = Hyrax.storage_adapter.fedora_identifier(id: resource.file_identifier)
af_object.uri = file_uri.to_s if af_object.uri.to_s.blank? && file_uri.to_s.present?
end

def perform_lease_conversion(af_object:, resource:)
# TODO(#6134): af_object.lease.class has the same name as resource.lease.class; however, each class has a different object_id
# so a type mismatch happens. the code below coerces the one object into the other
Expand Down
14 changes: 13 additions & 1 deletion spec/factories/hyrax_file_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
##
# Use this factory for FileMetadata for Files in valkyrie.
FactoryBot.define do
factory :hyrax_file_metadata, class: 'Hyrax::FileMetadata' do
factory :hyrax_file_metadata, class: 'Hyrax::FileMetadata', aliases: [:file_metadata] do
transient do
use { nil }
visibility_setting { nil }
Expand Down Expand Up @@ -32,6 +32,18 @@
end
end

trait :original_file do
use { Hyrax::FileMetadata::Use.uri_for(use: :original_file) }
end

trait :thumbnail do
use { Hyrax::FileMetadata::Use.uri_for(use: :thumbnail_file) }
end

trait :extracted_text do
use { Hyrax::FileMetadata::Use.uri_for(use: :extracted_file) }
end

trait :image do
mime_type { 'image/png' }
end
Expand Down
7 changes: 2 additions & 5 deletions spec/factories/hyrax_file_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,11 @@
.assign_access_for(visibility: evaluator.visibility_setting)
end
file_set.file_ids = evaluator.files.map(&:id) if evaluator.files
file_set.original_file_id = evaluator.original_file.id if evaluator.original_file
file_set.extracted_text_id = evaluator.extracted_text.id if evaluator.extracted_text
file_set.thumbnail_id = evaluator.thumbnail.id if evaluator.thumbnail

file_set.permission_manager.edit_groups = evaluator.edit_groups
file_set.permission_manager.edit_users = evaluator.edit_users
file_set.permission_manager.read_users = evaluator.read_users
file_set.permission_manager.read_users = evaluator.read_groups
file_set.permission_manager.read_groups = evaluator.read_groups
end

after(:create) do |file_set, evaluator|
Expand All @@ -44,7 +41,7 @@
file_set.permission_manager.edit_groups = evaluator.edit_groups
file_set.permission_manager.edit_users = evaluator.edit_users
file_set.permission_manager.read_users = evaluator.read_users
file_set.permission_manager.read_users = evaluator.read_groups
file_set.permission_manager.read_groups = evaluator.read_groups

file_set.permission_manager.acl.save

Expand Down
4 changes: 4 additions & 0 deletions spec/factories/uploaded_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
factory :uploaded_file, class: Hyrax::UploadedFile do
user
file { File.open('spec/fixtures/image.jp2') }

trait :audio do
file { File.open('spec/fixtures/sample_mpeg4.mp4') }
end
end
end
1 change: 1 addition & 0 deletions spec/indexers/hyrax/valkyrie_file_set_indexer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
allow(Hyrax.custom_queries).to receive(:find_original_file).with(file_set: file_set).and_return(mock_file)
allow(Hyrax.custom_queries).to receive(:find_file_metadata_by).with(id: file_set.original_file_id).and_return(mock_file)
allow(Hyrax.custom_queries).to receive(:find_thumbnail).with(file_set: file_set).and_return(mock_thumbnail)
allow(Hyrax.custom_queries).to receive(:find_extracted_text).with(file_set: file_set).and_return(mock_text)
allow(mock_file).to receive(:file_name).and_return(file_name)
end
subject { indexer.generate_solr_document }
Expand Down
Loading

0 comments on commit 0a611e2

Please sign in to comment.