spec/integration/ripple/associations_spec.rb in ripple-0.9.5 vs spec/integration/ripple/associations_spec.rb in ripple-1.0.0.beta

- old
+ new

@@ -1,63 +1,50 @@ -# 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 Associations" do - require 'support/test_server' + before :each do + @user = User.new(:email => 'riak@ripple.com') + @profile = UserProfile.new(:name => 'Ripple') + @billing = Address.new(:street => '123 Somewhere Dr', :kind => 'billing') + @shipping = Address.new(:street => '321 Anywhere Pl', :kind => 'shipping') + @friend1 = User.create(:email => "friend@ripple.com") + @friend2 = User.create(:email => "friend2@ripple.com") + @cc = CreditCard.new(:number => '12345') + @post = Post.new(:title => "Hello, world!") + @comment_one = Comment.new.tap{|c| c.key = "one"; c.save! } + @comment_two = Comment.new.tap{|c| c.key = "two"; c.save! } + end - before :all do - Object.module_eval do - class User - include Ripple::Document - one :profile - many :addresses - property :email, String, :presence => true - many :friends, :class_name => "User" - one :emergency_contact, :class_name => "User" - end - class Profile - include Ripple::EmbeddedDocument - property :name, String, :presence => true - embedded_in :user - end - class Address - include Ripple::EmbeddedDocument - property :street, String, :presence => true - property :kind, String, :presence => true - embedded_in :user - end - end + it "should save and restore a many stored key association" do + @post.comments << @comment_one << @comment_two + @post.save! + + post = Post.find(@post.key) + post.comment_keys.should == [ 'one', 'two' ] + post.comments.keys.should == [ 'one', 'two' ] + post.comments.should == [ @comment_one, @comment_two ] end - before :each do - @user = User.new(:email => 'riak@ripple.com') - @profile = Profile.new(:name => 'Ripple') - @billing = Address.new(:street => '123 Somewhere Dr', :kind => 'billing') - @shipping = Address.new(:street => '321 Anywhere Pl', :kind => 'shipping') - @friend1 = User.create(:email => "friend@ripple.com") - @friend2 = User.create(:email => "friend2@ripple.com") + it "should remove a document from a many stored key association" do + @post.comments << @comment_one + @post.comments << @comment_two + @post.save! + @post.comments.delete(@comment_one) + @post.save! + + @post = Post.find(@post.key) + @post.comment_keys.should == [ @comment_two.key ] + @post.comments.should == [ @comment_two ] end it "should save one embedded associations" do - @user.profile = @profile + @user.user_profile = @profile @user.save @found = User.find(@user.key) - @found.profile.name.should == 'Ripple' - @found.profile.should be_a(Profile) - @found.profile.user.should == @found + @found.user_profile.name.should == 'Ripple' + @found.user_profile.should be_a(UserProfile) + @found.user_profile.user.should == @found end it "should not raise an error when a one linked associated record has been deleted" do @user.emergency_contact = @friend1 @user.save @@ -106,28 +93,72 @@ it "should save a many linked association" do @user.friends << @friend1 << @friend2 @user.save @user.should_not be_new_record @found = User.find(@user.key) - @found.friends.map(&:key).should include(@friend1.key) - @found.friends.map(&:key).should include(@friend2.key) + @found.friends.should include(@friend1) + @found.friends.should include(@friend2) end it "should save a one linked association" do @user.emergency_contact = @friend1 @user.save @user.should_not be_new_record @found = User.find(@user.key) - @found.emergency_contact.key.should == @friend1.key + @found.emergency_contact.should == @friend1 end - after :each do - User.destroy_all + it "should reload associations" do + @user.friends << @friend1 + @user.save! + + friend1_new_instance = User.find(@friend1.key) + friend1_new_instance.email = 'new-address@ripple.com' + friend1_new_instance.save! + + @user.reload + @user.friends.map(&:email).should == ['new-address@ripple.com'] end - after :all do - Object.send(:remove_const, :User) - Object.send(:remove_const, :Profile) - Object.send(:remove_const, :Address) + it "allows and autosaves transitive linked associations" do + friend = User.new(:email => 'user-friend@example.com') + friend.key = 'main-user-friend' + @user.key = 'main-user' + @user.friends << friend + friend.friends << @user + + @user.save! # should save both since friend is new + + found_user = User.find!(@user.key) + found_friend = User.find!(friend.key) + + found_user.friends.should == [found_friend] + found_friend.friends.should == [found_user] end + it "should find the object associated by key after saving" do + @user.key = 'paying-user' + @user.credit_card = @cc + @user.save && @cc.save + @found = User.find(@user.key) + @found.reload + @found.credit_card.should eq(@cc) + end + + it "should assign the generated riak key to the associated object using key" do + @user.key.should be_nil + @user.credit_card = @cc + @user.save + @cc.key.should_not be_blank + @cc.key.should eq(@user.key) + end + + it "should save one association by storing key" do + @user.save! + @post.user = @user + @post.save! + @post.user_key.should == @user.key + @found = Post.find(@post.key) + @found.user.email.should == 'riak@ripple.com' + @found.user.should be_a(User) + end end