Skip to content

Commit

Permalink
optimize add_related_object tests for objects and collections
Browse files Browse the repository at this point in the history
Also fix typo in raised error for parent_collection in AddRelatedObjectToCollection
  • Loading branch information
elrayle committed Jul 9, 2015
1 parent 9b880c0 commit b621f94
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 110 deletions.
2 changes: 1 addition & 1 deletion lib/hydra/pcdm/services/collection/add_related_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AddRelatedObjectToCollection
# @return [Hydra::PCDM::Collection] the updated pcdm collection

def self.call( parent_collection, child_related_object )
raise ArgumentError, 'parent_collection must be a pcdm object' unless Hydra::PCDM.collection? parent_collection
raise ArgumentError, 'parent_collection must be a pcdm collection' unless Hydra::PCDM.collection? parent_collection
raise ArgumentError, 'child_related_object must be a pcdm object' unless Hydra::PCDM.object? child_related_object

# parent_collection.related_objects = parent_collection.related_objects.to_a + child_related_object
Expand Down
113 changes: 64 additions & 49 deletions spec/hydra/pcdm/services/collection/add_related_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,49 @@

describe Hydra::PCDM::AddRelatedObjectToCollection do

let(:subject) { Hydra::PCDM::Collection.create }
let(:subject) { Hydra::PCDM::Collection.new }

describe '#call' do

context 'with acceptable collections' do
let(:object1) { Hydra::PCDM::Object.create }
let(:object2) { Hydra::PCDM::Object.create }
let(:object3) { Hydra::PCDM::Object.create }
let(:object4) { Hydra::PCDM::Object.create }
let(:collection1) { Hydra::PCDM::Collection.create }
let(:collection2) { Hydra::PCDM::Collection.create }

it 'should add a related object to empty collection' do
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 )
expect( Hydra::PCDM::GetRelatedObjectsFromCollection.call( subject ) ).to eq [object1]
end

it 'should add a related object to collection with related objects' do
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 )
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object2 )
let(:object1) { Hydra::PCDM::Object.new }
let(:object2) { Hydra::PCDM::Object.new }
let(:object3) { Hydra::PCDM::Object.new }
let(:object4) { Hydra::PCDM::Object.new }
let(:collection1) { Hydra::PCDM::Collection.new }
let(:collection2) { Hydra::PCDM::Collection.new }

it 'should add objects to the related object set' do
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 ) # first add
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object2 ) # second add to same collection
related_objects = Hydra::PCDM::GetRelatedObjectsFromCollection.call( subject )
expect( related_objects.include? object1 ).to be true
expect( related_objects.include? object2 ).to be true
expect( related_objects.size ).to eq 2
end

it 'should not repeat objects in the related object set' do
skip 'pending resolution of ActiveFedora issue #853' do
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 ) # first add
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object2 ) # second add to same collection
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 ) # repeat an object replaces the object
related_objects = Hydra::PCDM::GetRelatedObjectsFromCollection.call( subject )
expect( related_objects.include? object1 ).to be true
expect( related_objects.include? object2 ).to be true
expect( related_objects.size ).to eq 2
end
end

context 'with collections and objects' do
before do
Hydra::PCDM::AddCollectionToCollection.call( subject, collection1 )
Hydra::PCDM::AddCollectionToCollection.call( subject, collection2 )
Hydra::PCDM::AddObjectToCollection.call( subject, object1 )
Hydra::PCDM::AddObjectToCollection.call( subject, object2 )
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object3 )
subject.save
end

it 'should add an object to collection with collections and objects' do
it 'should add a related object to a collection with collections and objects' do
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object4 )
related_objects = Hydra::PCDM::GetRelatedObjectsFromCollection.call( subject )
expect( related_objects.include? object3 ).to be true
Expand All @@ -48,46 +54,55 @@
end
end

context 'with unacceptable objects' do
let(:collection1) { Hydra::PCDM::Collection.create }
let(:file1) { Hydra::PCDM::File.new }
let(:non_PCDM_object) { "I'm not a PCDM object" }
let(:af_base_object) { ActiveFedora::Base.create }
context 'with unacceptable inputs' do
before(:all) do
@collection101 = Hydra::PCDM::Collection.new
@collection102 = Hydra::PCDM::Collection.new
@object101 = Hydra::PCDM::Object.new
@object102 = Hydra::PCDM::Object.new
@file101 = Hydra::PCDM::File.new
@non_PCDM_object = "I'm not a PCDM object"
@af_base_object = ActiveFedora::Base.new
end

let(:error_message) { 'child_related_object must be a pcdm object' }
context 'with unacceptable related objects' do
let(:error_message) { 'child_related_object must be a pcdm object' }

it 'should NOT aggregate Hydra::PCDM::Collection in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( subject, collection1 ) }.to raise_error(ArgumentError,error_message)
end
it 'should NOT aggregate Hydra::PCDM::Collection in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @collection102, @collection101 ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT aggregate Hydra::PCDM::Files in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( subject, file1 ) }.to raise_error(ArgumentError,error_message)
end
it 'should NOT aggregate Hydra::PCDM::Files in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @collection102, @file1 ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT aggregate non-PCDM objects in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( subject, non_PCDM_object ) }.to raise_error(ArgumentError,error_message)
end
it 'should NOT aggregate non-PCDM objects in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @collection102, @non_PCDM_object ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT aggregate AF::Base objects in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( subject, af_base_object ) }.to raise_error(ArgumentError,error_message)
it 'should NOT aggregate AF::Base objects in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @collection102, @af_base_object ) }.to raise_error(ArgumentError,error_message)
end
end

end
context 'with unacceptable parent object' do
let(:error_message) { 'parent_collection must be a pcdm collection' }

context 'with invalid bahaviors' do
let(:object1) { Hydra::PCDM::Object.create }
let(:object2) { Hydra::PCDM::Object.create }
it 'should NOT accept Hydra::PCDM::Collections as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @object102, @object101 ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT allow related objects to repeat' do
skip 'skipping this test because issue #92 needs to be addressed' do
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 )
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object2 )
Hydra::PCDM::AddRelatedObjectToCollection.call( subject, object1 )
related_objects = Hydra::PCDM::GetRelatedObjectsFromCollection.call( subject )
expect( related_objects.include? object1 ).to be true
expect( related_objects.include? object2 ).to be true
expect( related_objects.size ).to eq 2
end
it 'should NOT accept Hydra::PCDM::Files as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @file1, @object101 ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT accept non-PCDM objects as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @non_PCDM_object, @object101 ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT accept AF::Base objects as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToCollection.call( @af_base_object, @object101 ) }.to raise_error(ArgumentError,error_message)
end
end
end
end
Expand Down
113 changes: 53 additions & 60 deletions spec/hydra/pcdm/services/object/add_related_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,85 @@

describe Hydra::PCDM::AddRelatedObjectToObject do

let(:subject) { Hydra::PCDM::Object.create }
let(:subject) { Hydra::PCDM::Object.new }

describe '#call' do

context 'with acceptable objects' do
let(:object1) { Hydra::PCDM::Object.create }
let(:object2) { Hydra::PCDM::Object.create }
let(:object3) { Hydra::PCDM::Object.create }
let(:object1) { Hydra::PCDM::Object.new }
let(:object2) { Hydra::PCDM::Object.new }
let(:object3) { Hydra::PCDM::Object.new }
let(:file1) { Hydra::PCDM::File.new }

it 'should add a related object to empty object' do
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 )
expect( Hydra::PCDM::GetRelatedObjectsFromObject.call( subject ) ).to eq [object1]
it 'should add objects to the related object set' do
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 ) # first add
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object2 ) # second add to same object
related_objects = Hydra::PCDM::GetRelatedObjectsFromObject.call( subject )
expect( related_objects.include? object1 ).to be true
expect( related_objects.include? object2 ).to be true
expect( related_objects.size ).to eq 2
end

it 'should add a related object to object with related objects' do
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 )
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object2 )
it 'should not repeat objects in the related object set' do
skip 'pending resolution of ActiveFedora issue #853' do
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 ) # first add
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object2 ) # second add to same object
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 ) # repeat an object replaces the object
related_objects = Hydra::PCDM::GetRelatedObjectsFromObject.call( subject )
expect( related_objects.include? object1 ).to be true
expect( related_objects.include? object2 ).to be true
expect( related_objects.size ).to eq 2
end
end
end

context 'with unacceptable inputs' do
before(:all) do
@collection101 = Hydra::PCDM::Collection.new
@object101 = Hydra::PCDM::Object.new
@file101 = Hydra::PCDM::File.new
@non_PCDM_object = "I'm not a PCDM object"
@af_base_object = ActiveFedora::Base.new
end

context 'with files and objects' do
let(:file1) { subject.files.build }
let(:file2) { subject.files.build }
context 'with unacceptable related objects' do
let(:error_message) { 'child_related_object must be a pcdm object' }

before do
file1.content = "I'm a file"
file2.content = "I am too"
Hydra::PCDM::AddObjectToObject.call( subject, object1 )
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object2 )
subject.save!
it 'should NOT aggregate Hydra::PCDM::Collection in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @object101, @collection101 ) }.to raise_error(ArgumentError,error_message)
end

it 'should add an object to an object with files and objects' do
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object3 )
related_objects = Hydra::PCDM::GetRelatedObjectsFromObject.call( subject )
expect( related_objects.include? object2 ).to be true
expect( related_objects.include? object3 ).to be true
expect( related_objects.size ).to eq 2
it 'should NOT aggregate Hydra::PCDM::Files in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @object101, @file1 ) }.to raise_error(ArgumentError,error_message)
end
end
end

context 'with unacceptable objects' do
let(:collection1) { Hydra::PCDM::Collection.create }
let(:file1) { Hydra::PCDM::File.new }
let(:non_PCDM_object) { "I'm not a PCDM object" }
let(:af_base_object) { ActiveFedora::Base.create }

let(:error_message) { 'child_related_object must be a pcdm object' }

it 'should NOT aggregate Hydra::PCDM::Collection in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( subject, collection1 ) }.to raise_error(ArgumentError,error_message)
end
it 'should NOT aggregate non-PCDM objects in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @object101, @non_PCDM_object ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT aggregate Hydra::PCDM::Files in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( subject, file1 ) }.to raise_error(ArgumentError,error_message)
it 'should NOT aggregate AF::Base objects in objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @object101, @af_base_object ) }.to raise_error(ArgumentError,error_message)
end
end

it 'should NOT aggregate non-PCDM objects in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( subject, non_PCDM_object ) }.to raise_error(ArgumentError,error_message)
end
context 'with unacceptable parent object' do
let(:error_message) { 'parent_object must be a pcdm object' }

it 'should NOT aggregate AF::Base objects in related objects aggregation' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( subject, af_base_object ) }.to raise_error(ArgumentError,error_message)
end
it 'should NOT accept Hydra::PCDM::Collections as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @collection101, @object101 ) }.to raise_error(ArgumentError,error_message)
end

end
it 'should NOT accept Hydra::PCDM::Files as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @file1, @object101 ) }.to raise_error(ArgumentError,error_message)
end

context 'with invalid bahaviors' do
let(:object1) { Hydra::PCDM::Object.create }
let(:object2) { Hydra::PCDM::Object.create }
it 'should NOT accept non-PCDM objects as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @non_PCDM_object, @object101 ) }.to raise_error(ArgumentError,error_message)
end

it 'should NOT allow related objects to repeat' do
skip 'skipping this test because issue #92 needs to be addressed' do
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 )
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object2 )
Hydra::PCDM::AddRelatedObjectToObject.call( subject, object1 )
related_objects = Hydra::PCDM::GetRelatedObjectsFromObject.call( subject )
expect( related_objects.include? object1 ).to be true
expect( related_objects.include? object2 ).to be true
expect( related_objects.size ).to eq 2
end
it 'should NOT accept AF::Base objects as parent object' do
expect{ Hydra::PCDM::AddRelatedObjectToObject.call( @af_base_object, @object101 ) }.to raise_error(ArgumentError,error_message)
end
end
end
end
Expand Down

0 comments on commit b621f94

Please sign in to comment.