-
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 #239 from scientist-softserv/i233-rework_relations…
…hips_job Rework relationships job
- Loading branch information
Showing
5 changed files
with
166 additions
and
41 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
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
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 |
---|---|---|
@@ -1,17 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'misc_shared' | ||
|
||
RSpec.describe IiifPrint::Jobs::CreateRelationshipsJob do | ||
# TODO: add specs | ||
let(:parent) { WorkWithIiifPrintConfig.new(title: ['required title']) } | ||
let(:my_user) { build(:user) } | ||
let(:parent_model) { WorkWithIiifPrintConfig } | ||
let(:child_model) { WorkWithIiifPrintConfig } | ||
module IiifPrint::Jobs | ||
RSpec.describe CreateRelationshipsJob, type: :job do | ||
let(:create_relationships_job) { described_class.new } | ||
|
||
let(:parent_model) { WorkWithIiifPrintConfig.to_s } | ||
let(:child_model) { WorkWithIiifPrintConfig.to_s } | ||
let(:parent_record) { WorkWithIiifPrintConfig.new(title: ['required title']) } | ||
let(:child_record1) { WorkWithIiifPrintConfig.new(title: ["Child of #{parent_record.id} page 01"]) } | ||
let(:child_record2) { WorkWithIiifPrintConfig.new(title: ["Child of #{parent_record.id} page 02"]) } | ||
let(:pending_rel1) { IiifPrint::PendingRelationship.new(parent_id: parent_record.id, child_title: "Child of #{parent_record.id} page 01", child_order: "Child of #{parent_record.id} page 01") } | ||
let(:pending_rel2) { IiifPrint::PendingRelationship.new(parent_id: parent_record.id, child_title: "Child of #{parent_record.id} page 02", child_order: "Child of #{parent_record.id} page 02") } | ||
|
||
describe '#perform' do | ||
before do | ||
allow(create_relationships_job).to receive(:acquire_lock_for).and_yield | ||
allow(create_relationships_job).to receive(:reschedule_job) | ||
allow(parent_record).to receive(:save!) | ||
|
||
parent_record.save | ||
pending_rel1.save | ||
pending_rel2.save | ||
end | ||
|
||
subject(:perform) do | ||
create_relationships_job.perform( | ||
parent_id: parent_record.id, | ||
parent_model: parent_model, | ||
child_model: child_model, | ||
retries: 0 | ||
) | ||
end | ||
|
||
context 'when adding a child work to a parent work' do | ||
before do | ||
child_record1.save | ||
child_record2.save | ||
end | ||
|
||
it 'assigns the child to the parent\'s #ordered_members' do | ||
perform | ||
expect(parent_record.reload.ordered_member_ids).to eq([child_record1.id, child_record2.id]) | ||
end | ||
|
||
it 'deletes the pending relationships' do | ||
expect { perform }.to change(IiifPrint::PendingRelationship, :count).by(-2) | ||
end | ||
|
||
it 'does not reschedule the job' do | ||
perform | ||
expect(create_relationships_job).not_to have_received(:reschedule_job) | ||
end | ||
end | ||
|
||
context 'when a relationship fails' do | ||
before do | ||
child_record1.save | ||
child_record2.save | ||
end | ||
|
||
before do | ||
expect_any_instance_of(CreateRelationshipsJob).to receive(:add_to_work).and_raise('error') | ||
end | ||
|
||
it 'does not save the parent' do | ||
expect { perform }.to raise_error(RuntimeError) | ||
expect(parent_record).not_to have_received(:save!) | ||
end | ||
|
||
it 'does not delete the pending relationships' do | ||
expect { perform }.to raise_error(RuntimeError) | ||
expect(IiifPrint::PendingRelationship.where(parent_id: parent_record.id).count).to eq(2) | ||
end | ||
end | ||
|
||
context 'when any child record is not found' do | ||
let(:child_record2) { nil } | ||
|
||
before do | ||
child_record1.save | ||
end | ||
|
||
let(:subject) { described_class.perform(user: my_user, parent_id: parent.id, parent_model: parent_model, child_model: child_model) } | ||
it 'does not save the parent' do | ||
perform | ||
expect(parent_record).not_to have_received(:save!) | ||
end | ||
|
||
describe '#perform' do | ||
xit 'loads all child work ids into ordered_members' do | ||
it 'reschedules the job' do | ||
perform | ||
expect(create_relationships_job).to have_received(:reschedule_job) | ||
end | ||
end | ||
end | ||
end | ||
end |