Skip to content

Commit

Permalink
Forgot a binding spec for referenced in
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Jan 24, 2011
1 parent 6c37adf commit 16e5817
Showing 1 changed file with 131 additions and 15 deletions.
146 changes: 131 additions & 15 deletions spec/unit/mongoid/relations/bindings/referenced/in_spec.rb
Expand Up @@ -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

0 comments on commit 16e5817

Please sign in to comment.