From 038adc0ec84ac6ff18bd99f712f526b94131193e Mon Sep 17 00:00:00 2001 From: Adam Wead Date: Wed, 24 Jun 2015 16:10:04 -0400 Subject: [PATCH] Make autosave tests more specific Autosave should not be included when it's not needed and should be tested to ensure it saves changes to existing objects --- .../belongs_to_association_spec.rb | 10 +- .../integration/has_many_associations_spec.rb | 151 +++++++++++------- 2 files changed, 98 insertions(+), 63 deletions(-) diff --git a/spec/integration/belongs_to_association_spec.rb b/spec/integration/belongs_to_association_spec.rb index a52f85805..1726a72bc 100644 --- a/spec/integration/belongs_to_association_spec.rb +++ b/spec/integration/belongs_to_association_spec.rb @@ -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] @@ -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 diff --git a/spec/integration/has_many_associations_spec.rb b/spec/integration/has_many_associations_spec.rb index 77726d126..d1da86eca 100644 --- a/spec/integration/has_many_associations_spec.rb +++ b/spec/integration/has_many_associations_spec.rb @@ -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