diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-08 15:07:23 -0700 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-08 15:07:23 -0700 |
commit | cbf1dc78429bcceb9e90a3ff3159eafd8af5c711 (patch) | |
tree | 765e0bc392d77a62118dfe2fead7dbdd07b06467 /activerecord/test/cases/base_test.rb | |
parent | 04baa4b2cad105718ac6acda3d82b6521c4536ea (diff) | |
parent | 72317883ed3fd035a94ca1536def364d2d00a6c6 (diff) | |
download | rails-cbf1dc78429bcceb9e90a3ff3159eafd8af5c711.tar.gz rails-cbf1dc78429bcceb9e90a3ff3159eafd8af5c711.tar.bz2 rails-cbf1dc78429bcceb9e90a3ff3159eafd8af5c711.zip |
Merge pull request #2757 from andmej/first_or_create_pull_request
Add first_or_create family of methods to Active Record
Diffstat (limited to 'activerecord/test/cases/base_test.rb')
-rw-r--r-- | activerecord/test/cases/base_test.rb | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 87f5b5ee81..63879259af 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -278,6 +278,37 @@ class BasicsTest < ActiveRecord::TestCase assert_equal(true, cb.frickinawesome) end + def test_first_or_create + parrot = Bird.first_or_create(:color => 'green', :name => 'parrot') + assert parrot.persisted? + the_same_parrot = Bird.first_or_create(:color => 'yellow', :name => 'macaw') + assert_equal parrot, the_same_parrot + end + + def test_first_or_create_bang + assert_raises(ActiveRecord::RecordInvalid) { Bird.first_or_create! } + parrot = Bird.first_or_create!(:color => 'green', :name => 'parrot') + assert parrot.persisted? + the_same_parrot = Bird.first_or_create!(:color => 'yellow', :name => 'macaw') + assert_equal parrot, the_same_parrot + end + + def test_first_or_new + parrot = Bird.first_or_new(:color => 'green', :name => 'parrot') + assert_kind_of Bird, parrot + assert !parrot.persisted? + assert parrot.new_record? + assert parrot.valid? + end + + def test_first_or_build + parrot = Bird.first_or_build(:color => 'green', :name => 'parrot') + assert_kind_of Bird, parrot + assert !parrot.persisted? + assert parrot.new_record? + assert parrot.valid? + end + def test_load topics = Topic.find(:all, :order => 'id') assert_equal(4, topics.size) |