From 16e58172edd22a749e285703f907d1b3201df961 Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Mon, 24 Jan 2011 06:40:08 +0100 Subject: [PATCH] Forgot a binding spec for referenced in --- .../relations/bindings/referenced/in_spec.rb | 146 ++++++++++++++++-- 1 file changed, 131 insertions(+), 15 deletions(-) diff --git a/spec/unit/mongoid/relations/bindings/referenced/in_spec.rb b/spec/unit/mongoid/relations/bindings/referenced/in_spec.rb index a9a55ebe12..781852ee98 100644 --- a/spec/unit/mongoid/relations/bindings/referenced/in_spec.rb +++ b/spec/unit/mongoid/relations/bindings/referenced/in_spec.rb @@ -2,51 +2,167 @@ describe Mongoid::Relations::Bindings::Referenced::In do - describe "#bind" do + let(:person) do + Person.new + end - it "sets the foreign key value" do + let(:game) do + Game.new + end - end + let(:post) do + Post.new + end - context "when an inverse type exists" do + let(:game_metadata) do + Game.relations["person"] + end - end + let(:post_metadata) do + Post.relations["person"] + end + + describe "#bind" do - context "when an inverse exists" do + context "when the child of an references one" do - context "when continuing" do + let(:binding) do + described_class.new(game, person, game_metadata) + end - context "when the base is a references many" do + context "when the document is bindable" do + before do + person.expects(:save).never + game.expects(:save).never + binding.bind(:continue => true) end - context "when the base is a references one" do + it "sets the inverse relation" do + person.game.should == game + end + it "sets the foreign key" do + game.person_id.should == person.id end end - context "when not continuing" do + context "when the document is not bindable" do + + before do + person.game = game + end + it "does nothing" do + game.expects(:person=).never + binding.bind + end end end - context "when an inverse does not exist" do + context "when the child of an references many" do + + let(:binding) do + described_class.new(post, person, post_metadata) + end + + context "when the document is bindable" do + + before do + person.expects(:save).never + post.expects(:save).never + binding.bind(:continue => true) + end + + it "sets the inverse relation" do + person.posts.should include(post) + end + + it "sets the foreign key" do + post.person_id.should == person.id + end + end + + context "when the document is not bindable" do + + before do + person.posts = [ post ] + end + it "does nothing" do + post.expects(:person=).never + binding.bind + end + end end end describe "#unbind" do - it "removes the foreign key value" do + context "when the child of an references one" do - end + let(:binding) do + described_class.new(game, person, game_metadata) + end + + context "when the document is unbindable" do + + before do + binding.bind + person.expects(:delete).never + game.expects(:delete).never + binding.unbind + end - context "when continuing" do + it "removes the inverse relation" do + person.game.should be_nil + end + + it "removed the foreign key" do + game.person_id.should be_nil + end + end + context "when the document is not unbindable" do + + it "does nothing" do + game.expects(:person=).never + binding.unbind + end + end end - context "when not continuing" do + context "when the child of an references many" do + let(:binding) do + described_class.new(post, person, post_metadata) + end + + context "when the document is unbindable" do + + before do + binding.bind + person.expects(:delete).never + post.expects(:delete).never + binding.unbind + end + + it "removes the inverse relation" do + person.posts.should be_empty + end + + it "removes the foreign key" do + post.person_id.should be_nil + end + end + + context "when the document is not unbindable" do + + it "does nothing" do + post.expects(:person=).never + binding.unbind + end + end end end end