diff options
author | Michael Koziarski <michael@koziarski.com> | 2006-03-27 06:19:31 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2006-03-27 06:19:31 +0000 |
commit | 445cb5c08d4eea7bd035e86d1f38ae258c635e88 (patch) | |
tree | 2cee2182724cb3725eaa41415949cb435f2c0e7a /activerecord/test | |
parent | b8e23e37dd2342d156b36480662e6fe31b12d0ad (diff) | |
download | rails-445cb5c08d4eea7bd035e86d1f38ae258c635e88.tar.gz rails-445cb5c08d4eea7bd035e86d1f38ae258c635e88.tar.bz2 rails-445cb5c08d4eea7bd035e86d1f38ae258c635e88.zip |
Add support for :include to with_scope [andrew@redlinesoftware.com]
Remove overrated warning from adapter_test
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4064 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/adapter_test.rb | 2 | ||||
-rw-r--r-- | activerecord/test/method_scoping_test.rb | 65 |
2 files changed, 63 insertions, 4 deletions
diff --git a/activerecord/test/adapter_test.rb b/activerecord/test/adapter_test.rb index 3da1bf3e54..772dd0e5a8 100644 --- a/activerecord/test/adapter_test.rb +++ b/activerecord/test/adapter_test.rb @@ -43,8 +43,6 @@ class AdapterTest < Test::Unit::TestCase def test_current_database if @connection.respond_to?(:current_database) assert_equal "activerecord_unittest", @connection.current_database - else - warn "#{@connection.class} does not respond to #current_database" end end diff --git a/activerecord/test/method_scoping_test.rb b/activerecord/test/method_scoping_test.rb index c01aab6a36..bceb3869a6 100644 --- a/activerecord/test/method_scoping_test.rb +++ b/activerecord/test/method_scoping_test.rb @@ -1,11 +1,12 @@ require 'abstract_unit' require 'fixtures/developer' +require 'fixtures/project' require 'fixtures/comment' require 'fixtures/post' require 'fixtures/category' class MethodScopingTest < Test::Unit::TestCase - fixtures :developers, :comments, :posts + fixtures :developers, :projects, :comments, :posts def test_set_conditions Developer.with_scope(:find => { :conditions => 'just a test...' }) do @@ -60,6 +61,23 @@ class MethodScopingTest < Test::Unit::TestCase end end + def test_scoped_find_include + # with the include, will retrieve only developers for the given project + scoped_developers = Developer.with_scope(:find => { :include => :projects }) do + Developer.find(:all, :conditions => 'projects.id = 2') + end + assert scoped_developers.include?(developers(:david)) + assert !scoped_developers.include?(developers(:jamis)) + assert_equal 1, scoped_developers.size + end + + def test_scoped_count_include + # with the include, will retrieve only developers for the given project + Developer.with_scope(:find => { :include => :projects }) do + assert_equal 1, Developer.count('projects.id = 2') + end + end + def test_scoped_create new_comment = nil @@ -108,7 +126,7 @@ class MethodScopingTest < Test::Unit::TestCase end class NestedScopingTest < Test::Unit::TestCase - fixtures :developers, :comments, :posts + fixtures :developers, :projects, :comments, :posts def test_merge_options Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do @@ -160,6 +178,49 @@ class NestedScopingTest < Test::Unit::TestCase end end + def test_nested_scoped_find_include + Developer.with_scope(:find => { :include => :projects }) do + Developer.with_scope(:find => { :conditions => "projects.id = 2" }) do + assert_nothing_raised { Developer.find(1) } + assert_equal('David', Developer.find(:first).name) + end + end + end + + def test_nested_scoped_find_merged_include + # :include's remain unique and don't "double up" when merging + Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do + Developer.with_scope(:find => { :include => :projects }) do + assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length + assert_equal('David', Developer.find(:first).name) + end + end + + # the nested scope doesn't remove the first :include + Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do + Developer.with_scope(:find => { :include => [] }) do + assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length + assert_equal('David', Developer.find(:first).name) + end + end + + # mixing array and symbol include's will merge correctly + Developer.with_scope(:find => { :include => [:projects], :conditions => "projects.id = 2" }) do + Developer.with_scope(:find => { :include => :projects }) do + assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length + assert_equal('David', Developer.find(:first).name) + end + end + end + + def test_nested_scoped_find_replace_include + Developer.with_scope(:find => { :include => :projects }) do + Developer.with_exclusive_scope(:find => { :include => [] }) do + assert_equal 0, Developer.instance_eval('current_scoped_methods')[:find][:include].length + end + end + end + def test_three_level_nested_exclusive_scoped_find Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do assert_equal('Jamis', Developer.find(:first).name) |