diff options
author | miloops <miloops@gmail.com> | 2008-11-21 19:20:33 -0300 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-12-01 20:22:31 +0100 |
commit | 97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859 (patch) | |
tree | 77998cf59255d49323d7f7063025ca895e162f62 /activerecord/test | |
parent | 0c4ba90aa1ea6a8d386c724a55a31e63a13c46ab (diff) | |
download | rails-97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859.tar.gz rails-97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859.tar.bz2 rails-97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859.zip |
Add :having option to find, to use in combination with grouped finds. Also added to has_many and has_and_belongs_to_many associations.
Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1028 state:committed]
Diffstat (limited to 'activerecord/test')
6 files changed, 20 insertions, 0 deletions
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index b5bedf3704..2f08e09d43 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -658,6 +658,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, categories(:technology).posts_gruoped_by_title.size end + def test_find_scoped_grouped_having + assert_equal 2, projects(:active_record).well_payed_salary_groups.size + assert projects(:active_record).well_payed_salary_groups.all? { |g| g.salary > 10000 } + end + def test_get_ids assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort assert_equal [projects(:active_record).id], developers(:jamis).project_ids diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 59784e1bcb..816ceb6855 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -255,6 +255,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, companies(:first_firm).clients_grouped_by_name.length end + def test_find_scoped_grouped_having + assert_equal 1, authors(:david).popular_grouped_posts.length + assert_equal 0, authors(:mary).popular_grouped_posts.length + end + def test_adding force_signal37_to_load_all_clients_of_firm natural = Client.new("name" => "Natural Company") diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 153880afbd..d4d770b04e 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -175,6 +175,13 @@ class FinderTest < ActiveRecord::TestCase assert_equal 4, developers.map(&:salary).uniq.size end + def test_find_with_group_and_having + developers = Developer.find(:all, :group => "salary", :having => "sum(salary) > 10000", :select => "salary") + assert_equal 3, developers.size + assert_equal 3, developers.map(&:salary).uniq.size + assert developers.all? { |developer| developer.salary > 10000 } + end + def test_find_with_entire_select_statement topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'" diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index e5b19ff9e4..4ffac4fe32 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -1,6 +1,7 @@ class Author < ActiveRecord::Base has_many :posts has_many :posts_with_comments, :include => :comments, :class_name => "Post" + has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type" has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id' has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1 has_many :posts_with_categories, :include => :categories, :class_name => "Post" diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 4e9d247a4e..5efce6aaa6 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -14,6 +14,7 @@ class Category < ActiveRecord::Base :class_name => 'Post', :conditions => { :title => 'Yet Another Testing Title' } + has_and_belongs_to_many :popular_grouped_posts, :class_name => "Post", :group => "posts.type", :having => "sum(comments.post_id) > 2", :include => :comments has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title", :select => "title" def self.what_are_you diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index 44c692b5e7..550d4ae23c 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -13,6 +13,7 @@ class Project < ActiveRecord::Base :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"}, :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"}, :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"} + has_and_belongs_to_many :well_payed_salary_groups, :class_name => "Developer", :group => "salary", :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" attr_accessor :developers_log |