From a8b10a2a8d6f8d861453803264b9ef1b0cadbc6b Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 28 Dec 2009 01:08:34 +0530 Subject: Add relation#merge to merge two relations --- activerecord/test/cases/relations_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 2ec8c0d3a2..c639fb978d 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -328,4 +328,12 @@ class RelationTest < ActiveRecord::TestCase assert davids.loaded? end + def test_relation_merging + devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3") + assert_equal [developers(:david), developers(:jamis)], devs.to_a + + dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*, count(id) id_count').group('id') + assert_equal [developers(:poor_jamis)], dev_with_count.to_a + assert_equal 1, dev_with_count.first.id_count.to_i + end end -- cgit v1.2.3 From 51a1d5a6708726ae03deb5869f36e7d5cca22a6e Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 28 Dec 2009 01:33:20 +0530 Subject: Handle preloads and eager loads when merging relations --- activerecord/test/cases/relations_test.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index c639fb978d..0644db362a 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -332,8 +332,24 @@ class RelationTest < ActiveRecord::TestCase devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3") assert_equal [developers(:david), developers(:jamis)], devs.to_a - dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*, count(id) id_count').group('id') + dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*').group('id') assert_equal [developers(:poor_jamis)], dev_with_count.to_a - assert_equal 1, dev_with_count.first.id_count.to_i + end + + def test_relation_merging_with_eager_load + relations = [] + relations << (Post.order('comments.id DESC') & Post.eager_load(:last_comment) & Post.scoped) + relations << (Post.eager_load(:last_comment) & Post.order('comments.id DESC') & Post.scoped) + + relations.each do |posts| + post = posts.find { |p| p.id == 1 } + assert_equal Post.find(1).last_comment, post.last_comment + end + end + + def test_relation_merging_with_preload + [Post.scoped & Post.preload(:author), Post.preload(:author) & Post.scoped].each do |posts| + assert_queries(2) { assert posts.first.author } + end end end -- cgit v1.2.3 From 630dc5073075ba73251400400caae802f8d97d41 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Mon, 28 Dec 2009 02:50:04 +0530 Subject: Fix relation tests for postgres --- activerecord/test/cases/relations_test.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 0644db362a..61fcc7ca46 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -38,7 +38,7 @@ class RelationTest < ActiveRecord::TestCase end def test_scoped_first - topics = Topic.scoped + topics = Topic.scoped.order('id ASC') assert_queries(1) do 2.times { assert_equal "The First Topic", topics.first.title } @@ -48,7 +48,7 @@ class RelationTest < ActiveRecord::TestCase end def test_loaded_first - topics = Topic.scoped + topics = Topic.scoped.order('id ASC') assert_queries(1) do topics.all # force load @@ -244,7 +244,7 @@ class RelationTest < ActiveRecord::TestCase author = Author.scoped.find_by_id!(authors(:david).id) assert_equal "David", author.name - assert_raises(ActiveRecord::RecordNotFound) { Author.scoped.find_by_id_and_name!('invalid', 'wt') } + assert_raises(ActiveRecord::RecordNotFound) { Author.scoped.find_by_id_and_name!(20, 'invalid') } end def test_dynamic_find_all_by_attributes @@ -281,7 +281,7 @@ class RelationTest < ActiveRecord::TestCase david = authors.find(authors(:david).id) assert_equal 'David', david.name - assert_raises(ActiveRecord::RecordNotFound) { authors.where(:name => 'lifo').find('invalid') } + assert_raises(ActiveRecord::RecordNotFound) { authors.where(:name => 'lifo').find('42') } end def test_find_ids @@ -294,8 +294,8 @@ class RelationTest < ActiveRecord::TestCase assert_equal 'Mary', results[1].name assert_equal results, authors.find([authors(:david).id, authors(:mary).id]) - assert_raises(ActiveRecord::RecordNotFound) { authors.where(:name => 'lifo').find(authors(:david).id, 'invalid') } - assert_raises(ActiveRecord::RecordNotFound) { authors.find(['invalid', 'oops']) } + assert_raises(ActiveRecord::RecordNotFound) { authors.where(:name => 'lifo').find(authors(:david).id, '42') } + assert_raises(ActiveRecord::RecordNotFound) { authors.find(['42', 43]) } end def test_exists @@ -303,7 +303,8 @@ class RelationTest < ActiveRecord::TestCase assert davids.exists? assert davids.exists?(authors(:david).id) assert ! davids.exists?(authors(:mary).id) - assert ! davids.exists?("hax'id") + assert ! davids.exists?("42") + assert ! davids.exists?(42) fake = Author.where(:name => 'fake author') assert ! fake.exists? @@ -332,7 +333,7 @@ class RelationTest < ActiveRecord::TestCase devs = Developer.where("salary >= 80000") & Developer.limit(2) & Developer.order('id ASC').where("id < 3") assert_equal [developers(:david), developers(:jamis)], devs.to_a - dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*').group('id') + dev_with_count = Developer.limit(1) & Developer.order('id DESC') & Developer.select('developers.*') assert_equal [developers(:poor_jamis)], dev_with_count.to_a end -- cgit v1.2.3