-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from scientist-softserv/upload-actor-i7
Refactor IIIF Print Upload Actor
- Loading branch information
Showing
6 changed files
with
221 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...jobs/iiif_print/create_issue_pages_job.rb → app/jobs/iiif_print/create_pages_job.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module IiifPrint | ||
module IiifPrintBehavior | ||
# adds IIIF Print behavior to an object | ||
def split_pdf | ||
true | ||
end | ||
end | ||
end |
188 changes: 141 additions & 47 deletions
188
spec/actors/iiif_print/actors/iiif_print_upload_actor_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,163 @@ | ||
require 'faraday' | ||
require 'spec_helper' | ||
require 'misc_shared' | ||
# TODO: revisit commented out spec code which belongs in a feature or CreatePagesJob spec | ||
# require 'faraday' | ||
# require 'misc_shared' | ||
|
||
RSpec.describe IiifPrint::Actors::IiifPrintUploadActor, :perform_enqueued do | ||
include_context 'shared setup' | ||
|
||
let(:issue) { build(:newspaper_issue) } | ||
RSpec.describe IiifPrint::Actors::IiifPrintUploadActor do # , :perform_enqueued do | ||
let(:work) { build(:newspaper_issue) } | ||
let(:ability) { build(:ability) } | ||
let(:uploaded_pdf_file) { create(:uploaded_pdf_file) } | ||
let(:uploaded_file_ids) { [uploaded_pdf_file.id] } | ||
let(:uploaded_txt_file) { create(:uploaded_txt_file) } | ||
let(:uploaded_file_ids) { [uploaded_pdf_file.id, uploaded_txt_file.id] } | ||
let(:attributes) { { title: ['foo'], uploaded_files: uploaded_file_ids } } | ||
let(:terminator) { Hyrax::Actors::Terminator.new } | ||
let(:no_pdf_attributes) { { title: ['foo'], uploaded_files: [] } } | ||
|
||
# environment with uploads: | ||
let(:env) { Hyrax::Actors::Environment.new(issue, ability, attributes) } | ||
let(:with_pdf_env) { Hyrax::Actors::Environment.new(work, ability, attributes) } | ||
# environment with NO uploads: | ||
let(:edit_env) { Hyrax::Actors::Environment.new(work, ability, {}) } | ||
# environment with NO uploads: | ||
let(:edit_env) { Hyrax::Actors::Environment.new(issue, ability, {}) } | ||
let(:no_pdf_env) { Hyrax::Actors::Environment.new(work, ability, no_pdf_attributes) } | ||
|
||
let(:terminator) { Hyrax::Actors::Terminator.new } | ||
let(:middleware) do | ||
stack = ActionDispatch::MiddlewareStack.new.tap do |middleware| | ||
middleware.use described_class | ||
end | ||
stack.build(terminator) | ||
end | ||
|
||
let(:uploaded_issue) do | ||
middleware.public_send(:create, env) | ||
# return work, reloaded, because env.curation_concern will be stale after | ||
# running actor. | ||
NewspaperIssue.find(env.curation_concern.id) | ||
describe 'included in the actor stack' do | ||
let(:stack) { Hyrax::CurationConcern.actor_factory } | ||
it 'includes IiifPrint::UploadActor' do | ||
expect(stack.middlewares).to include(IiifPrint::Actors::IiifPrintUploadActor) | ||
end | ||
end | ||
|
||
let(:edited_issue) do | ||
middleware.public_send(:update, edit_env) | ||
NewspaperIssue.find(edit_env.curation_concern.id) | ||
context 'when work model includes IiifPrintBehavior' do | ||
describe ':create' do | ||
let(:mode) { :create } | ||
before do | ||
allow(work).to receive(:respond_to?).and_call_original | ||
allow(work).to receive(:respond_to?).with(:split_pdf).and_return true | ||
end | ||
context 'when work has a pdf file' do | ||
let(:mode_env) { with_pdf_env } | ||
it 'queues a IiifPrint::CreatePagesJob' do | ||
expect(IiifPrint::CreatePagesJob).to receive(:perform_later).with( | ||
work, | ||
["/app/samvera/hyrax-webapp/.internal_test_app/tmp/uploads/hyrax/uploaded_file/file/1/minimal-2-page.pdf"], | ||
"[email protected]", | ||
"admin_set/default" | ||
) | ||
expect(middleware.public_send(mode, mode_env)).to be true | ||
end | ||
end | ||
context 'when work has no pdf file' do | ||
let(:mode_env) { no_pdf_env } | ||
it 'does not queue IiifPrint::CreatePagesJob' do | ||
expect(IiifPrint::CreatePagesJob).not_to receive(:perform_later) | ||
expect(middleware.public_send(mode, mode_env)).to be true | ||
end | ||
end | ||
end | ||
|
||
describe ':update' do | ||
let(:mode) { :update } | ||
before do | ||
allow(work).to receive(:respond_to?).and_call_original | ||
allow(work).to receive(:respond_to?).with(:split_pdf).and_return true | ||
end | ||
context 'works is updated with no additional uploads' do | ||
let(:mode_env) { edit_env } | ||
it 'queues a IiifPrint::CreatePagesJob' do | ||
expect(IiifPrint::CreatePagesJob).not_to receive(:perform_later) | ||
expect(middleware.public_send(mode, mode_env)).to be true | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe "NewspaperIssue upload of PDF" do | ||
do_now_jobs = [ | ||
IiifPrint::CreateIssuePagesJob, | ||
IngestLocalFileJob, | ||
IngestJob | ||
] | ||
context 'when work model does not IiifPrintBehavior' do | ||
describe ':create' do | ||
let(:mode) { :create } | ||
before do | ||
allow(work).to receive(:respond_to?).and_call_original | ||
allow(work).to receive(:respond_to?).with(:split_pdf).and_return false | ||
end | ||
context 'when work has a pdf file' do | ||
let(:mode_env) { with_pdf_env } | ||
it 'queues a IiifPrint::CreatePagesJob' do | ||
expect(IiifPrint::CreatePagesJob).not_to receive(:perform_later) | ||
expect(middleware.public_send(mode, mode_env)).to be true | ||
end | ||
end | ||
context 'when work has no pdf file' do | ||
let(:mode_env) { no_pdf_env } | ||
it 'does not queue IiifPrint::CreatePagesJob' do | ||
expect(IiifPrint::CreatePagesJob).not_to receive(:perform_later) | ||
expect(middleware.public_send(mode, mode_env)).to be true | ||
end | ||
end | ||
end | ||
|
||
# we over-burden one example, because sadly RSpec does not do well with | ||
# shared state across examples (without use of `before(:all)` which is | ||
# mutually exclusive with `let` in practice, and ruffles rubocop's | ||
# overzealous sense of moral duty, speaking of which: | ||
it "creates child pages for issue", perform_enqueued: do_now_jobs do | ||
pages = uploaded_issue.ordered_pages | ||
expect(pages.size).to eq 2 | ||
page = pages[0] | ||
# Page needs correct admin set: | ||
expect(page.admin_set_id).to eq 'admin_set/default' | ||
file_sets = page.members.select { |v| v.class == FileSet } | ||
expect(file_sets.size).to eq 1 | ||
files = file_sets[0].files | ||
url = files[0].uri.to_s | ||
# fetch the thing from Fedora Commons: | ||
response = Faraday.get(url) | ||
stored_size = response.body.length | ||
expect(stored_size).to be > 0 | ||
# expect that subsequent edits of same issue (run though update | ||
# method of actor stack) do not duplicate pages (verify by count): | ||
expect(edited_issue.id).to eq uploaded_issue.id | ||
pages = edited_issue.ordered_pages | ||
expect(pages.size).to eq 2 # still the same page count | ||
describe ':update' do | ||
let(:mode) { :update } | ||
before do | ||
allow(work).to receive(:respond_to?).and_call_original | ||
allow(work).to receive(:respond_to?).with(:split_pdf).and_return false | ||
end | ||
context 'works is updated with no additional uploads' do | ||
let(:mode_env) { edit_env } | ||
it 'queues a IiifPrint::CreatePagesJob' do | ||
expect(IiifPrint::CreatePagesJob).not_to receive(:perform_later) | ||
expect(middleware.public_send(mode, mode_env)).to be true | ||
end | ||
end | ||
end | ||
end | ||
|
||
# let(:uploaded_work) do | ||
# middleware.public_send(:create, env) | ||
# # return work, reloaded, because env.curation_concern will be stale after | ||
# # running actor. | ||
# NewspaperIssue.find(env.curation_concern.id) | ||
# end | ||
# let(:edited_work) do | ||
# middleware.public_send(:update, edit_env) | ||
# NewspaperIssue.find(edit_env.curation_concern.id) | ||
# end | ||
|
||
# describe "NewspaperIssue upload of PDF" do | ||
# do_now_jobs = [ | ||
# IiifPrint::CreatePagesJob, | ||
# IngestLocalFileJob, | ||
# IngestJob | ||
# ] | ||
|
||
# # we over-burden one example, because sadly RSpec does not do well with | ||
# # shared state across examples (without use of `before(:all)` which is | ||
# # mutually exclusive with `let` in practice, and ruffles rubocop's | ||
# # overzealous sense of moral duty, speaking of which: | ||
# xit "creates child pages for issue", perform_enqueued: do_now_jobs do | ||
# pages = uploaded_issue.ordered_pages | ||
# expect(pages.size).to eq 2 | ||
# page = pages[0] | ||
# # Page needs correct admin set: | ||
# expect(page.admin_set_id).to eq 'admin_set/default' | ||
# file_sets = page.members.select { |v| v.class == FileSet } | ||
# expect(file_sets.size).to eq 1 | ||
# files = file_sets[0].files | ||
# url = files[0].uri.to_s | ||
# # fetch the thing from Fedora Commons: | ||
# response = Faraday.get(url) | ||
# stored_size = response.body.length | ||
# expect(stored_size).to be > 0 | ||
# # expect that subsequent edits of same issue (run though update | ||
# # method of actor stack) do not duplicate pages (verify by count): | ||
# expect(edited_issue.id).to eq uploaded_issue.id | ||
# pages = edited_issue.ordered_pages | ||
# expect(pages.size).to eq 2 # still the same page count | ||
# end | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :uploaded_txt_file, class: Hyrax::UploadedFile do | ||
initialize_with do | ||
base = File.join(IiifPrint::GEM_PATH, 'spec', 'fixtures', 'files') | ||
file_path = File.join(base, 'ndnp-sample1-txt.txt') | ||
new(file: File.open(file_path), user: create(:user)) | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
spec/models/concerns/iiif_print/iiif_print_behavior_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'spec_helper' | ||
RSpec.describe IiifPrint::IiifPrintBehavior do | ||
describe "including_this_module" do | ||
before do | ||
class PrintWork < ActiveFedora::Base | ||
include IiifPrint::IiifPrintBehavior | ||
end | ||
end | ||
let(:klass) { Class.new } | ||
subject { PrintWork.new } | ||
|
||
describe 'split_pdf' do | ||
it 'is true' do | ||
expect(subject.split_pdf).to be true | ||
end | ||
end | ||
end | ||
end |