Skip to content

Commit

Permalink
File Set Specs
Browse files Browse the repository at this point in the history
Update specs based on derivative & file migration processing in Hyrax.
  • Loading branch information
LaRita Robinson committed Jun 6, 2024
1 parent 4c73698 commit d0b4177
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 46 deletions.
23 changes: 23 additions & 0 deletions spec/fixtures/derivatives/00-xml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<alto xmlns="http://www.loc.gov/standards/alto/ns-v2#">
<Description>
<MeasurementUnit>pixel</MeasurementUnit>
</Description>
<Layout>
<Page ID="ID1" PHYSICAL_IMG_NR="1" HEIGHT="3685" WIDTH="2457">
<PrintSpace HEIGHT="3685" WIDTH="2457" HPOS="0" VPOS="0">
<TextBlock ID="ID1a" HEIGHT="3685" WIDTH="2457" HPOS="0" VPOS="0">
<TextLine HEIGHT="3685" WIDTH="2457" HPOS="0" VPOS="0">
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
<String CONTENT=" &#10; &#10; &#10; &#10; &#10; &#10;&#10;" WIDTH="1616" HEIGHT="3128" HPOS="466" VPOS="276"></String>
</TextLine>
</TextBlock>
</PrintSpace>
</Page>
</Layout>
</alto>
Binary file added spec/fixtures/derivatives/b6-thumbnail.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
108 changes: 62 additions & 46 deletions spec/models/hyrax/file_set_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,59 +13,75 @@
its(:to_rdf_representation) { is_expected.to eq('FileSet') }
end

# given an existing AF FileSet
let(:af_file_set) do
fs = FileSet.create(creator: ['test'], title: ['file set test'])
path_to_file = 'spec/fixtures/csv/sample.csv'
file = File.open(path_to_file, 'rb')
Hydra::Works::AddFileToFileSet.call(fs, file, :original_file)
fs
end
context 'lazy migration' do
# given an existing AF FileSet
let(:af_file_set) do
fs = FileSet.create(creator: ['test'], title: ['file set test'], label: 'sample.csv')
path_to_file = 'spec/fixtures/csv/sample.csv'
file = File.open(path_to_file, 'rb')
Hydra::Works::AddFileToFileSet.call(fs, file, :original_file)
fs
end
let(:file_set_resource) { Hyrax.query_service.find_by(id: af_file_set.id) }
let(:paths) { ['spec/fixtures/derivatives/00-xml.xml', 'spec/fixtures/derivatives/b6-thumbnail.jpeg'] }
let(:storage_adapter) { Valkyrie::StorageAdapter.find(:derivatives_disk) }

# Because we're running a job, we need to specify a tenant
it "converts an AF FileSet to a Valkyrie::FileSet", :singletenant do
## Preamble to test a "Created in ActiveFedora FileSet"
expect { Hyrax.query_service.services.first.find_by(id: af_file_set.id) }.to raise_error(Valkyrie::Persistence::ObjectNotFoundError)
# We are lazyily migrating a FileSet to a Hyrax::FileSet
# thus it should comeback as a Hyrax::FileSet
expect(Hyrax.query_service.services.last.find_by(id: af_file_set.id)).to be_a(Hyrax::FileSet)
# Expect the goddess combo works as expected
file_set_resource = Hyrax.query_service.find_by(id: af_file_set.id)
expect(file_set_resource).to be_a(Hyrax::FileSet)
before do
# set up some derivatives
allow(Hyrax::DerivativePath).to receive(:derivatives_for_reference).and_return(paths)
allow(Hyrax::ValkyriePersistDerivatives).to receive(:fileset_for_directives).and_return(file_set_resource)
allow(Hyrax.publisher).to receive(:publish)
end

af_file_id = af_file_set.original_file.id
expect { Hyrax.query_service.services.first.find_by(id: af_file_id) }.to raise_error(Valkyrie::Persistence::ObjectNotFoundError)
# We should be able to find this "thing" in the ActiveFedora storage
expect(Hyrax.query_service.services.last.find_by(id: af_file_id)).to be_present
# Expect the goddess combo works as expected
expect(Hyrax.query_service.find_by(id: af_file_id)).to be_present
# Because we're running a job, we need to specify a tenant
it "converts an AF FileSet to a Valkyrie::FileSet", :singletenant do
## Preamble to test a "Created in ActiveFedora FileSet"
expect { Hyrax.query_service.services.first.find_by(id: af_file_set.id) }.to raise_error(Valkyrie::Persistence::ObjectNotFoundError)
# We are lazily migrating a FileSet to a Hyrax::FileSet
# thus it should comeback as a Hyrax::FileSet
expect(Hyrax.query_service.services.last.find_by(id: af_file_set.id)).to be_a(Hyrax::FileSet)
# Expect the goddess combo works as expected
expect(file_set_resource).to be_a(Hyrax::FileSet)

# The file is in Fedora!
expect(file_set_resource.original_file.file_identifier.id).to start_with("fedora://")
expect { Hyrax.query_service.services.first.find_by(id: af_file_set.id) }.to raise_error(Valkyrie::Persistence::ObjectNotFoundError)
# We should be able to find this "thing" in the ActiveFedora storage
expect(Hyrax.query_service.services.last.find_by(id: af_file_set.id)).to be_present
# Expect the goddess combo works as expected
expect(Hyrax.query_service.find_by(id: af_file_set.id)).to be_present

## Do the "migration" task, it will conditionally enqueue; if we don't
## process the queue the statements after this will fail.
perform_enqueued_jobs do
Hyrax.persister.save(resource: file_set_resource)
end
# The file is in Fedora!
expect(file_set_resource.original_file.file_identifier.id).to start_with("fedora://")

# We found it in Postgresql
converted_file_set = Hyrax.query_service.services.first.find_by(id: af_file_set.id)
expect(converted_file_set).to be_a(Hyrax::FileSet)
## Do the "migration" task: saving the resource triggers the file migrations.
# if we don't process the queue the statements after this will fail.
perform_enqueued_jobs do
Hyrax.persister.save(resource: file_set_resource)
end

# It's been converted to Postgresql
expect(Hyrax.query_service.services.first.find_by(id: af_file_id)).to be_a(Hyrax::FileMetadata)
# It's still there in ActiveFedora
expect(Hyrax.query_service.services.last.find_by(id: af_file_id)).to be_present
# Expect the goddess combo works as expected
expect(Hyrax.query_service.find_by(id: af_file_id)).to be_a(Hyrax::FileMetadata)
## Verify FileSet migration
# We found the file_set in Postgresql
converted_file_set = Hyrax.query_service.services.first.find_by(id: af_file_set.id)
expect(converted_file_set).to be_a(Hyrax::FileSet)

file_identifier_id = converted_file_set.original_file.file_identifier.id
# Verify that the original file is now on disk (e.g. where we write files in
# the test environment)
expect(file_identifier_id).to start_with("disk://#{Rails.root}")
## Verify File migration
af_file_id = af_file_set.original_file.id
# It's been converted to Valkyrie if we have a Hyrax::FileMetadata object.
expect(Hyrax.custom_queries.find_file_metadata_by(id: af_file_id)).to be_a(Hyrax::FileMetadata)

# Verify that the file actually exists there!
expect(File.exist?(file_identifier_id.sub("disk://", ""))).to be_truthy
file_identifier_id = converted_file_set.original_file.file_identifier.id
# Verify that the original file is now on disk (e.g. where we write files in
# the test environment)
expect(file_identifier_id).to start_with("disk://#{Rails.root}")

# Verify that the file actually exists there!
expect(File.exist?(file_identifier_id.sub("disk://", ""))).to be_truthy

# Verify that it's still in ActiveFedora
expect(af_file_set.original_file).to be_a(Hydra::PCDM::File)
expect(Hydra::PCDM::File.find(af_file_set.original_file.uri.to_s).content).to be_present

# Verify that we have migrated derivatives & original file... The file_set should have 3 FileMetadata objects.
expect(Hyrax.custom_queries.find_many_file_metadata_by_ids(ids: converted_file_set.file_ids).count).to eq(3)
end
end
end

0 comments on commit d0b4177

Please sign in to comment.