0
+require 'spec/spec_helper'
0
+describe ThinkingSphinx::Association do
0
+ describe "class-level children method" do
0
+ @normal_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
0
+ :options => {:polymorphic => false}
0
+ @normal_association = ThinkingSphinx::Association.stub_instance
0
+ @poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
0
+ :options => {:polymorphic => true},
0
+ :active_record => "AR"
0
+ @non_poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance
0
+ Person.stub_method(:reflect_on_association => @normal_reflection)
0
+ ThinkingSphinx::Association.stub_methods(
0
+ :new => @normal_association,
0
+ :polymorphic_classes => [Person, Person],
0
+ :casted_options => {:casted => :options}
0
+ ::ActiveRecord::Reflection::AssociationReflection.stub_method(
0
+ :new => @non_poly_reflection
0
+ it "should return an empty array if no association exists" do
0
+ Person.stub_method(:reflect_on_association => nil)
0
+ ThinkingSphinx::Association.children(Person, :assoc).should == []
0
+ it "should return a single association instance in an array if assocation isn't polymorphic" do
0
+ ThinkingSphinx::Association.children(Person, :assoc).should == [@normal_association]
0
+ it "should return multiple association instances for polymorphic associations" do
0
+ Person.stub_method(:reflect_on_association => @poly_reflection)
0
+ ThinkingSphinx::Association.children(Person, :assoc).should ==
0
+ [@normal_association, @normal_association]
0
+ it "should generate non-polymorphic 'casted' associations for each polymorphic possibility" do
0
+ Person.stub_method(:reflect_on_association => @poly_reflection)
0
+ ThinkingSphinx::Association.children(Person, :assoc)
0
+ ThinkingSphinx::Association.should have_received(:casted_options).with(
0
+ Person, @poly_reflection
0
+ ::ActiveRecord::Reflection::AssociationReflection.should have_received(:new).with(
0
+ :has_many, :polly_Person, {:casted => :options}, "AR"
0
+ ThinkingSphinx::Association.should have_received(:new).with(
0
+ nil, @non_poly_reflection
0
+ describe "instance-level children method" do
0
+ it "should return the children associations for the given association" do
0
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
0
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
0
+ ThinkingSphinx::Association.stub_method(:children => :result)
0
+ @association.children(:assoc).should == :result
0
+ ThinkingSphinx::Association.should have_received(:children).with(:klass, :assoc, @association)
0
+ describe "join_to method" do
0
+ @parent_join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
0
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
0
+ @parent = ThinkingSphinx::Association.stub_instance(:join_to => true, :join => nil)
0
+ @base_join = Object.stub_instance(:joins => [:a, :b, :c])
0
+ ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_method(:new => @join)
0
+ it "should call the parent's join_to if parent has no join" do
0
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
0
+ @assoc.join_to(@base_join)
0
+ @parent.should have_received(:join_to).with(@base_join)
0
+ it "should not call the parent's join_to if it already has a join" do
0
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
0
+ @parent.stub_method(:join => @parent_join)
0
+ @assoc.join_to(@base_join)
0
+ @parent.should_not have_received(:join_to)
0
+ it "should define the join association with a JoinAssociation instance" do
0
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
0
+ @assoc.join_to(@base_join).should == @join
0
+ @assoc.join.should == @join
0
+ describe "to_sql method" do
0
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
0
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
0
+ @parent = Object.stub_instance(:aliased_table_name => "ALIAS TABLE NAME")
0
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance(
0
+ :association_join => "full association join SQL",
0
+ @association.join = @join
0
+ it "should return the join's association join value" do
0
+ @association.to_sql.should == "full association join SQL"
0
+ it "should replace ::ts_join_alias:: with the aliased table name" do
0
+ @join.stub_method(:association_join => "text with ::ts_join_alias:: gone")
0
+ @association.to_sql.should == "text with `ALIAS TABLE NAME` gone"
0
+ describe "is_many? method" do
0
+ @parent = ThinkingSphinx::Association.stub_instance(
0
+ :is_many? => :parent_is_many
0
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
0
+ it "should return true if association is either a has_many or a habtm" do
0
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
0
+ association.is_many?.should be_true
0
+ @reflection.stub_method(:macro => :has_and_belongs_to_many)
0
+ association.is_many?.should be_true
0
+ it "should return the parent value if not a has many or habtm and there is a parent" do
0
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
0
+ @reflection.stub_method(:macro => :belongs_to)
0
+ association.is_many?.should == :parent_is_many
0
+ it "should return false if no parent and not a has many or habtm" do
0
+ association = ThinkingSphinx::Association.new(nil, @reflection)
0
+ @reflection.stub_method(:macro => :belongs_to)
0
+ association.is_many?.should be_false
0
+ describe "ancestors method" do
0
+ it "should return an array of associations - including all parents" do
0
+ parent = ThinkingSphinx::Association.stub_instance(:ancestors => [:all, :ancestors])
0
+ association = ThinkingSphinx::Association.new(parent, @reflection)
0
+ association.ancestors.should == [:all, :ancestors, association]
0
+ describe "polymorphic_classes method" do
0
+ it "should return all the polymorphic result types as classes" do
0
+ Person.connection.stub_method(:select_all => [
0
+ {"person_type" => "Person"},
0
+ {"person_type" => "Friendship"}
0
+ ref = Object.stub_instance(
0
+ :active_record => Person,
0
+ :options => {:foreign_type => "person_type"}
0
+ ThinkingSphinx::Association.send(:polymorphic_classes, ref).should == [Person, Friendship]
0
+ describe "casted_options method" do
0
+ :foreign_key => "thing_id",
0
+ :foreign_type => "thing_type",
0
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
0
+ it "should return a new options set for a specific class" do
0
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
0
+ :class_name => "Person",
0
+ :foreign_key => "thing_id",
0
+ :foreign_type => "thing_type",
0
+ :conditions => "::ts_join_alias::.`thing_type` = 'Person'"
0
+ it "should append to existing Array of conditions" do
0
+ @options[:conditions] = ["first condition"]
0
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
0
+ :class_name => "Person",
0
+ :foreign_key => "thing_id",
0
+ :foreign_type => "thing_type",
0
+ :conditions => ["first condition", "::ts_join_alias::.`thing_type` = 'Person'"]
0
+ it "should merge to an existing Hash of conditions" do
0
+ @options[:conditions] = {"field" => "value"}
0
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
0
+ :class_name => "Person",
0
+ :foreign_key => "thing_id",
0
+ :foreign_type => "thing_type",
0
+ :conditions => {"field" => "value", "thing_type" => "Person"}
0
+ it "should append to an existing String of conditions" do
0
+ @options[:conditions] = "first condition"
0
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
0
+ :class_name => "Person",
0
+ :foreign_key => "thing_id",
0
+ :foreign_type => "thing_type",
0
+ :conditions => "first condition AND ::ts_join_alias::.`thing_type` = 'Person'"
0
\ No newline at end of file