Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test coverage for Hydra::Works::GenericFile::Derivatives. #183

Merged
merged 4 commits into from
Aug 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in hydra-works.gemspec
gemspec

gem 'activefedora-aggregation', github: 'projecthydra-labs/activefedora-aggregation', ref: 'master'
gem 'hydra-pcdm', github: 'projecthydra-labs/hydra-pcdm', ref: 'master'
gem 'hydra-derivatives', github: 'projecthydra/hydra-derivatives', ref: '7a8377c'
gem 'active-fedora', github: 'projecthydra/active_fedora', ref: '5466c6'
gem 'slop', '~> 3.6' # For byebug

unless ENV['CI']
Expand Down
6 changes: 3 additions & 3 deletions hydra-works.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "hydra-pcdm", "~> 0.0.1"
spec.add_dependency "hydra-derivatives", "~> 1.1.0"
spec.add_dependency "activefedora-aggregation", "~> 0.3"
spec.add_dependency "hydra-pcdm", "~> 0.1"
spec.add_dependency "hydra-derivatives", "~> 2.0"
spec.add_dependency "activefedora-aggregation", "~> 0.4"
spec.add_dependency "active-fedora", "~> 9.2"

spec.add_development_dependency "bundler", "~> 1.7"
Expand Down
1 change: 0 additions & 1 deletion lib/hydra/works.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'active_fedora/aggregation'
require 'hydra/pcdm'
require 'hydra/derivatives'
require 'hydra/works/processor'

module Hydra
module Works
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Hydra::Works::GenericFile::ContainedFiles
extend ActiveSupport::Concern

# HydraWorks supports only one each of original_file, thumbnail, and extracted_text. However
# you are free to add an unlimited number of addtition types such as different resolutions
# you are free to add an unlimited number of additional types such as different resolutions
# of images, different derivatives, etc, and use any established vocabulary you choose.

# TODO: se PCDM vocab class when projecthydra-labs/hydra-pcdm#80 is merged
Expand All @@ -11,5 +11,4 @@ module Hydra::Works::GenericFile::ContainedFiles
directly_contains_one :thumbnail, through: :files, type: ::RDF::URI("http://pcdm.org/use#ThumbnailImage"), class_name: "Hydra::PCDM::File"
directly_contains_one :extracted_text, through: :files, type: ::RDF::URI("http://pcdm.org/use#ExtractedText"), class_name: "Hydra::PCDM::File"
end

end
22 changes: 9 additions & 13 deletions lib/hydra/works/models/concerns/generic_file/derivatives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ module Derivatives
included do
include Hydra::Derivatives

# Sets output_file_service to PersistDerivative instead of default Hydra::Derivatives::PersistBasicContainedOutputFileService
Hydra::Derivatives.output_file_service = Hydra::Works::PersistDerivative

# This was taken directly from Sufia's GenericFile::Derivatives and modified to exclude any processing that modified the original file
makes_derivatives do |obj|
case obj.mime_type
case obj.original_file.mime_type
when *pdf_mime_types
obj.transform_file :original_file, { thumbnail: { format: 'jpg', size: '338x493', datastream: 'thumbnail' } }, processor: :work
obj.transform_file :original_file, thumbnail: { format: 'jpg', size: '338x493' }
when *office_document_mime_types
obj.transform_file :original_file, { thumbnail: { format: 'jpg', size: '200x150>', datastream: 'thumbnail' } }, processor: :document
obj.transform_file :original_file, { thumbnail: { format: 'jpg', size: '200x150>' } }, processor: :document
when *video_mime_types
obj.transform_file :original_file, { thumbnail: { format: 'jpg', datastream: 'thumbnail' } }, processor: :video
obj.transform_file :original_file, { thumbnail: { format: 'jpg' } }, processor: :video
when *image_mime_types
obj.transform_file :original_file, { thumbnail: { format: 'jpg', size: '200x150>', datastream: 'thumbnail' } }, processor: :work
obj.transform_file :original_file, thumbnail: { format: 'jpg', size: '200x150>' }
end
end

end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this overridden method still need to be here? (Will the work @grosscol is doing in hydra-derivatives obviate the need for this method override?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I'm looking at having the returned objects from derivatives have a mime_type method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


# This bit was not in Sufia, but needs to be here for derivatives to work
# TODO: delegate mime type somewhere else?
def mime_type
return nil unless self.original_file
self.original_file.mime_type
end

end
end
end
9 changes: 0 additions & 9 deletions lib/hydra/works/processor.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,4 @@ def update(*)
end
end
end

15 changes: 6 additions & 9 deletions lib/hydra/works/services/generic_file/persist_derivative.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ class PersistDerivative < Hydra::Derivatives::PersistOutputFileService
# Persists a derivative to a GenericFile
# This Service conforms to the signature of `Hydra::Derivatives::PersistOutputFileService`.
# The purpose of this Service is for use as an alternative to the default Hydra::Derivatives::PersistOutputFileService. It's necessary because the default behavior in Hydra::Derivatives assumes that you're using LDP Basic Containment. Hydra::Works::GenericFiles use IndirectContainment. This Service handles that case.
# This service will always update existing and does not do versioning of persisted files.
#
# @param [Hydra::Works::GenericFile::Base] object the file will be added to
# @param [#read] file the dervivative filestream optionally responds to :mime_type
# @option opts [RDF::URI or String] type URI for the RDF.type that identifies the file's role within the generic_file
# @option opts [Boolean] update_existing when set to false, always adds a new file. When set to true, performs a create_or_update
# @option opts [Boolean] versioning whether to create new version entries
# @param [#read or String] file the derivative filestream optionally responds to :mime_type
# @param [String] extract file type symbol (e.g. :thumbnail) from Hydra::Derivatives created destination_name

def self.call(object, file, opts={})
type = opts.fetch(:type, ::RDF::URI("http://pcdm.org/use#ServiceFile"))
update_existing = opts.fetch(:update_existing, true)
versioning = opts.fetch(:versioning, true)
Hydra::Works::AddFileToGenericFile.call(object, file, type, update_existing: update_existing, versioning: versioning)
def self.call(object, file, destination_name)
type = destination_name.gsub(/^original_file_/, '').to_sym
Hydra::Works::AddFileToGenericFile.call(object, file, type, update_existing: true, versioning: false)
end

end
Expand Down
Binary file added spec/fixtures/Example.ogg
Binary file not shown.
Binary file added spec/fixtures/charter.docx
Binary file not shown.
Binary file added spec/fixtures/countdown.avi
Binary file not shown.
Binary file added spec/fixtures/image.jp2
Binary file not shown.
Binary file added spec/fixtures/piano_note.wav
Binary file not shown.
Binary file added spec/fixtures/test5.mp3
Binary file not shown.
Binary file added spec/fixtures/world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 0 additions & 21 deletions spec/hydra/works/services/generic_file/persist_derivative_spec.rb

This file was deleted.

93 changes: 93 additions & 0 deletions spec/hydra/works/services/persist_derivatives_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
require 'spec_helper'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏


describe Hydra::Works::PersistDerivative do

describe 'thumbnail generation' do
before do
file = File.open(File.join(fixture_path, file_name), 'r')
Hydra::Works::UploadFileToGenericFile.call(generic_file, file)
allow_any_instance_of(Hydra::Works::GenericFile::Base).to receive(:mime_type).and_return(mime_type)
generic_file.save!
end

context 'with a video (.avi) file' do
let(:mime_type) { 'video/avi' }
let(:file_name) { 'countdown.avi' }
let(:generic_file) { Hydra::Works::GenericFile::Base.new }

it 'lacks a thumbnail' do
expect(generic_file.thumbnail).to be_nil
end

it 'generates a thumbnail derivative', unless: ENV['CI'] do
generic_file.create_derivatives
expect(generic_file.thumbnail).to have_content
expect(generic_file.thumbnail.mime_type).to eq('image/jpeg')
end
end

context 'with an image file' do
let(:mime_type) { 'image/png' }
let(:file_name) { 'world.png' }
let(:generic_file) { Hydra::Works::GenericFile::Base.new }

it 'lacks a thumbnail' do
expect(generic_file.thumbnail).to be_nil
end

it 'uses PersistDerivative service to generate a thumbnail derivative' do
generic_file.create_derivatives
expect(Hydra::Derivatives.output_file_service).to eq(Hydra::Works::PersistDerivative)
expect(generic_file.thumbnail).to have_content
expect(generic_file.thumbnail.mime_type).to eq('image/jpeg')
end
end

context 'with an audio (.wav) file' do
let(:mime_type) { 'audio/wav' }
let(:file_name) { 'piano_note.wav' }
let(:generic_file) { Hydra::Works::GenericFile::Base.new }

it 'lacks a thumbnail' do
expect(generic_file.thumbnail).to be_nil
end

it 'does not generate a thumbnail when derivatives are created', unless: ENV['CI'] do
generic_file.create_derivatives
expect(generic_file.thumbnail).to be_nil
end
end

context 'with an image (.jp2) file' do
let(:mime_type) { 'image/jp2' }
let(:file_name) { 'image.jp2' }
let(:generic_file) { Hydra::Works::GenericFile::Base.new }

it 'lacks a thumbnail' do
expect(generic_file.thumbnail).to be_nil
end

it 'generates a thumbnail on job run' do
generic_file.create_derivatives
expect(generic_file.thumbnail).to have_content
expect(generic_file.thumbnail.mime_type).to eq('image/jpeg')
end
end

context 'with an office document (.docx) file' do
let(:mime_type) { 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }
let(:file_name) { 'charter.docx' }
let(:generic_file) { Hydra::Works::GenericFile::Base.new }

it 'lacks a thumbnail' do
expect(generic_file.thumbnail).to be_nil
end

it 'generates a thumbnail on job run', unless: ENV['CI'] do
generic_file.create_derivatives
expect(generic_file.thumbnail).to have_content
expect(generic_file.thumbnail.mime_type).to eq('image/jpeg')
end
end
end
end