spec/ripple/callbacks_spec.rb in ripple-0.9.5 vs spec/ripple/callbacks_spec.rb in ripple-1.0.0.beta

- old
+ new

@@ -1,22 +1,9 @@ -# Copyright 2010 Sean Cribbs, Sonian Inc., and Basho Technologies, Inc. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -require File.expand_path("../../spec_helper", __FILE__) +require 'spec_helper' describe Ripple::Callbacks do - require 'support/models/box' + # require 'support/models/box' it "should add create, update, save, and destroy callback declarations" do [:save, :create, :update, :destroy].each do |event| Box.private_instance_methods.map(&:to_s).should include("_run_#{event}_callbacks") [:before, :after, :around].each do |time| @@ -47,10 +34,59 @@ $pinger.should_receive(:ping).exactly(3).times @box = Box.new @box.save end + it "propagates callbacks to embedded associated documents" do + Box.before_save { $pinger.ping } + BoxSide.before_save { $pinger.ping } + $pinger.should_receive(:ping).exactly(2).times + @box = Box.new + @box.sides << BoxSide.new + @box.save + end + + it 'does not persist the object to riak multiple times when propagating callbacks' do + Box.before_save { } + BoxSide.before_save { } + @box = Box.new + @box.sides << BoxSide.new << BoxSide.new + + @box.robject.should_receive(:store).once + @box.save + end + + it 'invokes the before/after callbacks in the correct order on embedded associated documents' do + callbacks = [] + BoxSide.before_save { callbacks << :before_save } + BoxSide.after_save { callbacks << :after_save } + + @box = Box.new + @box.sides << BoxSide.new + @box.robject.stub(:store) do + callbacks << :save + end + @box.save + + callbacks.should == [:before_save, :save, :after_save] + end + + it 'does not allow around callbacks on embedded associated documents' do + expect { + BoxSide.around_save { } + }.to raise_error(/around_save callbacks are not supported/) + end + + it 'does not propagate validation callbacks multiple times' do + Box.before_validation { $pinger.ping } + BoxSide.before_validation { $pinger.ping } + $pinger.should_receive(:ping).exactly(2).times + @box = Box.new + @box.sides << BoxSide.new + @box.valid? + end + it "should call create callbacks on save when the document is new" do Box.before_create { $pinger.ping } Box.after_create { $pinger.ping } Box.around_create(lambda { $pinger.ping }) $pinger.should_receive(:ping).exactly(3).times @@ -131,9 +167,10 @@ end after :each do [:save, :create, :update, :destroy, :validation].each do |type| Box.reset_callbacks(type) + BoxSide.reset_callbacks(type) end end end end