aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/locking_test.rb
diff options
context:
space:
mode:
authorJamis Buck <jamis@37signals.com>2007-02-07 16:10:40 +0000
committerJamis Buck <jamis@37signals.com>2007-02-07 16:10:40 +0000
commit83752373b9c83bf585b5fe44836c65612b680cf9 (patch)
tree0096d86743e56f8266daa280d4e364db734e3dab /activerecord/test/locking_test.rb
parentf458b376c545d8eae5189d1ecc16245cafe7e796 (diff)
downloadrails-83752373b9c83bf585b5fe44836c65612b680cf9.tar.gz
rails-83752373b9c83bf585b5fe44836c65612b680cf9.tar.bz2
rails-83752373b9c83bf585b5fe44836c65612b680cf9.zip
Made increment_counter/decrement_counter play nicely with optimistic locking, and added a more general update_counters method
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6139 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/locking_test.rb')
-rw-r--r--activerecord/test/locking_test.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/activerecord/test/locking_test.rb b/activerecord/test/locking_test.rb
index 471771bfbb..f62f1ac61c 100644
--- a/activerecord/test/locking_test.rb
+++ b/activerecord/test/locking_test.rb
@@ -1,5 +1,6 @@
require 'abstract_unit'
require 'fixtures/person'
+require 'fixtures/reader'
require 'fixtures/legacy_thing'
class LockWithoutDefault < ActiveRecord::Base; end
@@ -12,6 +13,11 @@ end
class OptimisticLockingTest < Test::Unit::TestCase
fixtures :people, :legacy_things
+ # need to disable transactional fixtures, because otherwise the sqlite3
+ # adapter (at least) chokes when we try and change the schema in the middle
+ # of a test (see test_increment_counter_*).
+ self.use_transactional_fixtures = false
+
def test_lock_existing
p1 = Person.find(1)
p2 = Person.find(1)
@@ -73,6 +79,51 @@ class OptimisticLockingTest < Test::Unit::TestCase
t1 = LockWithCustomColumnWithoutDefault.new
assert_equal 0, t1.custom_lock_version
end
+
+ { :lock_version => Person, :custom_lock_version => LegacyThing }.each do |name, model|
+ define_method("test_increment_counter_updates_#{name}") do
+ counter_test model, 1 do |id|
+ model.increment_counter :test_count, id
+ end
+ end
+
+ define_method("test_decrement_counter_updates_#{name}") do
+ counter_test model, -1 do |id|
+ model.decrement_counter :test_count, id
+ end
+ end
+
+ define_method("test_update_counters_updates_#{name}") do
+ counter_test model, 1 do |id|
+ model.update_counters id, :test_count => 1
+ end
+ end
+ end
+
+ private
+
+ def add_counter_column_to(model)
+ model.connection.add_column model.table_name, :test_count, :integer, :null => false, :default => 0
+ model.reset_column_information
+ end
+
+ def remove_counter_column_from(model)
+ model.connection.remove_column model.table_name, :test_count
+ model.reset_column_information
+ end
+
+ def counter_test(model, expected_count)
+ add_counter_column_to(model)
+ object = model.find(:first)
+ assert_equal 0, object.test_count
+ assert_equal 0, object.send(model.locking_column)
+ yield object.id
+ object.reload
+ assert_equal expected_count, object.test_count
+ assert_equal 1, object.send(model.locking_column)
+ ensure
+ remove_counter_column_from(model)
+ end
end