diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2005-10-28 04:53:22 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2005-10-28 04:53:22 +0000 |
commit | e7cd7e9d954f90737138495f6418bd4fab428931 (patch) | |
tree | 9c19565b4da5f19048d33f2400c9c4075f68cfd4 /activerecord/test | |
parent | f2c920ec24f318e28c0a1e78c019a331368ff5bb (diff) | |
download | rails-e7cd7e9d954f90737138495f6418bd4fab428931.tar.gz rails-e7cd7e9d954f90737138495f6418bd4fab428931.tar.bz2 rails-e7cd7e9d954f90737138495f6418bd4fab428931.zip |
r3800@sedna: jeremy | 2005-10-28 00:39:05 -0700
Readonly constraints, association collection method_missing, dup constraint options
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2774 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/conditions_scoping_test.rb | 9 | ||||
-rwxr-xr-x | activerecord/test/readonly_test.rb | 66 |
2 files changed, 70 insertions, 5 deletions
diff --git a/activerecord/test/conditions_scoping_test.rb b/activerecord/test/conditions_scoping_test.rb index b945593b57..a997ee9e85 100644 --- a/activerecord/test/conditions_scoping_test.rb +++ b/activerecord/test/conditions_scoping_test.rb @@ -41,6 +41,15 @@ class ConditionsScopingTest < Test::Unit::TestCase assert_equal 1, Developer.count("name LIKE 'fixture_1%'") end end + + def test_immutable_constraint + options = { :conditions => "name = 'David'" } + Developer.constrain(options) do + assert_equal %w(David), Developer.find(:all).map { |d| d.name } + options[:conditions] = "name != 'David'" + assert_equal %w(David), Developer.find(:all).map { |d| d.name } + end + end end class HasManyScopingTest< Test::Unit::TestCase diff --git a/activerecord/test/readonly_test.rb b/activerecord/test/readonly_test.rb index 458cd6886f..d621432596 100755 --- a/activerecord/test/readonly_test.rb +++ b/activerecord/test/readonly_test.rb @@ -1,12 +1,19 @@ require 'abstract_unit' +require 'fixtures/post' +require 'fixtures/comment' require 'fixtures/developer' require 'fixtures/project' +# Dummy class methods to test implicit association constraints. +def Comment.foo() find :first end +def Project.foo() find :first end + + class ReadOnlyTest < Test::Unit::TestCase - fixtures :developers, :projects, :developers_projects + fixtures :posts, :comments, :developers, :projects, :developers_projects def test_cant_save_readonly_record - dev = Developer.find(:first) + dev = Developer.find(1) assert !dev.readonly? dev.readonly! @@ -21,21 +28,70 @@ class ReadOnlyTest < Test::Unit::TestCase assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! } end + def test_find_with_readonly_option Developer.find(:all).each { |d| assert !d.readonly? } Developer.find(:all, :readonly => false).each { |d| assert !d.readonly? } Developer.find(:all, :readonly => true).each { |d| assert d.readonly? } end + def test_find_with_joins_option_implies_readonly - Developer.find(:all, :joins => '').each { |d| assert d.readonly? } - Developer.find(:all, :joins => '', :readonly => false).each { |d| assert !d.readonly? } + # Blank joins don't count. + Developer.find(:all, :joins => ' ').each { |d| assert !d.readonly? } + Developer.find(:all, :joins => ' ', :readonly => false).each { |d| assert !d.readonly? } + + # Others do. + Developer.find(:all, :joins => ', projects').each { |d| assert d.readonly? } + Developer.find(:all, :joins => ', projects', :readonly => false).each { |d| assert !d.readonly? } end + def test_habtm_find_readonly - dev = Developer.find(:first) + dev = Developer.find(1) + assert !dev.projects.empty? dev.projects.each { |p| assert !p.readonly? } dev.projects.find(:all) { |p| assert !p.readonly? } dev.projects.find(:all, :readonly => true) { |p| assert p.readonly? } end + + def test_has_many_find_readonly + post = Post.find(1) + assert !post.comments.empty? + post.comments.each { |r| assert !r.readonly? } + post.comments.find(:all) { |r| assert !r.readonly? } + post.comments.find(:all, :readonly => true) { |r| assert r.readonly? } + end + + + def test_readonly_constraint + Post.constrain(:conditions => '1=1') do + assert !Post.find(1).readonly? + assert Post.find(1, :readonly => true).readonly? + assert !Post.find(1, :readonly => false).readonly? + end + + Post.constrain(:joins => ' ') do + assert !Post.find(1).readonly? + assert Post.find(1, :readonly => true).readonly? + assert !Post.find(1, :readonly => false).readonly? + end + + Post.constrain(:joins => ', developers') do + assert Post.find(1).readonly? + assert Post.find(1, :readonly => true).readonly? + assert !Post.find(1, :readonly => false).readonly? + end + + Post.constrain(:readonly => true) do + assert Post.find(1).readonly? + assert Post.find(1, :readonly => true).readonly? + assert !Post.find(1, :readonly => false).readonly? + end + end + + def test_association_collection_method_missing_constraint_not_readonly + assert !Developer.find(1).projects.foo.readonly? + assert !Post.find(1).comments.foo.readonly? + end end |