spec/ripple/associations_spec.rb in ripple-0.9.5 vs spec/ripple/associations_spec.rb in ripple-1.0.0.beta
- old
+ new
@@ -1,34 +1,30 @@
-# 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/models/invoice'
- require 'support/models/customer'
- require 'support/models/note'
+ # require 'support/models/invoice'
+ # require 'support/models/customer'
+ # require 'support/models/note'
it "should provide access to the associations hash" do
Invoice.should respond_to(:associations)
Invoice.associations.should be_kind_of(Hash)
end
it "should collect the embedded associations" do
Invoice.embedded_associations.should == Array(Invoice.associations[:note])
end
+ it "should collect the linked associations" do
+ Invoice.linked_associations.should == Array(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])
+ end
+
it "should copy associations to a subclass" do
Invoice.associations[:foo] = "bar"
class SubInvoice < Invoice; end
SubInvoice.associations[:foo].should == "bar"
end
@@ -48,16 +44,21 @@
Invoice.instance_methods.map(&:to_sym).should include(:payee=)
Invoice.instance_methods.map(&:to_sym).should include(:payee?)
end
end
- after do
- Invoice.associations.delete(:foo)
- Invoice.associations.delete(:items)
- SubInvoice.associations.delete(:foo) if defined?(SubInvoice)
- Invoice.associations.delete(:payee)
+ before(:all) do
+ @orig_invoice = Invoice
+ Object.send(:remove_const, :Invoice)
+ class Invoice < @orig_invoice; end
end
+
+ after(:all) do
+ Object.send(:remove_const, :SubInvoice) if defined?(SubInvoice)
+ Object.send(:remove_const, :Invoice)
+ Object.send(:const_set, :Invoice, @orig_invoice)
+ end
end
describe Ripple::Association do
it "should initialize with a type and name" do
lambda { Ripple::Association.new(:many, :pages) }.should_not raise_error
@@ -79,12 +80,27 @@
@association.class_name.should == "Note"
end
end
describe "determining the target class" do
- require 'support/models/tree'
+ context "when in a nested module scope" do
+ it "should find the target via the owner's module scope" do
+ @association = Ripple::Association.new(:many, :children)
+ @association.setup_on(Nested::Scope::Parent)
+ @association.klass.should == Nested::Scope::Child
+ end
+ it "should not try to find the target in the owner's scope when the class name is fully qualified" do
+ @association = Ripple::Association.new(:many, :children, :class_name => "Nested::Scope::Child")
+ @association.setup_on(Nested::Scope::Parent)
+ expect {
+ @association.klass
+ }.should_not raise_error
+ @association.klass.should == Nested::Scope::Child
+ end
+ end
+
it "should default to the constantized class name" do
@association = Ripple::Association.new(:one, :t, :class_name => "Trunk")
@association.klass.should == Trunk
end
@@ -107,7 +123,22 @@
Ripple::Association.new(:one, :pages).should be_one
end
it "should determine an instance variable based on the name" do
Ripple::Association.new(:many, :pages).ivar.should == "@_pages"
+ end
+
+ describe "key correspondence" do
+ require 'support/models/profile'
+ require 'support/models/user'
+
+ it "should raise an exception if trying to use a many association when :using => :key" do
+ expect { Profile.many :user, :using => :key }.to raise_error(ArgumentError, "You cannot use a many association using :key")
+ end
+
+ it "should add the key_delegate attr_accessor to the model declaring the association" do
+ Profile.one :user, :using => :key
+ Profile.new.should respond_to(:key_delegate)
+ Profile.new.should respond_to(:key_delegate=)
+ end
end
end