diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-11-04 16:10:18 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-11-05 16:22:18 +0000 |
commit | 562583c7667f508493ab8c5b1a4215087fafd22d (patch) | |
tree | d61a983ca8e5b9d38164f34d1eb6c36b0f822be8 /activerecord/test | |
parent | 6aaae3de277b572f37e09f16ae12737c3c87dfb7 (diff) | |
download | rails-562583c7667f508493ab8c5b1a4215087fafd22d.tar.gz rails-562583c7667f508493ab8c5b1a4215087fafd22d.tar.bz2 rails-562583c7667f508493ab8c5b1a4215087fafd22d.zip |
Add ActiveRecord::Relation#uniq for toggling DISTINCT in the SQL query
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/base_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/relation_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 16 |
3 files changed, 23 insertions, 1 deletions
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 12c1cfb30e..fdb656fe13 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1935,4 +1935,10 @@ class BasicsTest < ActiveRecord::TestCase dev.update_attribute(:updated_at, nil) assert_match(/\/#{dev.id}$/, dev.cache_key) end + + def test_uniq_delegates_to_scoped + scope = stub + Bird.stubs(:scoped).returns(mock(:uniq => scope)) + assert_equal scope, Bird.uniq + end end diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb index b23ead6feb..715a378431 100644 --- a/activerecord/test/cases/relation_test.rb +++ b/activerecord/test/cases/relation_test.rb @@ -20,7 +20,7 @@ module ActiveRecord end def test_single_values - assert_equal [:limit, :offset, :lock, :readonly, :from, :reorder, :reverse_order].map(&:to_s).sort, + assert_equal [:limit, :offset, :lock, :readonly, :from, :reorder, :reverse_order, :uniq].map(&:to_s).sort, Relation::SINGLE_VALUE_METHODS.map(&:to_s).sort end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 95408a5f29..bf1eb6386a 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -1148,4 +1148,20 @@ class RelationTest < ActiveRecord::TestCase assert_equal posts(:thinking), comments(:more_greetings).post assert_equal posts(:welcome), comments(:greetings).post end + + def test_uniq + tag1 = Tag.create(:name => 'Foo') + tag2 = Tag.create(:name => 'Foo') + + query = Tag.select(:name).where(:id => [tag1.id, tag2.id]) + + assert_equal ['Foo', 'Foo'], query.map(&:name) + assert_sql(/DISTINCT/) do + assert_equal ['Foo'], query.uniq.map(&:name) + end + assert_sql(/DISTINCT/) do + assert_equal ['Foo'], query.uniq(true).map(&:name) + end + assert_equal ['Foo', 'Foo'], query.uniq(true).uniq(false).map(&:name) + end end |