Skip to content

Commit

Permalink
Make autosave tests more specific
Browse files Browse the repository at this point in the history
Autosave should not be included when it's not needed and should be tested to ensure it saves changes to existing objects
  • Loading branch information
awead committed Jun 24, 2015
1 parent 2af40eb commit 038adc0
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 63 deletions.
10 changes: 5 additions & 5 deletions spec/integration/belongs_to_association_spec.rb
Expand Up @@ -106,13 +106,13 @@ def assert_content_model
end

class SimpleCollection < ActiveFedora::Base
has_many :objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'SimpleObject', autosave: true
has_many :complex_objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ZComplexObject', autosave: true
has_many :objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'SimpleObject'
has_many :complex_objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ZComplexObject'
end

class ComplexCollection < SimpleCollection
has_many :objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'SimpleObject', autosave: true
has_many :complex_objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ZComplexObject', autosave: true
has_many :objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'SimpleObject'
has_many :complex_objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ZComplexObject'

def assert_content_model
self.has_model = [self.class.to_s, self.class.superclass.to_s]
Expand Down Expand Up @@ -223,7 +223,7 @@ class SubclassObject < SuperclassObject
end

class SuperclassCollection < ActiveFedora::Base
has_many :objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'SuperclassObject', autosave: true
has_many :objects, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'SuperclassObject'
end
end
after :all do
Expand Down
151 changes: 93 additions & 58 deletions spec/integration/has_many_associations_spec.rb
Expand Up @@ -280,85 +280,120 @@ class Component < ActiveFedora::Base
end

describe "Autosave" do
context "a has_many - belongs_to relationship" do
before do
class Item < ActiveFedora::Base
has_many :components
has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
m.field "title", :string
end
Deprecation.silence(ActiveFedora::Attributes) do
has_attributes :title, datastream: 'foo'
end
end
class Component < ActiveFedora::Base
belongs_to :item, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf
has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
m.field "description", :string
context "with new objects" do
context "a has_many - belongs_to relationship" do
before do
class Item < ActiveFedora::Base
has_many :components
has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
m.field "title", :string
end
Deprecation.silence(ActiveFedora::Attributes) do
has_attributes :title, datastream: 'foo'
end
end
Deprecation.silence(ActiveFedora::Attributes) do
has_attributes :description, datastream: 'foo'
class Component < ActiveFedora::Base
belongs_to :item, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf
has_metadata "foo", type: ActiveFedora::SimpleDatastream do |m|
m.field "description", :string
end
Deprecation.silence(ActiveFedora::Attributes) do
has_attributes :description, datastream: 'foo'
end
end
end
end

after do
Object.send(:remove_const, :Item)
Object.send(:remove_const, :Component)
end
after do
Object.send(:remove_const, :Item)
Object.send(:remove_const, :Component)
end

context "From the belongs_to side" do
let(:component) { Component.create(item: Item.new(title: 'my title')) }
context "From the belongs_to side" do
let(:component) { Component.create(item: Item.new(title: 'my title')) }

it "should save dependent records" do
component.reload
expect(component.item.title).to eq 'my title'
it "should save dependent records" do
component.reload
expect(component.item.title).to eq 'my title'
end
end
end

context "From the has_many side" do
let(:item) { Item.create(components: [Component.new(description: 'my description')]) }
context "From the has_many side" do
let(:item) { Item.create(components: [Component.new(description: 'my description')]) }

it "should save dependent records" do
item.reload
expect(item.components.first.description).to eq 'my description'
it "should save dependent records" do
item.reload
expect(item.components.first.description).to eq 'my description'
end
end
end
end

context "a has_many - has_and_belongs_to_many relationship" do
context "with ActiveFedora::Base as classes" do
before do
class Novel < ActiveFedora::Base
has_many :books, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
has_and_belongs_to_many :contents, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
context "a has_many - has_and_belongs_to_many relationship" do
context "with ActiveFedora::Base as classes" do
before do
class Novel < ActiveFedora::Base
has_many :books, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
has_and_belongs_to_many :contents, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
end
class Text < ActiveFedora::Base
has_many :books, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
end
end
class Text < ActiveFedora::Base
has_many :books, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.isPartOf, class_name: 'ActiveFedora::Base'
let(:text) { Text.create}
let(:novel) { Novel.create}

after do
Object.send(:remove_const, :Novel)
Object.send(:remove_const, :Text)
end

it "should work when added via the has_many" do
text.books << novel
novel.save
expect(novel.reload.contents).to eq [text]
expect(text.reload.books).to eq [novel]
end

it "should work when added via the has_and_belongs_to_many" do
novel.contents << text
novel.save!
text.reload
expect(text.books).to eq [novel]
end
end
let(:text) { Text.create}
let(:novel) { Novel.create}

after do
Object.send(:remove_const, :Novel)
Object.send(:remove_const, :Text)
end
end
end

it "should work when added via the has_many" do
text.books << novel
novel.save
expect(novel.reload.contents).to eq [text]
expect(text.reload.books).to eq [novel]
context "with updated objects" do

before :all do
class Library < ActiveFedora::Base
has_many :books, autosave: true
end

it "should work when added via the has_and_belongs_to_many" do
novel.contents << text
novel.save!
text.reload
expect(text.books).to eq [novel]
class Book < ActiveFedora::Base
belongs_to :library, predicate: ActiveFedora::RDF::Fcrepo::RelsExt.hasConstituent
property :title, predicate: ::RDF::DC.title
end
end
after :all do
Object.send(:remove_const, :Book)
Object.send(:remove_const, :Library)
end

let(:library) { Library.create }

before do
library.books.create(title: ["Great Book"])
library.books.first.title = ["Better book"]
library.save
end

subject { library.books(true) }

it "saves the new title" do
expect(subject.first.title).to eql ["Better book"]
end

end
end

0 comments on commit 038adc0

Please sign in to comment.