From 7aaf4867d2d7c1adea419052f069dab542af13b9 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 16 Aug 2006 18:10:52 +0000 Subject: Included associations: go deep. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4776 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../associations_cascaded_eager_loading_test.rb | 35 +++++++++++++++++++--- .../test/fixtures/db_definitions/schema.rb | 10 +++++++ activerecord/test/fixtures/edge.rb | 5 ++++ activerecord/test/fixtures/edges.yml | 6 ++++ activerecord/test/fixtures/mixin.rb | 1 + activerecord/test/fixtures/vertex.rb | 9 ++++++ activerecord/test/fixtures/vertices.yml | 4 +++ 7 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 activerecord/test/fixtures/edge.rb create mode 100644 activerecord/test/fixtures/edges.yml create mode 100644 activerecord/test/fixtures/vertex.rb create mode 100644 activerecord/test/fixtures/vertices.yml (limited to 'activerecord/test') diff --git a/activerecord/test/associations_cascaded_eager_loading_test.rb b/activerecord/test/associations_cascaded_eager_loading_test.rb index 20df8cd30e..863af199ce 100644 --- a/activerecord/test/associations_cascaded_eager_loading_test.rb +++ b/activerecord/test/associations_cascaded_eager_loading_test.rb @@ -11,7 +11,7 @@ require 'fixtures/topic' require 'fixtures/reply' class CascadedEagerLoadingTest < Test::Unit::TestCase - fixtures :authors, :mixins, :companies, :posts, :categorizations, :topics + fixtures :authors, :mixins, :companies, :posts, :topics def test_eager_association_loading_with_cascaded_two_levels authors = Author.find(:all, :include=>{:posts=>:comments}, :order=>"authors.id") @@ -94,7 +94,7 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase author.posts.first.very_special_comment end end - + def test_eager_association_loading_of_stis_with_multiple_references authors = Author.find(:all, :include => { :posts => { :special_comments => { :post => [ :special_comments, :very_special_comment ] } } }, :order => 'comments.body, very_special_comments_posts.body', :conditions => 'posts.id = 4') assert_equal [authors(:david)], authors @@ -103,9 +103,36 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase authors.first.posts.first.special_comments.first.post.very_special_comment end end - - def test_eager_association_loading_with_recursive_cascaded_three_levels + + def test_eager_association_loading_with_recursive_cascading_three_levels_has_many root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:children=>{:children=>:children}}, :order => 'mixins.id') assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.children.first.children.first.children.first } end + + def test_eager_association_loading_with_recursive_cascading_three_levels_has_one + root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:first_child=>{:first_child=>:first_child}}, :order => 'mixins.id') + assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.first_child.first_child.first_child } + end + + def test_eager_association_loading_with_recursive_cascading_three_levels_belongs_to + leaf_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:parent=>{:parent=>:parent}}, :order => 'mixins.id DESC') + assert_equal mixins(:recursively_cascaded_tree_1), assert_no_queries { leaf_node.parent.parent.parent } + end +end + + +require 'fixtures/vertex' +require 'fixtures/edge' +class CascadedEagerLoadingTest < Test::Unit::TestCase + fixtures :edges, :vertices + + def test_eager_association_loading_with_recursive_cascading_four_levels_has_many_through + source = Vertex.find(:first, :include=>{:sinks=>{:sinks=>{:sinks=>:sinks}}}, :order => 'vertices.id') + assert_equal vertices(:vertex_4), assert_no_queries { source.sinks.first.sinks.first.sinks.first } + end + + def test_eager_association_loading_with_recursive_cascading_four_levels_has_and_belongs_to_many + sink = Vertex.find(:first, :include=>{:sources=>{:sources=>{:sources=>:sources}}}, :order => 'vertices.id DESC') + assert_equal vertices(:vertex_1), assert_no_queries { sink.sources.first.sources.first.sources.first.sources.first } + end end diff --git a/activerecord/test/fixtures/db_definitions/schema.rb b/activerecord/test/fixtures/db_definitions/schema.rb index a5f2c9dc10..f565b74564 100644 --- a/activerecord/test/fixtures/db_definitions/schema.rb +++ b/activerecord/test/fixtures/db_definitions/schema.rb @@ -39,4 +39,14 @@ ActiveRecord::Schema.define do t.column :author_id, :integer t.column :favorite_author_id, :integer end + + create_table :vertices, :force => true do |t| + t.column :label, :string + end + + create_table :edges, :force => true do |t| + t.column :source_id, :integer, :null => false + t.column :sink_id, :integer, :null => false + end + add_index :edges, [:source_id, :sink_id], :unique => true end diff --git a/activerecord/test/fixtures/edge.rb b/activerecord/test/fixtures/edge.rb new file mode 100644 index 0000000000..55e0c31fcb --- /dev/null +++ b/activerecord/test/fixtures/edge.rb @@ -0,0 +1,5 @@ +# This class models an edge in a directed graph. +class Edge < ActiveRecord::Base + belongs_to :source, :class_name => 'Vertex', :foreign_key => 'source_id' + belongs_to :sink, :class_name => 'Vertex', :foreign_key => 'sink_id' +end diff --git a/activerecord/test/fixtures/edges.yml b/activerecord/test/fixtures/edges.yml new file mode 100644 index 0000000000..c16c70dd2f --- /dev/null +++ b/activerecord/test/fixtures/edges.yml @@ -0,0 +1,6 @@ +<% (1..4).each do |id| %> +edge_<%= id %>: + id: <%= id %> + source_id: <%= id %> + sink_id: <%= id + 1 %> +<% end %> \ No newline at end of file diff --git a/activerecord/test/fixtures/mixin.rb b/activerecord/test/fixtures/mixin.rb index afc976f8f1..1f200da62d 100644 --- a/activerecord/test/fixtures/mixin.rb +++ b/activerecord/test/fixtures/mixin.rb @@ -12,6 +12,7 @@ end class RecursivelyCascadedTreeMixin < Mixin acts_as_tree :foreign_key => "parent_id" + has_one :first_child, :class_name => 'RecursivelyCascadedTreeMixin', :foreign_key => :parent_id end class ListMixin < Mixin diff --git a/activerecord/test/fixtures/vertex.rb b/activerecord/test/fixtures/vertex.rb new file mode 100644 index 0000000000..f4c11144de --- /dev/null +++ b/activerecord/test/fixtures/vertex.rb @@ -0,0 +1,9 @@ +# This class models a vertex in a directed graph. +class Vertex < ActiveRecord::Base + has_many :sink_edges, :class_name => 'Edge', :foreign_key => 'source_id' + has_many :sinks, :through => :sink_edges, :source => :sink + + has_and_belongs_to_many :sources, + :class_name => 'Vertex', :join_table => 'edges', + :foreign_key => 'sink_id', :association_foreign_key => 'source_id' +end diff --git a/activerecord/test/fixtures/vertices.yml b/activerecord/test/fixtures/vertices.yml new file mode 100644 index 0000000000..8af0593f75 --- /dev/null +++ b/activerecord/test/fixtures/vertices.yml @@ -0,0 +1,4 @@ +<% (1..5).each do |id| %> +vertex_<%= id %>: + id: <%= id %> +<% end %> \ No newline at end of file -- cgit v1.2.3