aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-01-25 12:45:01 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-01-25 12:45:01 +0000
commitefa81dad5158eb61243d00811e4664248dd2c167 (patch)
tree161e540cd3382f82afd867e5d1a876619ad61b2f /activerecord/test
parent256b594a03f184e63c4682a427a1982cbf99f6fa (diff)
downloadrails-efa81dad5158eb61243d00811e4664248dd2c167.tar.gz
rails-efa81dad5158eb61243d00811e4664248dd2c167.tar.bz2
rails-efa81dad5158eb61243d00811e4664248dd2c167.zip
Added the option of supplying an array of ids and attributes to Base#update, so that multiple records can be updated at once (inspired by #526/Duane Johnson). Added the option of supplying an array of attributes to Base#create, so that multiple records can be created at once. Added that Base#delete and Base#destroy both can take an array of ids to delete/destroy #336. Added that has_many association build and create methods can take arrays of record data like Base#create and Base#build to build/create multiple records at once.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@504 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test')
-rwxr-xr-xactiverecord/test/associations_test.rb13
-rwxr-xr-xactiverecord/test/base_test.rb36
2 files changed, 35 insertions, 14 deletions
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 7ede042879..2095f70cd3 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -386,6 +386,14 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal 2, @signals37.clients_of_firm(true).size
end
+ def test_build_many
+ new_clients = @signals37.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}])
+ assert_equal 2, new_clients.size
+
+ assert @signals37.save
+ assert_equal 3, @signals37.clients_of_firm(true).size
+ end
+
def test_invalid_build
new_client = @signals37.clients_of_firm.build
assert new_client.new_record?
@@ -403,6 +411,11 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal new_client, @signals37.clients_of_firm.last
assert_equal new_client, @signals37.clients_of_firm(true).last
end
+
+ def test_create_many
+ @signals37.clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
+ assert_equal 3, @signals37.clients_of_firm(true).size
+ end
def test_deleting
force_signal37_to_load_all_clients_of_firm
diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb
index 2119785caf..0f93f2ffbc 100755
--- a/activerecord/test/base_test.rb
+++ b/activerecord/test/base_test.rb
@@ -114,6 +114,12 @@ class BasicsTest < Test::Unit::TestCase
topicReloaded = Topic.find(id)
assert_equal("New Topic", topicReloaded.title)
end
+
+ def test_create_many
+ topics = Topic.create([ { "title" => "first" }, { "title" => "second" }])
+ assert_equal 2, topics.size
+ assert_equal "first", topics.first.title
+ end
def test_create_columns_not_equal_attributes
topic = Topic.new
@@ -256,12 +262,22 @@ class BasicsTest < Test::Unit::TestCase
end
def test_destroy_all
- assert_equal(2, Topic.find_all.size)
+ assert_equal 2, Topic.find_all.size
Topic.destroy_all "author_name = 'Mary'"
- assert_equal(1, Topic.find_all.size)
+ assert_equal 1, Topic.find_all.size
end
-
+
+ def test_destroy_many
+ Client.destroy([2, 3])
+ assert_equal 0, Client.count
+ end
+
+ def test_delete_many
+ Topic.delete([1, 2])
+ assert_equal 0, Topic.count
+ end
+
def test_boolean_attributes
assert ! Topic.find(1).approved?
assert Topic.find(2).approved?
@@ -292,21 +308,13 @@ class BasicsTest < Test::Unit::TestCase
assert_equal "bulk updated again!", Topic.find(2).content
end
- def test_update_collection
- ids_and_attributes = { "1" => { "content" => "1 updated" }, "2" => { "content" => "2 updated" } }
- updated = Topic.update_collection(ids_and_attributes)
+ def test_update_many
+ topic_data = { "1" => { "content" => "1 updated" }, "2" => { "content" => "2 updated" } }
+ updated = Topic.update(topic_data.keys, topic_data.values)
assert_equal 2, updated.size
assert_equal "1 updated", Topic.find(1).content
assert_equal "2 updated", Topic.find(2).content
-
- ids_and_attributes["1"]["content"] = "one updated"
- ids_and_attributes["2"]["content"] = "two updated"
- updated = Topic.update_collection(ids_and_attributes) { |ar, attrs| ar.id == 1 }
-
- assert_equal 1, updated.size
- assert_equal "one updated", Topic.find(1).content
- assert_equal "2 updated", Topic.find(2).content
end
def test_delete_all