Permalink
Browse files

Fix lots of test breakages resulting from dependent orderings.

* Tests that modify class state should clean up after themselves or be isolated.
* Some Array/Hash orderings on 1.8 are not reliable, so try to test order-insensitive equality.
* Search-required specs need the test server to be created first.
* Capture callback sequences instead of using a mock.
* Change how Riak Search is detected.
  • Loading branch information...
1 parent c4a2454 commit 17d0ca4e71ee12f7ced6d490d01f04ef92905a1d @seancribbs seancribbs committed Jan 25, 2012
View
@@ -33,3 +33,4 @@ Gemfile*.lock
**/.riaktest
vendor/erlang
vendor/riak
+tmp/*
@@ -178,7 +178,7 @@ def propagate_callbacks_to_embedded_associations(name, kind)
# @private
def run_callbacks(name, *args, &block)
# validation is already propagated to embedded documents via the
- # AssociatedValidator. We don't need to duplicate the propgation here.
+ # AssociatedValidator. We don't need to duplicate the propagation here.
return super if name == :validation
propagate_callbacks_to_embedded_associations(name, :before)
@@ -9,10 +9,11 @@ def create_conflict(main_record, *modifiers)
begin
klass, key = main_record.class, main_record.key
+ raise "#{klass.bucket.name} allow_mult property is false!" unless klass.bucket.allow_mult
records = modifiers.map { |_| klass.find!(key) }
records.zip(modifiers).each do |(record, modifier)|
- # necessary to get conflict, so riak thinks they are being saved by different clients
+ # necessary to get conflict on 0.14 and earlier, so riak thinks they are being saved by different clients
Ripple.client.client_id += 1
modifier.call(record)
@@ -116,7 +116,6 @@
end
it "should not add the associated validator multiple times" do
- #$stderr.puts User.validators.collect(&:inspect)
User.validators_on(:addresses).count.should eq(1)
end
end
@@ -58,7 +58,7 @@
@person.tasks.__send__(:should_receive, :reset)
@person.tasks.__send__(:links).should == [@task.robject.to_link("tasks")]
@person.tasks.replace_links([@other_task, @third_task].map { |t| t.robject.to_link("tasks") })
- @person.tasks.__send__(:links).should == [@other_task, @third_task].map { |t| t.robject.to_link("tasks") }
+ @person.tasks.__send__(:links).should =~ [@other_task, @third_task].map { |t| t.robject.to_link("tasks") }
end
it "should replace associated documents with a new set" do
@@ -151,7 +151,7 @@
it "returns a set of keys" do
@person.tasks.keys.should be_a(Set)
- @person.tasks.keys.to_a.should == link_keys
+ @person.tasks.keys.to_a.should =~ link_keys
end
it "is memoized between calls" do
@@ -123,7 +123,7 @@
it "returns a set of keys" do
@account.payment_methods.keys.should be_a(Set)
- @account.payment_methods.keys.to_a.should == ze_keys
+ @account.payment_methods.keys.to_a.should =~ ze_keys
end
it "is memoized between calls" do
@@ -11,22 +11,23 @@
end
it "should collect the embedded associations" do
- Invoice.embedded_associations.should == Array(Invoice.associations[:note])
+ Invoice.embedded_associations.should == [ Invoice.associations[:note] ]
end
it "should collect the linked associations" do
- Invoice.linked_associations.should == Array(Invoice.associations[:customer])
+ Invoice.linked_associations.should == [ Invoice.associations[:customer] ]
end
it "should collect the stored_key associations" do
- Account.stored_key_associations.should == Array(Account.associations[:transactions])
- Transaction.stored_key_associations.should == Array(Transaction.associations[:account])
+ Account.stored_key_associations.should == [ Account.associations[:transactions] ]
+ Transaction.stored_key_associations.should == [ Transaction.associations[:account] ]
end
it "should copy associations to a subclass" do
Invoice.associations[:foo] = "bar"
class SubInvoice < Invoice; end
SubInvoice.associations[:foo].should == "bar"
+ Invoice.associations.delete :foo
end
describe "when adding a :many association" do
@@ -1,184 +1,194 @@
require 'spec_helper'
describe Ripple::Callbacks do
- # require 'support/models/box'
+ let(:doc) do
+ _e = embedded
+ Class.new do
+ include Ripple::Document
+ self.bucket_name = "docs"
+ many :embeddeds, :class => _e
+ end
+ end
+
+ let(:embedded) do
+ Class.new do
+ include Ripple::EmbeddedDocument
+ end
+ end
+
+ subject { doc.new }
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")
+ doc.private_instance_methods.map(&:to_s).should include("_run_#{event}_callbacks")
[:before, :after, :around].each do |time|
- Box.should respond_to("#{time}_#{event}")
+ doc.should respond_to("#{time}_#{event}")
end
end
end
it "should validate callback declarations" do
- Box.private_instance_methods.map(&:to_s).should include("_run_validation_callbacks")
- Box.should respond_to("before_validation")
- Box.should respond_to("after_validation")
+ doc.private_instance_methods.map(&:to_s).should include("_run_validation_callbacks")
+ doc.should respond_to("before_validation")
+ doc.should respond_to("after_validation")
end
describe "invoking callbacks" do
before :each do
- response = {:headers => {"content-type" => ["application/json"]}, :body => "{}"}
@client = Ripple.client
@client.stub(:store_object => true)
- $pinger = mock("callback verifier")
end
it "should call save callbacks on save" do
- Box.before_save { $pinger.ping }
- Box.after_save { $pinger.ping }
- Box.around_save(lambda { $pinger.ping })
- $pinger.should_receive(:ping).exactly(3).times
- @box = Box.new
- @box.save
+ callbacks = []
+ doc.before_save { callbacks << :before }
+ doc.after_save { callbacks << :after }
+ doc.around_save(lambda { callbacks << :around })
+ subject.save
+ callbacks.should == [ :before, :around, :after ]
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
+ callbacks = []
+ doc.before_save { callbacks << :box }
+ embedded.before_save { callbacks << :side }
+ subject.embeddeds << embedded.new
+ subject.save
+ callbacks.should == [:side, :box]
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
+ doc.before_save { }
+ embedded.before_save { }
+ subject.embeddeds << embedded.new << embedded.new
- @box.robject.should_receive(:store).once
- @box.save
+ subject.robject.should_receive(:store).once
+ subject.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 }
+ embedded.before_save { callbacks << :before_save }
+ embedded.after_save { callbacks << :after_save }
- @box = Box.new
- @box.sides << BoxSide.new
- @box.robject.stub(:store) do
+ subject.embeddeds << embedded.new
+ subject.robject.stub(:store) do
callbacks << :save
end
- @box.save
+ subject.save
callbacks.should == [:before_save, :save, :after_save]
end
it 'does not allow around callbacks on embedded associated documents' do
expect {
- BoxSide.around_save { }
+ embedded.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?
+ callbacks = []
+ doc.before_validation { callbacks << :box }
+ embedded.before_validation { callbacks << :side }
+
+ subject.embeddeds << embedded.new
+ subject.valid?
+ callbacks.should == [:box, :side]
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
- @box = Box.new
- @box.save
+ callbacks = []
+ doc.before_create { callbacks << :before }
+ doc.after_create { callbacks << :after }
+ doc.around_create(lambda { callbacks << :around })
+
+ subject.save
+ callbacks.should == [:before, :around, :after ]
end
it "should call update callbacks on save when the document is not new" do
- Box.before_update { $pinger.ping }
- Box.after_update { $pinger.ping }
- Box.around_update(lambda { $pinger.ping })
- $pinger.should_receive(:ping).exactly(3).times
- @box = Box.new
- @box.stub!(:new?).and_return(false)
- @box.save
+ callbacks = []
+ doc.before_update { callbacks << :before }
+ doc.after_update { callbacks << :after }
+ doc.around_update(lambda { callbacks << :around })
+
+ subject.stub!(:new?).and_return(false)
+ subject.save
+ callbacks.should == [:before, :around, :after ]
end
describe "destroy callbacks" do
- let(:box) { Box.new }
+ let(:callbacks) { [] }
before(:each) do
- Box.before_destroy { $pinger.ping }
- Box.after_destroy { $pinger.ping }
- Box.around_destroy(lambda { $pinger.ping })
- $pinger.should_receive(:ping).exactly(3).times
+ _callbacks = callbacks
+ doc.before_destroy { _callbacks << :before }
+ doc.after_destroy { _callbacks << :after }
+ doc.around_destroy(lambda { _callbacks << :around })
end
+ after { callbacks.should == [ :before, :around, :after ] }
+
it "invokes them when #destroy is called" do
- box.destroy
+ subject.destroy
end
it "invokes them when #destroy! is called" do
- box.destroy!
+ subject.destroy!
end
end
it "should call save and validate callbacks in the correct order" do
- Box.before_validation { $pinger.ping(:v) }
- Box.before_save { $pinger.ping(:s) }
- $pinger.should_receive(:ping).with(:v).ordered
- $pinger.should_receive(:ping).with(:s).ordered
- @box = Box.new
- @box.save
+ callbacks = []
+ doc.before_validation { callbacks << :validation }
+ doc.before_save { callbacks << :save }
+
+ subject.save
+ callbacks.should == [:validation, :save]
end
describe "validation callbacks" do
it "should call validation callbacks" do
- Box.before_validation { $pinger.ping }
- Box.after_validation { $pinger.ping }
- $pinger.should_receive(:ping).twice
- @box = Box.new
- @box.valid?
+ callbacks = []
+ doc.before_validation { callbacks << :before }
+ doc.after_validation { callbacks << :after }
+ subject.valid?
+ callbacks.should == [:before, :after]
end
it "should call validation callbacks only if the document is new" do
- Box.before_validation(:on => :create) { $pinger.ping }
- Box.after_validation(:on => :create) { $pinger.ping }
- $pinger.should_receive(:ping).twice
- @box = Box.new
- @box.valid?
+ callbacks = []
+ doc.before_validation(:on => :create) { callbacks << :before }
+ doc.after_validation(:on => :create) { callbacks << :after }
+ subject.valid?
+ callbacks.should == [:before, :after]
end
it "should not call validation callbacks only if the document is new" do
- Box.before_validation(:on => :update) { $pinger.ping }
- Box.after_validation(:on => :update) { $pinger.ping }
- $pinger.should_not_receive(:ping)
- @box = Box.new
- @box.valid?
+ callbacks = []
+ doc.before_validation(:on => :update) { callbacks << :before }
+ doc.after_validation(:on => :update) { callbacks << :after }
+ subject.valid?
+ callbacks.should be_empty
end
it "should call validation callbacks only if the document is not new" do
- Box.before_validation(:on => :update) { $pinger.ping }
- Box.after_validation(:on => :update) { $pinger.ping }
- $pinger.should_receive(:ping).twice
- @box = Box.new
- @box.stub(:new?).and_return(false)
- @box.valid?
- end
+ callbacks = []
+ doc.before_validation(:on => :update) { callbacks << :before }
+ doc.after_validation(:on => :update) { callbacks << :after }
- it "should not call validation callbacks only if the document is not new" do
- Box.before_validation(:on => :create) { $pinger.ping }
- Box.after_validation(:on => :create) { $pinger.ping }
- $pinger.should_not_receive(:ping)
- @box = Box.new
- @box.stub!(:new?).and_return(false)
- @box.valid?
+ subject.stub(:new?).and_return(false)
+ subject.valid?
+ callbacks.should == [:before, :after]
end
- end
- after :each do
- [:save, :create, :update, :destroy, :validation].each do |type|
- Box.reset_callbacks(type)
- BoxSide.reset_callbacks(type)
+ it "should not call validation callbacks only if the document is not new" do
+ callbacks = []
+ doc.before_validation(:on => :create) { callbacks << :validation }
+ doc.after_validation(:on => :create) { callbacks << :after }
+ subject.stub!(:new?).and_return(false)
+ subject.valid?
+ callbacks.should be_empty
end
end
end
Oops, something went wrong.

0 comments on commit 17d0ca4

Please sign in to comment.